有一个25万行的表test,有2个字符串字段:a和b,其中a带普通索引.a的值有五种"test1","test2","test3","test4","test5",各种分别5万行,随机散列在不同的行而不是连续的.执行语句:
explain select * from test where a = "test1" and b like "%测试%";
分析出的rows为50582,只比50000多了一点.
再执行语句:
explain select * from test where b like "%没有匹配的%";
分析出的rows为250000,等于总行数,说明进行了全表扫描.这样看起来似乎后面一个语句明显是更慢的,但是执行结果却是:
select * from test where a = "test1" and b like "%测试%";
0 rows in set (0.26 secs)select * from test where b like "%测试%";
0 rows in set (0.15 secs)后面一个全表扫描的语句反而更快,这是怎么回事?