简体   繁体   English

是否应该为WHERE子句中的所有字段添加索引? -MySQL

[英]Should I add an index for all fields in the WHERE clause? - MySQL

In my program I have very few inserts, and any which are run frequently are not needed instantly and therefore have been changed to INSERT DELAYED . 在我的程序中,插入很少,并且不需要经常运行的插入,因此已将其更改为INSERT DELAYED Should I go through my code and see which fields are referenced in the WHERE clause and add an index for each of them? 我是否应该遍历代码,查看WHERE子句中引用了哪些字段,并为每个字段添加索引? If so what type of index do I use? 如果是这样,我应该使用哪种类型的索引? Is it just inserts that are slowed down? 只是插入速度变慢了吗?

Also can I use these indexes on any data type? 我还可以在任何数据类型上使用这些索引吗?

There are two major places where we can consider indexing: columns referenced in the WHERE clause and columns used in JOIN clauses. 我们可以考虑在两个主要位置建立索引:WHERE子句中引用的列和JOIN子句中使用的列。 In short, such columns should be indexed against which you are required to search particular records. 简而言之,应为需要搜索特定记录的这些列建立索引。

  • Only index those columns that are required in WHERE and ORDER BY clauses. 仅索引WHERE和ORDER BY子句中所需的那些列。 Indexing columns in abundance will result in some disadvantages. 索引列过多会导致一些缺点。
  • Use the NOT NULL attribute for those columns in which you consider the indexing, so that NULL values will never be stored. 对于要考虑建立索引的列,请使用NOT NULL属性,以便永远不会存储NULL值。
  • Use the --log-long-format option to log queries that aren't using indexes. 使用--log-long-format选项可以记录未使用索引的查询。 In this way, you can examine this log file and adjust your queries accordingly. 这样,您可以检查此日志文件并相应地调整查询。 Also slow-query log. 也是慢查询日志。
  • The EXPLAIN statement helps you to reveal that how MySQL will execute a query. EXPLAIN语句可帮助您揭示MySQL如何执行查询。 It shows how and in what order tables are joined. 它显示了表的连接方式和顺序。 This can be much useful for determining how to write optimized queries, and whether the columns are needed to be indexed. 这对于确定如何编写优化查询以及是否需要对列进行索引非常有用。

This blog is good. 这个博客很好。

Don't go changing your queries before you see a performance problem - that's premature optimization. 在看到性能问题之前,不要去更改查询-这是过早的优化。 Instead, use MySQL query log to see which queries are taking a long time, and concentrate on improving those queries. 取而代之的是,使用MySQL查询日志来查看哪些查询花费很长时间,并集中精力改进那些查询。

You should always add an index on any field to be used in a WHERE clause (whether for SELECT, UPDATE, or DELETE). 您应该始终在要在WHERE子句中使用的任何字段上添加索引(无论是SELECT,UPDATE还是DELETE)。 The type of index depends on the type of data in the field and whether you need each row to have a unique value. 索引的类型取决于字段中的数据类型以及是否需要每一行都具有唯一值。 Generally the default index type (Hash vs. Btree) is best left to the default settings unless you really know what you are doing. 通常,除非您真的知道自己在做什么,否则最好将默认索​​引类型(哈希与Btree)留给默认设置。

Now whether you use individual indexes (one per field) or compound indexes kind of depends on how the application works and is a more advanced subject, so in general if just getting started just use and index for each field. 现在,您使用单个索引(每个字段一个)还是复合索引的类型取决于应用程序的工作方式,并且是一个更高级的主题,因此,通常,如果刚入门,则只为每个字段使用和索引。 Note that your primary keys automatically have indexes so you don't need to create another index on those. 请注意,您的主键会自动具有索引,因此您无需在这些索引上创建另一个索引。

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

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