mysql在索引之后仍然很慢

tvz2xvvm  于 2021-06-24  发布在  Mysql
关注(0)|答案(1)|浏览(271)

我有这张table:

+-------------+---------------------+------+-----+---------+----------------+
| Field       | Type                | Null | Key | Default | Extra          |
+-------------+---------------------+------+-----+---------+----------------+
| id          | bigint(20) unsigned | NO   | PRI | NULL    | auto_increment |
| client_name | varchar(32)         | YES  | MUL | NULL    |                |
| timestamp   | varchar(32)         | YES  |     | NULL    |                |
| pair_name   | varchar(8)          | YES  |     | NULL    |                |
| bid_price   | decimal(20,8)       | YES  |     | NULL    |                |
| ask_price   | decimal(20,8)       | YES  |     | NULL    |                |
+-------------+---------------------+------+-----+---------+----------------+

它有超过70万行。由于我尝试选择某些内容时速度很慢,因此我决定添加索引:

ALTER TABLE price_trading ADD INDEX price_trading_index (client_name, timestamp, pair_name);

+---------------+------------+---------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table         | Non_unique | Key_name            | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------------+------------+---------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| price_trading |          0 | PRIMARY             |            1 | id          | A         |      715216 |     NULL | NULL   |      | BTREE      |         |          |
| price_trading |          1 | price_trading_index |            1 | client_name | A         |           4 |     NULL | NULL   | YES  | BTREE      |         |          |
| price_trading |          1 | price_trading_index |            2 | timestamp   | A         |       41897 |     NULL | NULL   | YES  | BTREE      |         |          |
| price_trading |          1 | price_trading_index |            3 | pair_name   | A         |      740698 |     NULL | NULL   | YES  | BTREE      |         |          |
+---------------+------------+---------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

当我运行此语法时:

SELECT * FROM price_trading WHERE timestamp = 'lm83-1524620255198';

结果是:

18 rows in set (23.43 sec)

如何加快查询速度?为什么索引方法不能使它更快?
谢谢您。

wlsrxk51

wlsrxk511#

您使用的是复合索引,复合索引从左到右在您的示例中适用于1)客户机名称和时间戳以及配对名称2)客户机名称和时间戳3)客户机名称
因此,根据您的需求,为timestamp列创建单独的索引。

相关问题