Jump to content

Quoted varchar() uses index, unquoted doesn't?


anon_login_001

Recommended Posts

I have an index on this table that contains the 'deleted' tinyint() column first, followed by the parent_id varchar(36) column.

 

When I run this query:

SELECT * FROM registration_task 
WHERE 	
 name like '%address%' 
 and deleted=0
 and status!='Completed' 
 and status!='Not Applicable'
 and parent_id=120142;

 

an EXPLAIN tells me that it uses the correct index, with a key_len of '1', and that it searches 157,000-some-odd records. Obviously, using only the 'deleted' column of the index.

 

When I add quotes in the query around the parent_id, as such:

SELECT * FROM registration_task 
WHERE 
 name like '%address%' 
 and deleted=0 
 and status!='Completed' 
 and status!='Not Applicable' 
 and parent_id='120142';

 

an EXPLAIN shows that the key_len used was 39 (deleted and the parent_id) and it only intends to search '1' row.

 

Can someone please enlighten me about this behavior? I haven't really come accross it before, and am just looking for an explanation of why an Index wouldn't get utilized just because of an unquoted parameter.

 

(though, I know that the param should have been quoted in the first place... I stumbled upon this in the slow query log, and was experimenting for optimization. )

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.