mysql联合索引分析测试

本文通过实验展示了在不同数据量下,使用联合索引查询性能的显著提升,特别是对于小数据集,联合索引效果尤为明显。同时,文章还探讨了LIMIT语句在查询中的应用及其对性能的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

小数据量(2W)条,走联合索引大概快10倍左右。

#联合索引
#和查询的顺序无关!MYSQL会自动重新排列
小数据量,效果明显。
#联合索引在单个查询和联合查询时速度比较如下:
#单个查询结果:2692条,用时0.117s
SELECT * FROM vlc_caomei_state 
WHERE 
city = '济南市' 
AND 
id < 1500000 
order by region 
#联合查询结果:2692条,用时0.017s
SELECT * FROM vlc_caomei_state 
WHERE 
city = '济南市' 
AND 
region='山东省' 
AND 
id < 1500000 
order by region 



大数据量查询,效果不明显,速度基本相同
#单个查询结果: 12w条,用时26 s
SELECT * FROM vlc_caomei_state 
WHERE 
city = '济南市' 
order by region 
#联合查询结果:12w 条,用时21 s
SELECT * FROM vlc_caomei_state 
WHERE 
city = '济南市' 
AND 
region='山东省' 
order by region 

#limit效果

mysql> explain SELECT * FROM vlc_caomei_state
    -> WHERE
    -> city = '济南市'
    -> order by region
    -> limit 0,10;
+----+-------------+------------------+-------+---------------+---------+---------+------+------+-------------+
| id | select_type | table            | type  | possible_keys | key     | key_len | ref  | rows | Extra       |
+----+-------------+------------------+-------+---------------+---------+---------+------+------+-------------+
|  1 | SIMPLE      | vlc_caomei_state | index | NULL          | regions | 306     | NULL |   10 | Using where |
+----+-------------+------------------+-------+---------------+---------+---------+------+------+-------------+
1 row in set (0.00 sec)

mysql> explain SELECT * FROM vlc_caomei_state
    -> WHERE
    -> city = '济南市'
    -> AND
    -> region='山东省'
    -> order by region
    -> limit 0,10;
+----+-------------+------------------+------+---------------+---------+---------+-------------+--------+-----------------------+
| id | select_type | table            | type | possible_keys | key     | key_len | ref         | rows   | Extra                 |
+----+-------------+------------------+------+---------------+---------+---------+-------------+--------+-----------------------+
|  1 | SIMPLE      | vlc_caomei_state | ref  | regions       | regions | 306     | const,const | 263458 | Using index condition |
+----+-------------+------------------+------+---------------+---------+---------+-------------+--------+-----------------------+
1 row in set (0.07 sec)

mysql> explain SELECT * FROM vlc_caomei_state
    -> WHERE
    -> city = '济南市'
    -> order by region ;
+----+-------------+------------------+------+---------------+------+---------+------+---------+-----------------------------+
| id | select_type | table            | type | possible_keys | key  | key_len | ref  | rows    | Extra                       |
+----+-------------+------------------+------+---------------+------+---------+------+---------+-----------------------------+
|  1 | SIMPLE      | vlc_caomei_state | ALL  | NULL          | NULL | NULL    | NULL | 3918061 | Using where; Using filesort |
+----+-------------+------------------+------+---------------+------+---------+------+---------+-----------------------------+
1 row in set (0.00 sec)

mysql> explain SELECT * FROM vlc_caomei_state
    -> WHERE
    -> city = '济南市'
    -> AND
    -> region='山东省'
    -> order by region  ;
+----+-------------+------------------+------+---------------+---------+---------+-------------+--------+-----------------------+
| id | select_type | table            | type | possible_keys | key     | key_len | ref         | rows   | Extra                 |
+----+-------------+------------------+------+---------------+---------+---------+-------------+--------+-----------------------+
|  1 | SIMPLE      | vlc_caomei_state | ref  | regions       | regions | 306     | const,const | 263458 | Using index condition |
+----+-------------+------------------+------+---------------+---------+---------+-------------+--------+-----------------------+
1 row in set (0.42 sec)




总结,大数据量,索引不明显。

http://bbs.youkuaiyun.com/topics/390747126



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值