| 订阅 | 在线投稿
分享
 
 
 

由浅入深讲解MySQL数据库索引的选择性

来源:互联网  宽屏版  评论
2008-06-01 03:22:20

本文为【由浅入深讲解MySQL数据库索引的选择性】的汉字拼音对照版显示拼音

zaiMySQLzhongduiyusuoyinde使shiyongbingshiyizhidoucaiyongzhengquedejueding

jiandanbiaodeshili

CREATE TABLE `r2` (

ID` int(11) DEFAULT NULL,

ID1` int(11) DEFAULT NULL,

CNAME` varchar(32) DEFAULT NULL,

KEY `ID1` (`ID1`)

) ENGINE=MyISAM DEFAULT CHARSET=latin1

SELECT COUNT(*) FROM r2;

250001 (V1)

SELECT COUNT(*) FROM r2 WHERE ID1=1;

83036 (V2)

(execution time = 110 ms)

(ID1=1)tiaojianchaxunsuoyindexuanzexingshi V2/V1 = 0.3321 huo 33.21%

yibanlaishuo(lirushu SQL Tuning),ruguoxuanzexingchaoguo 20% namequanbiaosaomiaobi使shiyongsuoyinxingnenggengyou

wozhidaoOracleyizhishizaixuanzexingchaoguo25%shihuixuanzequanbiaosaomiao

erMySQLne:

mysql> EXPLAIN SELECT COUNT(SUBNAME) FROM r2 WHERE ID1=1;

+----+-------------+-------+------+---------------+-----

| id | select_type | TABLE | type | possible_keys | KEY | key_len | ref | rows | Extra |

+----+-------------+-------+------+---------------+-----

| 1 | SIMPLE | t2 | ref | ID1 | ID1 | 5 | const | 81371 | USING WHERE |

+----+-------------+-------+------+---------------+-----

zhejiushiMySQLjianghui使shiyongsuoyinlaiwanchengzhegechaxun

rangwomenlaiduibisuoyinchaxunhequanbiaosaomiaodezhixingshijian:

SELECT COUNT(SUBNAME) FROM t2 WHERE ID1=1 - 410 ms

SELECT COUNT(SUBNAME) FROM t2 IGNORE INDEX (ID1) WHERE ID1=1 - 200 ms

runisuokandaoquanbiaosaomiaoyaokuai2bei

shenkaogengteshudelizixuanzexing ~95%:

SELECT cnt2 / cnt1 FROM (SELECT count(*) cnt1 FROM r2) d1, (SELECT count(*) cnt2 FROM r2 WHERE ID1=1) d2;

0.9492 = 94.92%;

shuomingMySQLjianghuiyongsuoyinlaiwanchengchaxun

zhixingshijian:

SELECT COUNT(SUBNAME) FROM t2 WHERE ID1=1 - 1200 ms

SELECT COUNT(SUBNAME) FROM t2 IGNORE INDEX (ID1) WHERE ID1=1 - 260 ms

zheciquanbiaosaomiaoyaokuai4.6bei

weishenmeMySQLxuanzesuoyin访fangwenchaxun?

MySQLmeiyoujisuansuoyindexuanzexingzhishiyuceluojiIOcaozuodeshuliangbingqiewomendelizizhongjiandeluojiIOshuliangsuoyin访fangwenyaoshaoyuquanbiaosaomiao

zuihouwomendechujielunduiyusuoyinyaoxiaoxin使shiyongyinweitamenbingbunengbangzhusuoyoudechaxun

原文
在MySQL中,对于索引的使用并是一直都采用正确的决定。 简单表的示例: CREATE TABLE `r2` ( ID` int(11) DEFAULT NULL, ID1` int(11) DEFAULT NULL, CNAME` varchar(32) DEFAULT NULL, KEY `ID1` (`ID1`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 SELECT COUNT(*) FROM r2; 250001 (V1) SELECT COUNT(*) FROM r2 WHERE ID1=1; 83036 (V2) (execution time = 110 ms) (ID1=1)条件查询索引的选择性是 V2/V1 = 0.3321 或 33.21% 一般来说(例如书 “SQL Tuning“),如果选择性超过 20% 那么全表扫描比使用索引性能更优。 我知道Oracle一直是在选择性超过25%时会选择全表扫描。 而MySQL呢: mysql> EXPLAIN SELECT COUNT(SUBNAME) FROM r2 WHERE ID1=1; +----+-------------+-------+------+---------------+----- | id | select_type | TABLE | type | possible_keys | KEY | key_len | ref | rows | Extra | +----+-------------+-------+------+---------------+----- | 1 | SIMPLE | t2 | ref | ID1 | ID1 | 5 | const | 81371 | USING WHERE | +----+-------------+-------+------+---------------+----- 这就是MySQL将会使用索引来完成这个查询。 让我们来对比索引查询和全表扫描的执行时间: SELECT COUNT(SUBNAME) FROM t2 WHERE ID1=1 - 410 ms SELECT COUNT(SUBNAME) FROM t2 IGNORE INDEX (ID1) WHERE ID1=1 - 200 ms 如你所看到全表扫描要快2倍。 参考更特殊的例子:选择性 ~95%: SELECT cnt2 / cnt1 FROM (SELECT count(*) cnt1 FROM r2) d1, (SELECT count(*) cnt2 FROM r2 WHERE ID1=1) d2; 0.9492 = 94.92%; 说明MySQL将会用索引来完成查询。 执行时间: SELECT COUNT(SUBNAME) FROM t2 WHERE ID1=1 - 1200 ms SELECT COUNT(SUBNAME) FROM t2 IGNORE INDEX (ID1) WHERE ID1=1 - 260 ms 这次全表扫描要快4.6倍。 为什么MySQL选择索引访问查询? MySQL没有计算索引的选择性,只是预测逻辑IO操作的数量,并且我们的例子中间的逻辑IO数量,索引访问要少于全表扫描。 最后我们得出结论,对于索引要小心使用,因为它们并不能帮助所有的查询。
󰈣󰈤
 
 
 
>>返回首页<<
 
 热帖排行
 
 
王朝网络微信公众号
微信扫码关注本站公众号wangchaonetcn
 
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
©2005- 王朝网络 版权所有