点击关注公众号,Java干货及时送达
来自:计算机奇闻逸事(ID:gh_2d7137908da3)
在今天这个卷得离谱的 IT/互联网职场环境中,“什么是覆盖索引?”似乎是一道烂大街的八股面试题,恐怕连应届生都能倒背如流。(实际中用不用得上再单说,我一个 SELECT*
……)
但在当年,著名数据库 SQLite 的作者 D. Richard Hipp 一开始竟然也不知道这个概念。只不过,Richard Hipp 不同凡人之处就在于,在从 MySQL 的同(yuan1)行(jia1)那里了解了这个概念的几个小时后,就在会议结束乘飞机回家的途中为 SQLite 增加了覆盖索引的功能。
SQLite的作者D. Richard Hipp(美国 1961-04-09~)
Richard Hipp 在一次采访(https://corecursive.com/066-sqlite-with-richard-hipp)中提到了这段趣闻。我把这段采访音频搬了过来。以下是这段音频的文本和译文。
注:Adam是这次采访的主持人
Richard: ...我之前从来没有听说过覆盖索引。有一次我受邀飞去参加一个在德国某处举行的 PHP 会议。PHP 那时已经集成了 SQLite,所以大会想让我在会上讲讲 SQLite。于是我飞过去参加了那个会议。巧的是 MySQL 的创始人之一 David Axmark 也是与会者。
David 在他的演讲中解释了 MySQL 是如何实现覆盖索引的。我当时就感到这点子真绝。覆盖索引是指一个索引,该索引包含多个列,如果对索引中的前几列进行查询,那么你想要的结果就位于索引中的其余列中。此时,数据库引擎仅需使用索引即可,而没有必要再引用原始表了,这就能提升查询速度。
Adam: 这听起来就像一个键值存储的数据库,只不过是在索引上的。
Richard: 对对,没错。于是,在回家的路上,在达美航空的飞机上,那班航班刚好人不多,我那一排都没人。我撸起袖子甩开膀子,打开了笔记本,在大西洋上空为 SQLite 实现了覆盖索引。
MySQL AB的创始人之一David Axmark(瑞典 1962-05-28~)
音频的文本如下
Richard: ... I had never heard of, for example, a covering index. I was invited to fly to a conference, it was a PHP conference in Germany somewhere, because PHP had integrated SQLite into the project. They wanted me to talk there, so I went over and I was at the conference, but David Axmark was at that conference, as well. He’s one of the original MySQL people.
David was giving a talk and he explained about how MySQL did covering indexes. I thought, “Wow, that’s a really clever idea.” A covering index is when you have an index and it has multiple columns in the index and you’re doing a query on just the first couple of columns in the index and the answer you want is in the remaining columns in the index. When that happens, the database engine can use just the index. It never has to refer to the original table, and that makes things go faster if it only has to look up one thing.
Adam: It becomes like a key value store, but just on the index.
Richard: Right, right, so, on the fly home, on a Delta Airlines flight, it was not a crowded flight. I had the whole row. I spread out. I opened my laptop and I implemented covering indexes for SQLite mid-Atlantic.