SQL: MySQL speed optimisation
Would be nice to test with other versions of MySQL too.
Comparison before/after:
mysql> EXPLAIN SELECT e.id, e.guid, e.title, e.author,
UNCOMPRESS(content_bin) AS content, e.link, e.date, e.is_read,
e.is_favorite, e.id_feed, e.tags FROM `freshrss_alex_entry` e INNER JOIN
(SELECT e1.id FROM `freshrss_alex_entry` e1 INNER JOIN
`freshrss_alex_feed` f ON e1.id_feed=f.id WHERE f.priority > 0 AND
(e1.id >= 1381615200000000 OR e1.is_read=0 OR e1.is_favorite=1 OR
(f.keep_history <> 0)) ORDER BY e1.id DESC LIMIT 31) e2 ON e2.id=e.id
ORDER BY e.id DESC;
+----+-------------+------------+--------+-------------------------------------+----------+---------+---------------+------+----------------------------------------------+
| id | select_type | table | type | possible_keys
| key | key_len | ref | rows | Extra
|
+----+-------------+------------+--------+-------------------------------------+----------+---------+---------------+------+----------------------------------------------+
| 1 | PRIMARY | <derived2> | ALL | NULL
| NULL | NULL | NULL | 31 | Using temporary; Using
filesort |
| 1 | PRIMARY | e | eq_ref | PRIMARY
| PRIMARY | 8 | e2.id | 1 |
|
| 2 | DERIVED | f | range | PRIMARY,priority,keep_history
| priority | 1 | NULL | 137 | Using where; Using
temporary; Using filesort |
| 2 | DERIVED | e1 | ref |
PRIMARY,id_feed,is_favorite,is_read | id_feed | 2 | freshrss.f.id
| 452 | Using where |
+----+-------------+------------+--------+-------------------------------------+----------+---------+---------------+------+----------------------------------------------+
4 rows in set (3.50 sec)
mysql> EXPLAIN SELECT e.id, e.guid, e.title, e.author,
UNCOMPRESS(content_bin) AS content, e.link, e.date, e.is_read,
e.is_favorite, e.id_feed, e.tags FROM `freshrss_alex_entry` e INNER JOIN
(SELECT e1.id FROM `freshrss_alex_entry` e1 INNER JOIN
`freshrss_alex_feed` f ON e1.id_feed=f.id WHERE f.priority > 0 AND e1.id
<=9000000000000000 AND (e1.id >= 1381615200000000 OR e1.is_read=0 OR
e1.is_favorite=1 OR (f.keep_history <> 0)) ORDER BY e1.id DESC LIMIT 31)
e2 ON e2.id=e.id ORDER BY e.id DESC;
+----+-------------+------------+--------+-------------------------------------+---------+---------+---------------------+-------+---------------------------------+
| id | select_type | table | type | possible_keys
| key | key_len | ref | rows | Extra
|
+----+-------------+------------+--------+-------------------------------------+---------+---------+---------------------+-------+---------------------------------+
| 1 | PRIMARY | <derived2> | ALL | NULL
| NULL | NULL | NULL | 31 | Using temporary;
Using filesort |
| 1 | PRIMARY | e | eq_ref | PRIMARY
| PRIMARY | 8 | e2.id | 1 |
|
| 2 | DERIVED | e1 | range |
PRIMARY,id_feed,is_favorite,is_read | PRIMARY | 8 | NULL
| 70084 | Using where |
| 2 | DERIVED | f | eq_ref | PRIMARY,priority,keep_history
| PRIMARY | 2 | freshrss.e1.id_feed | 1 | Using where
|
+----+-------------+------------+--------+-------------------------------------+---------+---------+---------------------+-------+---------------------------------+
4 rows in set (0.01 sec)