使用索引时加速 COUNT(*)

gil*_*ilm 5 mysql performance index count mysql-5.5 query-performance

我有一个与此类似的表(简化):

CREATE TABLE books (
    id INT AUTO_INCREMENT,
    category INT NOT NULL,
    PRIMARY KEY (id),
    KEY (category)
);
Run Code Online (Sandbox Code Playgroud)

这张桌子已经结束了10,000,000 rows,大约在12 categories。所以每个类别的平均值为833,333 books.

查询计数时:

SELECT COUNT(*) FROM books WHERE category=1;
Run Code Online (Sandbox Code Playgroud)

即使它在查询时使用索引,这也需要很长时间才能完成(几秒钟)。您将如何优化它?

之前我每次插入书籍时都会增加一个数字(插入一个涉及类别->书籍数量的表。)但是我们的代码很复杂,很多地方插入或删除书籍。我知道可以用 解决这个问题EVENTS,但我在问也许我错过了一个 MySQL 功能。

aku*_*sky 4

category由于索引基数较低,查询会很慢。有 12 个类别,因此平均查询将读取索引的 1/12 部分。您无法改进此查询。

您原来的方法可以提高整体性能。只需book_count在 INSERT 和 DELETE 事件上创建触发器,而不是手动更新。

更新:证明查询将部分读取索引category

mysql> select count(*) from books;
+----------+
| count(*) |
+----------+
|     1000 |
+----------+
1 row in set (0.00 sec)

mysql> select category, count(*) from books group by 1;
+----------+----------+
| category | count(*) |
+----------+----------+
|        0 |       50 |
|        1 |       77 |
|        2 |       88 |
|        3 |       84 |
|        4 |      102 |
|        5 |       79 |
|        6 |       79 |
|        7 |       73 |
|        8 |       84 |
|        9 |       76 |
|       10 |       87 |
|       11 |       83 |
|       12 |       38 |
+----------+----------+
13 rows in set (0.01 sec)

mysql> flush status;
Query OK, 0 rows affected (0.00 sec)

mysql> select count(*) from books where category = 6;
+----------+
| count(*) |
+----------+
|       79 |
+----------+
1 row in set (0.00 sec)

mysql> show status like 'Hand%';
+----------------------------+-------+
| Variable_name              | Value |
+----------------------------+-------+
| Handler_commit             | 1     |
| Handler_delete             | 0     |
| Handler_discover           | 0     |
| Handler_external_lock      | 2     |
| Handler_mrr_init           | 0     |
| Handler_prepare            | 0     |
| Handler_read_first         | 0     |
| Handler_read_key           | 1     |
| Handler_read_last          | 0     |
| Handler_read_next          | 79    |
| Handler_read_prev          | 0     |
| Handler_read_rnd           | 0     |
| Handler_read_rnd_next      | 0     |
| Handler_rollback           | 0     |
| Handler_savepoint          | 0     |
| Handler_savepoint_rollback | 0     |
| Handler_update             | 0     |
| Handler_write              | 0     |
+----------------------------+-------+
18 rows in set (0.01 sec)
Run Code Online (Sandbox Code Playgroud)