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)
pull/535/head
Alexandre Alapetite 10 years ago
parent ab9d656135
commit 6c83264f83
  1. 3
      app/Models/EntryDAO.php

@ -352,6 +352,9 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo {
default:
throw new FreshRSS_EntriesGetter_Exception('Bad order in Entry->listByType: [' . $order . ']!');
}
if ($firstId === '' && parent::$sharedDbType === 'mysql') {
$firstId = '9000000000' . '000000'; //MySQL optimization. Tested on MySQL 5.5 with 150k articles
}
if ($firstId !== '') {
$where .= 'AND e1.id ' . ($order === 'DESC' ? '<=' : '>=') . $firstId . ' ';
}

Loading…
Cancel
Save