遇见的问题:30W查询速度慢
mysql版本:5.7
内存:1G
CPU:1核
mysql的一些配置:
innodb_buffer_pool_size:134217728 ( 128M)
key_buffer_size:16777216 ( 16M)
sort_buffer_size:720896(704K)
tmp_table_size:16M 默认为16M 调到64-256最挂
max_connections:2532
查询的SQL语句:
select count(*) as aggregate from `dtk_products` where `is_display` = 1 and `Cid` = 1 and `Quan_timestamp` > 1546411307 and `Quan_surplus` > 0
数据量:近30W
无条件统计:耗时542ms

未创建索引
使用以上SQL查询(4个字段条件):执行耗时14.6s


创建4个字段的单独索引
is_display Cid Quan_timestamp Quan_surplus

执行SQL统计:耗时12.5s

创建联合索引
执行SQL统计:耗时25.9s


通过图7 和图4的 索引情况对比,不难发现,字段Quan_timestamp 和Quan_surplus的索引基数(Cardinality)差别很大。
结论:索引基数越高,查询效率越高。联合索引的索引基数比较大
PS:
耗时:148ms

数据量:308553

Explain SQL:结果如下 type=range ;key=4个字段的联合索引

该表字段共37个,包含数据类型:int archer decimal timestamp
网友评论