29

I have a unique compound key like fr(fromid,toid) in the table, when I run the query with explain I get the following result:

Impossible WHERE noticed after reading const tables`

The query I ran:

explain SELECT rid FROM relationship WHERE fromid=78 AND toid=60   

Any help?

EDIT1:
When I use the below query:

explain SELECT rid FROM relationship WHERE fromid=60 and toid=78 AND is_approved='s'  OR is_approved='f' OR is_approved='t'

I see USING WHERE instead of the previous message, but when I use the below query:

explain SELECT rid FROM relationship WHERE fromid=60 and toid=78 AND (is_approved='s'  OR is_approved='f' OR is_approved='t')  

I again get the first impossible ... message! What these parenthesis do here?

EDIT2:

CREATE TABLE `relationship` (
 `rid` int(10) unsigned NOT NULL AUTO_INCREMENT,
 `fromid` mediumint(8) unsigned NOT NULL,
 `toid` mediumint(8) unsigned NOT NULL,
 `type` tinyint(3) unsigned NOT NULL,
 `is_approved` char(1) NOT NULL,
 PRIMARY KEY (`rid`),
 UNIQUE KEY `fromid` (`fromid`,`toid`),
 KEY `toid` (`toid`),
 CONSTRAINT `relationship_ibfk_1` FOREIGN KEY (`fromid`) REFERENCES `user` (`uid`) ON DELETE CASCADE ON UPDATE CASCADE,
 CONSTRAINT `relationship_ibfk_2` FOREIGN KEY (`toid`) REFERENCES `user` (`uid`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB

EDIT3:
As mysql site say:

Impossible WHERE noticed after reading const tables

MySQL has read all const (and system) tables and notice that the WHERE clause is always false.

But in the query I get the result I want, the WHERE part is not false. Is there someone who could explain this and shed some light on the subject?

2
  • What does SELECT COUNT(1) FROM relationship WHERE fromid=78 AND toid=60; return ??? Jul 20, 2012 at 14:51
  • @RolandoMySQLDBA, there will be using index in extra instead of impossible...
    – Alireza
    Jul 20, 2012 at 15:34

4 Answers 4

23

You are getting the message

Impossible WHERE noticed after reading const tables

This is documented in the page you already linked.

MySQL has read all const (and system) tables and notice that the WHERE clause is always false

const tables are defined as

The table has at most one matching row, which is read at the start of the query. ... const is used when you compare all parts of a PRIMARY KEY or UNIQUE index to constant values.

You have a UNIQUE KEY on (fromid,toid). The query on WHERE fromid=78 AND toid=60 can be satisfied by reading this unique index. From the message you are getting this must return no results.

Similarly the query WHERE fromid=60 and toid=78 AND (is_approved='s' OR is_approved='f' OR is_approved='t') can also use this index to locate the row of interest (though it still has a residual predicate to evaluate were any row to match).

Your other query is different

SELECT rid
FROM   relationship
WHERE  fromid = 60
       AND toid = 78
       AND is_approved = 's'
        OR is_approved = 'f'
        OR is_approved = 't' 

AND has a higher precedence than Or, so this is the same as

SELECT rid
FROM   relationship
WHERE  ( ( fromid = 60 ) AND ( toid = 78 ) AND ( is_approved = 's' ) )
        OR ( is_approved = 'f' )
        OR ( is_approved = 't' ) 

This can no longer use that index and has different semantics in that it will return any rows where is_approved IN ('f','t') irrespective of what the values in the other columns are.

7
  • So how should I say for example: if its fromid=12 AND toid=78 then check if is_approved='f' or is_approved='t' or is_approved='s'
    – Alireza
    Jul 21, 2012 at 4:51
  • 1
    @john.locke: That is your third query: WHERE fromid=60 AND toid=78 AND (is_approved='s' OR is_approved='f' OR is_approved='t') which can also be written as: WHERE fromid=60 AND toid=78 AND ( is_approved IN ('s', 'f', 't') ) Jul 21, 2012 at 7:39
  • 1
    Exactly. Because no row matches the fromid=60 AND toid=78 part so no further check is needed (for the is_approved part). Jul 21, 2012 at 7:41
  • 1
    You can't have row s that do. There is a unique constraint on (fromid,toid) so there would be a maximum of one surely? And from the message you say you are getting MySQL doesn't think that there is even one that does. Do you mean that you have some rows that match fromid=60 and some rows that match toid=78 but not necessarily the same rows? Jul 21, 2012 at 7:49
  • 1
    Perhaps this is the AND-OR confusion. Maybe you want all rows that have fromid=60 and all rows that have toid=78 and then from those, keep only those that have either 's' or 'f' or t' is_approved? If yes, try this condition: WHERE (fromid=60 OR toid=78) AND (is_approved IN ('s', 'f', 't')) Jul 21, 2012 at 8:52
5

MySql Explain uses the values you provide, literally, to traverse rows of the associated tables. If you provide a constant/key value which is not in the associated table, MySql Explain will stop with this error. Simply query the associated table(s) for values which do exist and provide those in your Explain query and everything will work as expected.

1
  • 3
    Impossible WHERE noticed ... is not an error. It's part of the explain. May 29, 2015 at 16:58
3

Impossible WHERE noticed after reading const tables in explain query?

This error occurs due to invalid value being put on a column which are either primary key or unique key.

Try with a correct value in the where clause .

0
0

I'm jumping into this late. But here is what I noticed for me.

I was doing this query and the item column was UNIQUE.

SELECT `vari-groupid` FROM shop_item_variations_group where `item` = 'itemnu1' limit 1

which would get the Impossible WHERE noticed after reading const tables

All I had to do was change "=" to "like" and it now is using my index.

SELECT `vari-groupid` FROM shop_item_variations_group where `item` like 'itemnu1' limit 1
3
  • Was it not using the index with the = ? Mar 28, 2019 at 15:23
  • No crazy enough it wasn't.... It was just saying this in the explain Impossible WHERE noticed after reading const tables Mar 28, 2019 at 16:18
  • Yes, but that "impossible WHERE" is usually good. means the query doesn't have to read any more from the tables or indexes. Was it slow? If not, you should not worry at all. Mar 28, 2019 at 16:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.