To tune queries for 要优化InnoDB
tables, create an appropriate set of indexes on each table. InnoDB
表的查询,请在每个表上创建一组适当的索引。See Section 8.3.1, “How MySQL Uses Indexes” for details. 有关详细信息,请参阅第8.3.1节,“MySQL如何使用索引”。Follow these guidelines for 请遵循以下InnoDB
indexes:InnoDB
索引指南:
Because each 因为每个InnoDB
table has a primary key (whether you request one or not), specify a set of primary key columns for each table, columns that are used in the most important and time-critical queries.InnoDB
表都有一个主键(无论您是否请求主键),所以为每个表指定一组主键列,这些列用于最重要和时间关键的查询。
Do not specify too many or too long columns in the primary key, because these column values are duplicated in each secondary index. When an index contains unnecessary data, the I/O to read this data and memory to cache it reduce the performance and scalability of the server.不要在主键中指定太多或太长的列,因为这些列值在每个辅助索引中都是重复的。当索引包含不必要的数据时,读取该数据的I/O和缓存该数据的内存会降低服务器的性能和可扩展性。
Do not create a separate secondary index for each column, because each query can only make use of one index. 不要为每列创建单独的辅助索引,因为每个查询只能使用一个索引。Indexes on rarely tested columns or columns with only a few different values might not be helpful for any queries. 很少测试的列或只有几个不同值的列上的索引可能对任何查询都没有帮助。If you have many queries for the same table, testing different combinations of columns, try to create a small number of concatenated indexes rather than a large number of single-column indexes. 如果对同一个表有多个查询,测试不同的列组合,请尝试创建少量的串联索引,而不是大量的单列索引。If an index contains all the columns needed for the result set (known as a covering index), the query might be able to avoid reading the table data at all.如果索引包含结果集所需的所有列(称为覆盖索引),则查询可能完全可以避免读取表数据。
If an indexed column cannot contain any 如果索引列不能包含任何NULL
values, declare it as NOT NULL
when you create the table. NULL
值,请在创建表时将其声明为NOT NULL
。The optimizer can better determine which index is most effective to use for a query, when it knows whether each column contains 当优化器知道每列是否包含NULL
values.NULL
值时,它可以更好地确定哪个索引对查询最有效。
You can optimize single-query transactions for 您可以使用第8.5.3节,“优化InnoDB
tables, using the technique in Section 8.5.3, “Optimizing InnoDB Read-Only Transactions”.InnoDB
只读事务”中的技术来优化InnoDB
表的单个查询事务。