- Querycache is disabled!
- Indexes are 100 % loaded in RAM
- index is only ID
1benchmark:
"SELECT title FROM posts LIMIT ?, 15";
? = rand(1,183655);
EXPLAIN
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE posts ALL NULL NULL NULL NULL 183637
after doing 100 loops it took over 100 sec.
2Benchmark:
"SELECT title FROM posts WHERE id = 78845 OR id = 158738 OR id = 57065 OR id = 146797 OR id = 78918 OR id = 65227 OR id = 117987 OR id = 92541 OR id = 39782 OR id = 1958 OR id = 180384 OR id = 170758 OR id = 102227 OR id = 180223 OR id = 46391";
in each loop every id is generated via rand(1,183655);
after doing 100 loops it took 6 sec to finish and it uses the primary key.
The problem is I think in the LIMIT MySQL doesn’t use the primary key for the id.
After testing with Limit again:
SECONDS: LOOPS: SQL:
41.560034990311 = 30 = "SELECT title FROM posts LIMIT ?, 15";
36.302664995193 = 30 = "SELECT title FROM posts ORDER BY id LIMIT ?, 15";
70.335160970688 = 30 = "SELECT title FROM posts ORDER BY id ASC LIMIT ?, 15";
48.453547000885 = 30 = "SELECT id, title FROM posts LIMIT ?, 15"
Somehow but I’m not 100 % sure. I updated on my VMware PHP5.1.6 to PHP5.3 since then I noticed, that these LIMIT queries take longer.
You could run this instead:
It’s not equivalent to the
LIMIT ?, 15but it will use the index.You can also check this related article.