Thursday, 5 September 2013

MySQL PDO query optimization

MySQL PDO query optimization

I'm trying to reduce this script execution time. It queries a database
with about 2 million records about 1000 times on a loop:
foreach ($ids as $id){
$stmt=$dbh->query("SELECT SQL_CACHE * FROM `ids`
WHERE $id BETWEEN `id_start` AND `id_end`");
$rows[] = $stmt->fetch();
}
It takes forever on a 4 core 8 GB machine (about 800 seconds!). Id groups
do not overlap, ids tend to be from just a few different groups in each
execution and I have indexed both (id_start,id_end) and (id_end).
Caching improves very much the situation (running the same 1000 values
more than once is just a few seconds), but I would like to know what can I
do to speed up non cached queries.

No comments:

Post a Comment