Bug #45915 SELECT on primary key with ROUND(RAND()) does not consider Index
Submitted: 2 Jul 2009 14:36 Modified: 2 Jul 2009 14:39
Reporter: Oli Sennhauser Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server Severity:S3 (Non-critical)
Version:5.1.35 OS:Any
Assigned to: CPU Architecture:Any

[2 Jul 2009 14:36] Oli Sennhauser
Description:
A SELECT on PK field with a ROUND(RAND()) leads to a full table scan instead of a (PK) index access.
Further the result is empty.

How to repeat:
CREATE TABLE `test` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `data` varchar(64) DEFAULT NULL,
  `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM
;

INSERT INTO test VALUES (NULL, 'blablablablablablablablablablablabla', NULL);
INSERT INTO test SELECT NULL, 'blablablablablablablablablablablabla', NULL FROM test;
...

explain
select * from resources
 WHERE id = 512
   AND status >= 0
;

+----+-------------+-----------+------+---------------+------+---------+-------+------+-------------+
| id | select_type | table     | type | possible_keys | key  | key_len | ref   | rows | Extra       |
+----+-------------+-----------+------+---------------+------+---------+-------+------+-------------+
|  1 | SIMPLE      | resources | ref  | id,status     | id   | 8       | const |   50 | Using where |
+----+-------------+-----------+------+---------------+------+---------+-------+------+-------------+

But:

explain
select * from resources
 WHERE id = ROUND(RAND()*4096, 0)
   AND status >= 0
;

+----+-------------+-----------+------+---------------+------+---------+------+--------+-------------+
| id | select_type | table     | type | possible_keys | key  | key_len | ref  | rows   | Extra       |
+----+-------------+-----------+------+---------------+------+---------+------+--------+-------------+
|  1 | SIMPLE      | resources | ALL  | status        | NULL | NULL    | NULL | 131173 | Using where |
+----+-------------+-----------+------+---------------+------+---------+------+--------+-------------+

Suggested fix:
No idea.
[2 Jul 2009 14:39] Oli Sennhauser
Just forget about this bug. I seem to be tired...