mysql索引对单表查询的影响

索引被用来快速找出在一个列上用一特定值的行。没有索引,MySQL不得不首先以第一条记录开始并然后读完整个表直到它找出相关的行。表越大,花费时间越多。
如果表对于查询的列有一个索引,MySQL能快速到达一个位置去搜寻到数据文件的中间,没有必要考虑所有数据。如果一个表有1000 行,这比顺序读取至少快100倍。注意你需要存取几乎所有1000行,它较快的顺序读取,因为此时我们避免磁盘寻道。

例如对下面这样的一个student表:

mysql>SELECT * FROM student;

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAR8AAAFLCAIAAABgKh/7AAAZdElEQVR4nO2dS47qPBCF//0g9fDuJGLERiIGbCQSKyFiJVlK/kFeflT5lVQ5wPl0B33TEB8cH9tx2of/RgCADP/VFgDA1wJ3ASAF3AWAFHAXAFIUuut9v/y7v46Vcoay0oGqdH5WFdxVCFSl87Oq4K5CoCqdn1WV6a6+/fd3cf49ehFlqmVBFVQJcMjY9Xp4cnf/a996ZUEVVImAmWEhUJXOz6qCuwqBqnR+VhWedwEgBdwFgBRwFwBSwF0ASAF3ASAF3AWAFHAXAFLAXQBIAXcBIAXcBYAUcBcAUsBdAEgBdwEgBdwFgBRwFwBSwF0ASAF3gS9nGIZaRee66/UwkgmMg81z8F7bt/+IF2ed+bSIql3rk6nY7WXRuJV0ndzZwhok4NpYgbzheb3cOvbXRWRc+kPcxQB3lZ88pUHruEufLHeF+QZ3ma1hTfxpnt1+d70ef82z727rOYflN8N68PJvrfehu83lXv79Xf7dX1u81pagMDyvyxuvxRXPVTF5cv5T0NVFjF1D1/xz3/56/F0eHXnamM619iyR5NlsMfSnoKuU0sy+OKY5Ks8vzihovvpk0cPzuraZ5na1wjaGrvGyN6q5y+wqpnaz313bSd73tVKsnux9X14zNZqpOiZfTT9PrnMVjkPXlBqMVMudPPQp5tfPzZ1xlzkL6E0TkqeN6lxrj71etshVDPkC5lPTmlPqP7clBKrILG54Gs5x9G8yrAnX8Lz6A2YtdzlTwQNmhvZ8gDvhenzobtvrzfcuP7tn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MREcuSFxKSi+DTZjXcvzZftW7fndtdFou5aRgN/RT7vy1BknuqEa6bW0+TKqxoj2ZbqjOrJIM9wD6cSCTEccFe1gvZwKpEQwwF3VStoD6cSCTEcH+MuAEAqcBcAUsBdAEgBdwEgBdwFgBRwFwBSwF0ASAF3ASAF3AWAFHAXAFLAXQBIAXcBIAXcBYAUcBcAUsBdAEhR6C430l0SzbLSgap0flYV3FUIVKXzs6rgrkKgKp2fVZXpLiOxiI/gOQjNsqAKqgQ4ZOwyv0DpqH9bEIJ8WVAFVSJgZlgIVKXzs6rgrkKgKp2fVYXnXQBIAXcBIAXcBYAUcBcAUsBdAEgBdwEgBdwFgBT/A3lYxVqMxPmkAAAAAElFTkSuQmCC" alt="" />

这样,我们试图对它进行一个特定查询时,就不得不做一个全表的扫描,速度很慢。
例如,我们查找出所有english成绩不及格的学生:

