Jump to content

Impossible WHERE noticed?


fry2010

Recommended Posts

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?

 

Link to comment
https://forums.phpfreaks.com/topic/229292-impossible-where-noticed/
Share on other sites

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

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'";

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.