简体   繁体   English

MySQL:非主键列上的索引

[英]MySQL: index on non-primary key column

是否可以使用除主键以外的其他列作为索引,这样做有什么缺点吗?

Absolutely. 绝对。 This helps with SELECT ... WHERE where the condition isn't your primary key. 这对于条件不是您的主键的SELECT ... WHERE有帮助。 However, be careful, don't index too much - only index what you need. 但是,请注意,不要索引过多-仅索引您需要的内容。

Many of my tables have secondary indexes besides the primary key. 我的许多表除主键外还具有辅助索引。 You can create multiple indexes per table, but in MySQL only one index is used per table in a given query. 您可以为每个表创建多个索引,但是在MySQL中,给定查询中的每个表仅使用一个索引。

Which indexes should you create? 您应该创建哪些索引? This depends on your queries. 这取决于您的查询。 See the presentation I did this week, MENTOR Your Indexes . 请参阅我本周所做的演示,“指导您的索引”

Adding more indexes adds incrementally to overhead on insert/update/delete. 添加更多索引会逐渐增加插入/更新/删除的开销。 When you change data, the RDBMS must keep indexes in sync. 更改数据时,RDBMS必须使索引保持同步。 But it's nearly always true that the right indexes give your select queries so much benefit that they more than justify the overhead of maintaining them. 但是,正确的索引为您的选择查询带来了很多好处,几乎比保证维护它们的开销更合理,这几乎总是正确的。


is there much of a performance hit with using multiple indexes? 使用多个索引会大大降低性能吗?

In the book "The Art of SQL" the author tested this and found that there's a greater performance hit as you add multiple indexes. 在《 SQL的艺术》一书中,作者对此进行了测试,发现添加多个索引会带来更大的性能影响。 But interestingly, it doesn't go up linearly. 但是有趣的是,它并没有线性上升。 The additional overhead of the second, third, etc. indexes falls off, in a roughly logarithmic curve. 第二,第三等索引的额外开销呈大致对数曲线下降。 That is, two indexes is not twice as expensive as one index. 也就是说,两个索引的价格不比一个索引高两倍。 Four indexes is not twice as expensive as two indexes. 四个索引并不比两个索引贵两倍。

You can go for alternative how ever there are 您可以选择其他方式

Advantages of MySQL Indexes Generally speaking, MySQL indexing into database gives you three advantages: MySQL索引的优点一般来说,将MySQL索引到数据库可为您带来三个优点:

  • Query optimization: Indexes make search queries much faster. 查询优化:索引使搜索查询更快。
  • Uniqueness: Indexes like primary key index and unique index help to avoid duplicate row data. 唯一性:主键索引和唯一索引等索引有助于避免重复的行数据。
  • Text searching: Full-text indexes in MySQL version 3.23.23, users have the opportunity to optimize searching against even large amounts of text located in any field indexed as such. 文本搜索:在MySQL 3.23.23版中,全文索引使用户有机会针对位于这样索引的任何字段中的大量文本进行优化搜索。

Disadvantages of MySQL indexes When an index is created on the column(s), MySQL also creates a separate file that is sorted, and contains only the field(s) you're interested in sorting on. MySQL索引的缺点在列上创建索引时,MySQL还会创建一个单独的文件进行排序,并且仅包含您感兴趣的字段。

  • Firstly, the indexes take up disk space. 首先,索引占用磁盘空间。 Usually the space usage isn't significant, but because of creating index on every column in every possible combination, the index file would grow much more quickly than the data file. 通常,空间使用并不重要,但是由于在每种可能的组合中的每一列上都创建了索引,因此索引文件的增长将比数据文件快得多。 In the case when a table is of large table size, the index file could reach the operating system's maximum file size. 如果表的大小很大,则索引文件可能会达到操作系统的最大文件大小。

  • Secondly, the indexes slow down the speed of writing queries, such as INSERT, UPDATE and DELETE . 其次,索引减慢了诸如INSERT,UPDATE和DELETE之类的查询编写速度。 Because MySQL has to internally maintain the “pointers” to the inserted rows in the actual data file, so there is a performance price to pay in case of above said writing queries because every time a record is changed, the indexes must be updated. 因为MySQL必须在内部维护实际数据文件中插入行的“指针”,所以在上述写查询的情况下,必须付出性能代价,因为每次记录更改时,索引都必须更新。 However, you may be able to write your queries in such a way that do not cause the very noticeable performance degradation. 但是,您可以以不会引起明显的性能下降的方式编写查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM