我们是否应该包括排序列,复合索引的主键(MySQL) [英] Should we include sort column, primary key on composite index (MySQL)

查看:100
本文介绍了我们是否应该包括排序列,复合索引的主键(MySQL)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

表(简体):

+---------------------------------------------------------------------+
| id (Primary AI) |  user_id  |  status  |  type  |   data   |   ip   |
+=====================================================================+
|        1        |     3     |    0     |   abc  |   a-s-d  |    -   |
+---------------------------------------------------------------------+
|        2        |     1     |    0     |   ooo  |   z-z-z  |    -   |
+---------------------------------------------------------------------+
|        3        |     3     |    0     |   ooo  |   f-f-f  |    -   |
+---------------------------------------------------------------------+
|        4        |     2     |    0     |   abc  |   h-h-h  |    -   |
+---------------------------------------------------------------------+
|        5        |     1     |    0     |   abc  |   a-s-d  |    -   |
+---------------------------------------------------------------------+

更多信息:

ID是此表的主键(自动递增)

ID is the Primary Key of this table (Auto Increment)

请注意,我已将ID (Primary Key)用作新索引的第三个Seq_in_index

Please Note that i have used ID (Primary Key) as the 3rd Seq_in_index on new index

我已经为上述表格创建了一个综合索引

I have created a Composite Index for the mentioned table

CREATE INDEX userid_type_id ON table (user_id, type, id);

此索引中的id仅用于排序.

样本查询

SELECT id, status, data, ip 
                      FROM `table`
                      WHERE user_id=3 AND type='abc' 
                      ORDER BY id DESC;

我的问题是:

  1. 在复合索引中插入ID是一种好的(性能)做法吗?因为它仅用于ORDER BY

  1. Is it a good (performance) practice to insert ID in the composite index? as it is only used for ORDER BY

在表的第一列时,可以使用ID(主键)作为索引中的第三个序列

Is it okay to use ID (primary key) as the third sequence in index while it is first column on the table

我是否根据样本查询正确选择了索引?

Did i choose my Index correctly based on my sample query?

我使用InnoDB

I use InnoDB

推荐答案

答案取决于您使用的引擎:

Answer depends on engine you use:

  • MyISAM-将ID添加到索引可以并且可能会有所帮助
  • InnoDB-主键已经是每个二级索引的一部分,因为innodb将按主键排序的行存储在BTREE中,并且索引需要主键指向实际行-在这种情况下,如果它在索引中位于最后,则添加冗余(但这样做不会将其添加两次,因此它不会使情况变得更糟).在某些情况下,您可能希望将其添加为非最后一个,或者您拥有多列主数据库,并以不同的顺序向索引添加了一些列-这样做没有问题,innodb会将剩余的主列附加到该索引中,但可以使用之前添加的内容,而不必重复它们)

所以答案:

  1. 在InnoDB中是没有必要的,在MyISAM中,如果您实际使用该排序是很好的,如果您不使用它,则添加它只会使该索引更大.
  2. 表定义中的列顺序与索引中的顺序是分开的,所以可以
  3. 是的,该索引看起来确实不错-但您可以使用解释,则可能会有更好的性能-"
  1. In InnoDB it is unnecessary, In MyISAM it is good in case you actually use that sorting, if you dont use it, adding it only makes that index bigger.
  2. Order of columns in the table definition and order in index are separate things, so it is OK
  3. Yes, that index seems really good - but you can check yourself using EXPLAIN, there is possibility of even better performance - "covering index", but that comes with a cost so unless the query is critical and underperforming, it is probably overkill.

这篇关于我们是否应该包括排序列,复合索引的主键(MySQL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