MySQL索引如何工作? [英] How do MySQL indexes work?
问题描述
我对MySQL索引的工作方式非常感兴趣,更具体地说,他们如何在不扫描整个表的情况下返回所请求的数据?
I am really interested in how MySQL indexes work, more specifically, how can they return the data requested without scanning the entire table?
这是偏离主题的,我我知道,但如果有人可以向我详细解释,我会非常,非常感谢。
It's off-topic, I know, but if there is someone who could explain this to me in detail, I would be very, very thankful.
推荐答案
基本上表上的索引就像书中的索引(名称来自的位置):
Basically an index on a table works like an index in a book (that's where the name came from):
假设你有一本关于数据库的书,你想找一些关于存储的信息。如果没有索引(假设没有其他帮助,例如目录),您必须逐个浏览页面,直到找到主题(那是全表扫描
)。
另一方面,索引有一个关键字列表,所以你可以参考索引,看看第113-120,231和354页提到了存储
然后你可以直接翻到那些页面而不进行搜索(这是一个带索引的搜索,有点快)。
Let's say you have a book about databases and you want to find some information about, say, storage. Without an index (assuming no other aid, such as a table of contents) you'd have to go through the pages one by one, until you found the topic (that's a full table scan
).
On the other hand, an index has a list of keywords, so you'd consult the index and see that storage
is mentioned on pages 113-120,231 and 354. Then you could flip to those pages directly, without searching (that's a search with an index, somewhat faster).
当然,索引的用处有多大,取决于关于很多事情 - 一些例子,使用上面的明喻:
Of course, how useful the index will be, depends on many things - a few examples, using the simile above:
- 如果你有一本关于数据库的书并且索引了数据库这个词,你会看到它在1-59,61-290和292到400页面上提到过。在这种情况下,索引没有多大帮助,逐个浏览页面可能会更快(在数据库中,这是糟糕的选择性)。
- 对于一本10页的书,制作一个索引是没有意义的,因为你可能会得到一本10页的书,前面是一个5 -page索引,这只是愚蠢的 - 只扫描10页并完成它。
- 索引也需要有用 - 索引通常没有意义例如每页字母L的频率。
这篇关于MySQL索引如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!