This query shows up in the slow query logs on a site I'm working on:
Count : 1.96k (2.40%)
Time : 3984 s total, 2.029547 s avg, 2 s to 3 s max (1.57%)
95% of Time : 3728 s total, 2 s avg, 2 s to 2 s max
Lock Time (s) : 0 total, 0 avg, 0 to 0 max (0.00%)
95% of Lock : 0 total, 0 avg, 0 to 0 max
Rows sent : 0 avg, 0 to 0 max (0.00%)
Rows examined : 543.05k avg, 537.20k to 548.72k max (2.19%)
Database :
Users :
connect@prod1 172.16.100.162 : 100.00% (1963) of query, 99.85% (81732) of all users
Query abstract:
SELECT txn_id, uid, points, time_stamp, operation, description FROM userpoints_txn WHERE status = N AND expired = N AND (expirydate < N AND expirydate != N);
Query sample:
SELECT txn_id, uid, points, time_stamp, operation, description
FROM userpoints_txn
WHERE status = 0 AND expired = 0
AND (expirydate < 1289600109 AND expirydate != 0);
MariaDB [connect]> EXPLAIN SELECT txn_id, uid, points, time_stamp, operation, description FROM userpoints_txn WHERE status = 0 AND expired = 0 AND (expirydate < 1289600109 AND expirydate != 0);
+----+-------------+----------------+------+---------------+--------+---------+-------+--------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------------+------+---------------+--------+---------+-------+--------+-------------+
| 1 | SIMPLE | userpoints_txn | ref | status | status | 4 | const | 249584 | Using where |
+----+-------------+----------------+------+---------------+--------+---------+-------+--------+-------------+
1 row in set (0.00 sec)
I added an index like such:
ALTER TABLE userpoints_txn ADD INDEX (status, expired, expirydate);
With that index I get:
MariaDB [connect]> EXPLAIN SELECT txn_id, uid, points, time_stamp, operation, description FROM userpoints_txn WHERE status = 0 AND expired = 0 AND (expirydate < 1289600109 AND expirydate != 0);
+----+-------------+----------------+-------+-----------------+----------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------------+-------+-----------------+----------+---------+------+------+-------------+
| 1 | SIMPLE | userpoints_txn | range | status,status_2 | status_2 | 10 | NULL | 20 | Using where |
+----+-------------+----------------+-------+-----------------+----------+---------+------+------+-------------+
1 row in set (0.00 sec)
So 20 rows vs. 249k.
Here's a patch.
Comments
Comment #2
berdirI think the & is wrong here :)
Oh, and ignore the test results, 6.x-1.x tests don't pass right now.
Powered by Dreditor.
Comment #3
catchYou mean 'call time pass by reference is deprecated' ;) grr, can't believe I did that.
Updated patch, back to CNR.
Comment #5
catchIgnoring the bot.
Comment #6
kbahey commentedThanks Nat.
Committed to 6.x
Berdir, does this need to be ported to 7.x?
Comment #7
berdirOnly the definition in hook_schema needs to be updated for new installations. I'll do that myself, no re-roll necessary...
Comment #8
catchThanks!
Comment #9
berdirCommited to 7.x-1.x.
Comment #10
berdirReverted the patch, this turned out to be a duplicate of #704888: For userpoints_txn, need an index on expirydate and expired columns and caused #986814: Error: Cannot redeclare userpoints_update_6012() when running update.php