我有一个表来存储大约35k个项目每15分钟2周内的价格变化情况。大致上,这就相当于在表中有大约3500万行数据。我试图执行最简单的查询:
SELECT buy_price, sell_price, created_at FROM price_archive WHERE item_id = X
首次运行该查询需要大约4-7秒才能返回~1300行(每个项目)。对于数据库来说,这似乎非常慢和琐碎,尤其是考虑到在item_id
列上有一个索引。
该表每15分钟插入35k行,并且每天都会运行一个任务来删除<(2周-1)之前的项目(以防止表太大)。我怀疑这会使表碎片化,但这种碎片化会导致查询性能如此差吗?如果是,那么在created_at
上进行分区以删除旧数据是否会使其更好?
1306 rows in set (8.32 sec)
mysql> explain select * from price_archives where item_id = 743;
+----+-------------+----------------+------+---------------------------------+---------------------------------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------------+------+---------------------------------+---------------------------------+---------+-------+------+-------------+
| 1 | SIMPLE | price_archives | ref | index_price_archives_on_item_id | index_price_archives_on_item_id | 5 | const | 1305 | Using where |
+----+-------------+----------------+------+---------------------------------+---------------------------------+---------+-------+------+-------------+