fry2010 Posted March 1, 2011 Share Posted March 1, 2011 I have several tables, all with a primary key set. But I have just performed an explain on them, and it appears that the primary key is not being used, even though the field is being used in a where clause. I also notice that when I change the tables to include a compound key that it then uses the primary key. Here is one of the problematic tables: create table authors ( username varchar(22) not null PRIMARY KEY, email char(60) not null, password char(133) not null, website char(120) not null default '-', account_type tinyint(1) not null default '0', profile char(150) not null default '-' ); When using: EXPLAIN SELECT * FROM authors WHERE username = 'username' I get this result: possible_keys: NULL; key: NULL; ref: NULL; rows: NULL; extra: Impossible WHERE noticed after reading const tables; This occurs on several tables. But when I change the table to this: create table authors ( username varchar(22) not null, email char(60) not null, password char(133) not null, website char(120) not null default '-', account_type tinyint(1) not null default '0', profile char(150) not null default '-', PRIMARY KEY (username, email) ); I get this result from the explain: possible_keys: PRIMARY; key: PRIMARY; ref: const; rows: 1; extra: Using where, Using index; Am I missing something? Quote Link to comment https://forums.phpfreaks.com/topic/229292-impossible-where-noticed/ Share on other sites More sharing options...
fry2010 Posted March 1, 2011 Author Share Posted March 1, 2011 I forgot to say that it is mysql version 5.1 Quote Link to comment https://forums.phpfreaks.com/topic/229292-impossible-where-noticed/#findComment-1181471 Share on other sites More sharing options...
fry2010 Posted March 1, 2011 Author Share Posted March 1, 2011 Could this be to do with the fact I specified NOT NULL values, and since the query I enter does not match any record, it therefore cannot return any information about the query? Or something along those lines? Quote Link to comment https://forums.phpfreaks.com/topic/229292-impossible-where-noticed/#findComment-1181560 Share on other sites More sharing options...
fenway Posted March 1, 2011 Share Posted March 1, 2011 It's because the PK is indexed, and it knows nothing matches. But NEVER use a varchar for a pK. Quote Link to comment https://forums.phpfreaks.com/topic/229292-impossible-where-noticed/#findComment-1181568 Share on other sites More sharing options...
fry2010 Posted March 1, 2011 Author Share Posted March 1, 2011 great, thanks again fenway. I just used varchar in that to test the explain. Quote Link to comment https://forums.phpfreaks.com/topic/229292-impossible-where-noticed/#findComment-1181581 Share on other sites More sharing options...
mikosiko Posted March 2, 2011 Share Posted March 2, 2011 I forgot to say that it is mysql version 5.1 probable explanation: http://bugs.mysql.com/bug.php?id=29087 there is a patch published.... also check if you have a record with "username" containing a blank space at the end. fenway gave you a very good advice regarding varchars/PK's Quote Link to comment https://forums.phpfreaks.com/topic/229292-impossible-where-noticed/#findComment-1181627 Share on other sites More sharing options...
fry2010 Posted March 2, 2011 Author Share Posted March 2, 2011 ok. I have another question: I have found somewhere else that says another optimization with a query is to use LIMIT 1, but if you are selecting from a primary key, would making it LIMIT 1 even make a difference since only 1 row is returned anyway? eg: "SELECT * FROM table WHERE primrykey = 'example' LIMIT 1"; as opposed to: "SELECT * FROM table WHERE primrykey = 'example'"; Quote Link to comment https://forums.phpfreaks.com/topic/229292-impossible-where-noticed/#findComment-1181757 Share on other sites More sharing options...
gizmola Posted March 2, 2011 Share Posted March 2, 2011 No there is no advantage to limiting a result set that only has one row in it. Quote Link to comment https://forums.phpfreaks.com/topic/229292-impossible-where-noticed/#findComment-1181773 Share on other sites More sharing options...
fry2010 Posted March 2, 2011 Author Share Posted March 2, 2011 K thanks. Looks like my problems sorted for now. Quote Link to comment https://forums.phpfreaks.com/topic/229292-impossible-where-noticed/#findComment-1181814 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.