我们是否应该包括排序列,复合索引的主键(MySQL)

编程入门 行业动态 更新时间:2024-10-28 12:19:28
本文介绍了我们是否应该包括排序列,复合索引的主键(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;

我的问题是:

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

  • 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会将剩余的主列附加到该索引中,但可以使用之前添加的内容,而不必重复它们)

    所以答案:

  • 在InnoDB中是没有必要的,在MyISAM中,如果您实际使用该排序是很好的,如果您不使用它,则添加它只会使该索引更大.
  • 表定义中的列顺序与索引中的顺序是分开的,所以可以
  • 是的,该索引看起来确实不错-但您可以使用解释,则可能会有更好的性能-"覆盖索引",但这要付出一定的代价,因此,除非查询很关键且效果不佳,否则它可能是过大的了.
  • 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.
  • Order of columns in the table definition and order in index are separate things, so it is OK
  • 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)

    本文发布于:2023-10-22 23:26:24,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1519040.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:序列   主键   索引   MySQL

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!