mariadb 在两列表上进行选择计数需要两分钟

mbzjlibv  于 2022-11-08  发布在  其他
关注(0)|答案(1)|浏览(110)

我有一个MariaDB表,它的行数不到100000行,选择计数需要很长时间(几乎2分钟)。
不过,按id从表中选择任何内容只需要4毫秒。
这里的文本字段平均包含5000个字符。
我如何才能加快速度?

MariaDB [companies]> describe company_details;
+---------+------------------+------+-----+---------+-------+
| Field   | Type             | Null | Key | Default | Extra |
+---------+------------------+------+-----+---------+-------+
| id      | int(10) unsigned | NO   | PRI | NULL    |       |
| details | text             | YES  |     | NULL    |       |
+---------+------------------+------+-----+---------+-------+

MariaDB [companies]> explain select count(id) from company_details;
+------+-------------+-----------------+-------+---------------+---------+---------+------+-------+-------------+
| id   | select_type | table           | type  | possible_keys | key     | key_len | ref  | rows  | Extra       |
+------+-------------+-----------------+-------+---------------+---------+---------+------+-------+-------------+
|    1 | SIMPLE      | company_details | index | NULL          | PRIMARY | 4       | NULL | 71267 | Using index |
+------+-------------+-----------------+-------+---------------+---------+---------+------+-------+-------------+

MariaDB [companies]> analyze table company_details;
+---------------------------+---------+----------+----------+
| Table                     | Op      | Msg_type | Msg_text |
+---------------------------+---------+----------+----------+
| companies.company_details | analyze | status   | OK       |
+---------------------------+---------+----------+----------+
1 row in set (0.098 sec)

MariaDB [companies]> select count(id) from company_details;
+-----------+
| count(id) |
+-----------+
|     96544 |
+-----------+
1 row in set (1 min 43.199 sec)

当我试图联接表时,这就成了一个更大的问题。例如,要查找没有关联详细信息的公司的数量:

MariaDB [companies]> SELECT COUNT(*) FROM company c LEFT JOIN company_details cd ON c.id = cd.id WHERE cd.id IS NULL;
+----------+
| count(*) |
+----------+
|    42178 |
+----------+
1 row in set (10 min 28.846 sec)

编辑:在表上运行OPTIMIZE后,选择计数的速度从1分43秒提高到了5秒,连接的速度从10分钟提高到了25秒。

MariaDB [companies]> optimize table company_details;
+---------------------------+----------+----------+-------------------------------------------------------------------+
| Table                     | Op       | Msg_type | Msg_text                                                          |
+---------------------------+----------+----------+-------------------------------------------------------------------+
| companies.company_details | optimize | note     | Table does not support optimize, doing recreate + analyze instead |
| companies.company_details | optimize | status   | OK                                                                |
+---------------------------+----------+----------+-------------------------------------------------------------------+
2 rows in set (11 min 21.195 sec)
66bbxpm5

66bbxpm51#

我认为有OPTIMIZE -重建索引的命令。

OPTIMIZE company_details;

这通常需要一些时间才能完成。更多详细信息:https://mariadb.com/kb/en/optimize-table/

相关问题