mysql>SELECT name,english FROM student WHERE english<60;

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAJcAAADpCAIAAACm1TKfAAAJgklEQVR4nO2dS46rOhRF73wipVkzQWllIlEaTAQpIwnKSDwU34b5HBsbbMDE7OylatTNo3gHFjagbA7/NDk//75dANkBWkSAFhGgRQRoEQFaRCDZ4ud5+Xu+c5SyhR+vihYzQotp/HhV0Rbbx9/14vzUbcbKWFU8G8fiu56Ue+DP43OmqjLCGTUjtJjGj1fF+0UEaBEBWkSAFhGgRQRoEQFaRIAWEaBFBGgRAVpEgBYRoEUEaBEBWkSAFhGgRQTiLb5rkSgpnqzVvutr9VLyl9Bii6GpfeqkxXUrn5EnFzuHxXd9rV5tc+/iX2Lb1PChyPGp5n6tXk0f9Hu+x9DfmE9Rr1v/h7dGuf/HLdWGVh7eijFOZ8o26/SMRdVUf+6fv+vrpW68q12sM43tFsfs3uc57BrrMPw8+2WMWiPM+DO/G7tKm71873evaqq1Ir3VhlY+txXd8t1BGbDYPsYAYytle1e7WGcyO1gcJw25MZLhc9Xcx+Xl3/a/u2tQr9u6PK6v2uDKA1vRPu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oRZJudAiArSIAC0iQIsI0CICtIgALSJAiwjQIgK0iAAtIkCLCGzPozqZhmEZb+/emP/LulTx7wbD9S4W5U63O/VhWCw9GK53tygyFsKiGrLedgBp+Nzb30/mkleWtwPlB8N1jrEo0sNDBnxc5vO8CM2TTqpWhC5pUP5uMFxnOC9KH74ZdaGVrdkp7zqhxfRMeRs5RzBc7z4WVVP9jUNw7gUigbbSw2Kp59TfDYbr/c+LnqBiJ2bsIb5ksW6nrcbXlbeB8wTD9REWHR9RM6pOfzXO7wbDdZYZ1bnTkLa6i1L3qkc1lXioMb63+mJ5O1F2MFwfc9cvwuOPz/Rewv0r+4I2dl793WC4BsqjFlUeLa6kqPJocSVFlVeuRVIutIgALSJAiwjQIgK0iAAtIkCLCNAiArSIAC0iQIsI0CICtIgALSKQbNFu914KP14VLWaEFtP48aqiLYrg11w+82BYldZ681j0PKdx4M+YbTlDVRnhjJoRWkzjx6vi/SICtIgALSJAiwjQIgK0iAAtIkCLCNAiArSIAC0i8B/KC3DNH8TqqgAAAABJRU5ErkJggg==" alt="" />

其中,WHERE从句不得不匹配每个记录,以检查是否符合条件。对于这个较小的表也许感觉不到太多的影响。但是对于一个较大的表,例如一个非常大的学校,我们可能需要存储成千上万的记录,这样一个检索的所花的时间是十分可观的。
如果,我们为english列创建一个索引:

mysql>ALTER TABLE student ADD INDEX (english) ;

再执行下述查询:

mysql>SELECT name,english FROM user WHERE english<60;

结果为:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAJcAAADqCAIAAAAgQUAxAAAJp0lEQVR4nO2dQY6jOhRFez+Ralg7QRllI1EGbAQpKwnKSrwU9wCCn40NtmMo++oe1aCKJvwHJzbwc3n5p0n7/PvrAkgBaBEBWkSAFhGgRQRoEYF9i+/H5ffxOqGUJFiVhBZLQotpsCpJ2OJ4//25OD/9eF5lrCqe1LH46lflnvhzf7dU1XlwRi0JLabBqiS8X0SAFhGgRQRoEQFaRIAWEaBFBGgRAVpEgBYRoEUEaBEBWk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dXH0wlJZX3JU0Gw/X3Y1EN3a8ZglsNwcPfXpDxnE2ovK9oNBiuC5wXPUHFl7ch+IbFuaVzwnM2ofK+oNlguD7EYqgh+NaMqsNp3aTy8mk3GK7LzKjOnUaoIbh4rf3NG58NjveUeZXBcMMhd/3+huBa3oHIV9kXtLHzKoPhhnbzqFWVR4uZVFUeLWZSVXn1WiTNQIsI0CICtIgALSJAiwjQIgK0iAAtIkCLCNAiArSIAC0ikPcpsbN8iuH+yf/UP+DDBDtM6/nU2h9JPaaYOHI6Tt+c+JAabtfOl0Y5gQMO3HjfTDY7yfGDi4kjq2/4YO2nGrp+EJkifzBcPa9LV/Gpe7j582bH5kUqZ5fyB07kpD24yfGDi4kkr/u7/Cf1vN7fJhkWCoY7LTntP614nDcqmFTkN6jntbtdA9PpKjl+cDGxZPbwN29YNdwer0C+T+px0mPOn07C6g+ba9opdRlS9SbHjy0mlkyLS4x6/sU99G4wfDXC3AG3fE1HynQaKrIgVvrSlxw/s5ggud+nMb9J5dvTzKjrYPiuxc8WkqbTUJEF+dSzlRw/rZgg2d+Kop7X7jl+9m2xGAqG71qclgxJ02moyC9wA+xm7olIjrdncd4xMcmsYvxWMHzXomxDHc8RVzd2B2zU+0VP9F2cFwPB8H2L6Y8whor8EnFS9wbV27d4JGlXpxNMpWpdlUVzEZgALWpdi8XpDJr2/OIELWrNzzQwoEUEaBEBWkSAFhGgRQRoEQFaRIAWEaBFBGgRAVpEgBYRoEUEaBGBfYtLxrAqWJWEFktCi2mwKknYomgKutW792RYlY/Usejp4X/ij0m1tFDVeXBGLQktpsGqJLxfRIAWEaBFBGgRAVpEgBYRoEUEaBEBWkSAFhGgRQRoEYH/yfs0Jl4efpkAAAAASUVORK5CYII=" alt="" />

可以发现,这个结果与未索引english列之前的不同,它是排序的,原因正式如上所述。

上一篇:JavaScript事件监听以及addEventListener参数分析


下一篇:python基础题型一