MySQL - 为什么不索引每个字段?

2021-12-21 00:00:00 indexing sql mysql

最近我了解了索引的神奇之处,性能有了显着的提高.然而,就我所知,我似乎无法找到这个问题的答案.

Recently I've learned the wonder of indexes, and performance has improved dramatically. However, with all I've learned, I can't seem to find the answer to this question.

索引很棒,但为什么有人不能只索引所有字段以使表非常快?我确信有一个很好的理由不这样做,但是 30 个字段的表中的三个字段怎么样?30 个字段中有 10 个?应该在哪里划线,为什么?

Indexes are great, but why couldn't someone just index all fields to make the table incredibly fast? I'm sure there's a good reason to not do this, but how about three fields in a thirty-field table? 10 in a 30 field? Where should one draw the line, and why?

推荐答案

索引占用内存 (RAM) 空间;索引过多或过大,数据库将不得不将它们与磁盘交换.它们还增加了插入和删除时间(必须为插入/删除/更新的每条数据更新每个索引).

Indexes take up space in memory (RAM); Too many or too large of indexes and the DB is going to have to be swapping them to and from the disk. They also increase insert and delete time (each index must be updated for every piece of data inserted/deleted/updated).

你没有无限的记忆.使所有索引都适合 RAM = 好.

You don't have infinite memory. Making it so all indexes fit in RAM = good.

你没有无限的时间.仅索引需要索引的列可以最大限度地减少插入/删除/更新性能损失.

You don't have infinite time. Indexing only the columns you need indexed minimizes the insert/delete/update performance hit.

相关文章