What is an index? 索引是什么?
An index is a pre-sorted copy stored next to the table. 索引是一份排好序的副本,单独存在表的旁边。
It's like…就像……
The index at the back of a book: sorted entries, each with a page number. You look up the entry, then turn straight to the page. 书后面的索引页:词条按拼音排好,旁边写着页码。先查到页码,再直接翻到那一页。
Without an index, how does it search? 没有索引,数据库怎么找?
Without an index, every query is a full table scan. 没有索引,每次查询都是一次全表扫描。
With an index, how does it find the row? 有了索引,它怎么找?
It never flips the table itself — it flips the sorted copy. 数据库不翻原表,先翻这份排好序的副本。
Open the index, not the table 翻开索引,不翻原表
Halve, halve again — one entry left 一次丢一半,剩下一格
Take the row number, fetch the row 拿着行号,去取那一行
An index takes space too 索引也要占地方
An index is stored for real: a 3GB table carries about 300MB — one tenth of the table. Every insert writes twice, so writing slows down a little. 索引自己也占硬盘:3GB 的表,索引约 300MB,是表的十分之一。每存一条新数据,表和索引都要各写一次,写入会慢一点。