【问题标题】:What is the optimal index for this query?此查询的最佳索引是什么?
【发布时间】:2020-08-16 22:14:58
【问题描述】:

我在 MYSQL 8 中有下表:

create table session
(
    ID                           bigint unsigned auto_increment
        primary key,
    session_id                   varchar(255)                                         null,
    username                     varchar(255)                                         null,
    session_status               varchar(255)                                         null,
    session_time                 int                                                  null,    
    time_created                 int                                                  null,
    time_last_updated            int                                                  null,
    time_ended                   int                                                  null,
    date_created                 date                                                 null,
);

我正在执行以下语句:

select * from session where username = VALUE and session_id = VALUE order by time_created desc

加快查询速度的表的最佳索引是多少?

EXPLAIN 查询告诉我我有两个潜在的索引,它们是:

create index username_3
    on session (username, time_created);

create index username_session_id_time_created_desc
    on session (username, session_id, time_created desc);

我原以为会选择索引“username_session_id_time_created_desc”,但 EXPLAIN 语句表示选择了索引“username_3”。

编辑*

SHOW CREATE TABLE 会话结果:

CREATE TABLE `session` (
  `ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `session_id` varchar(255) COLLATE utf8_bin DEFAULT NULL,
  `username` varchar(255) COLLATE utf8_bin DEFAULT NULL,
  `session_status` varchar(255) COLLATE utf8_bin DEFAULT NULL,
  `session_time` int(11) DEFAULT NULL,
  `time_created` int(11) DEFAULT NULL,
  `time_last_updated` int(11) DEFAULT NULL,
  `time_ended` int(11) DEFAULT NULL,
  `date_created` date DEFAULT NULL,
  PRIMARY KEY (`ID`),
  KEY `username_3` (`username`,`time_created`),
  KEY `username_session_id_time_created_desc` (`username`,`session_id`,`time_created`)
) ENGINE=InnoDB AUTO_INCREMENT=76149265 DEFAULT CHARSET=utf8 COLLATE=utf8_bin

EXPLAIN 语句的结果:

select_type: SIMPLE
type: ref
possible_keys: username_3,username_session_id_time_created_desc
key: username_3
key_len: 768
ref: const
rows: 1
Extra: Using where

【问题讨论】:

  • 你能显示show create table session和explain select * from session where username = VALUE and session_id = VALUE order by time_created desc的完整输出吗?
  • @ysth 信息添加为编辑。
  • 试试EXPLAIN 以及它将返回超过 1 行的查询。

标签: mysql sql indexing


【解决方案1】:

对于这个查询:

select *
from session
where username = %s and session_id = %s
order by time_created desc

最佳索引是(username, session_id, time_created desc)。前两列可以是任意顺序。

【讨论】:

  • 那么 EXPLAIN 语句是否有理由选择索引“username_3”而不是索引“username_session_id_time_created_desc”,而第二个正是您指定的?
  • @vddox 。 . .如果你只有一排,没关系。在表变大之前,索引并不重要。
  • EXPLAIN 输出中的 rows: 1 是关键——只有一行满足 WHERE 条件,所以 ORDER BY 是多余的,它不会打扰那个索引.
【解决方案2】:

首先我以为你写错字了

create index username_session_id_time_created_desc on session (username, session_id, time_created desc);

但你的创建表显示

KEY `username_session_id_time_created_desc` (`username`,`session_id`,`time_created`)

而不是

KEY `username_session_id_time_created_desc` (`username`,`session_id`,`time_created` DESC)

但是,我现在认为它在 MySQL 8.0 中使用 username_3,因为 backward_index_scan (I did not read it all)

你怎么知道:你的简单EXPLAIN中没有filesort,所以它一定是优化器可以使用username_3索引来对你的结果集进行排序。 (如果您从任何索引中删除 time_created,您将看到Using filesort。如果您没有使用 MySQL 8.0,可能也可以在您的版本中使用username_3 索引进行排序。)

Fiddle in 5.7 显示 "key": "username_session_id_time_created_desc", :好吧 sometimes,但并非总是如此......它可能取决于索引长度(字段长度)。

Whereas in MySQL 8.0 显示"key": "username_3", 和"backward_index_scan": true,

With only the 3 column index 显示查询成本较低,为什么要选择其他索引?

我的猜测是 2 列索引要短得多,而且由于可以进行反向索引扫描,因此排序成本低,优化器仍然更喜欢 IO 少,计算量少。

【讨论】:

    猜你喜欢
    • 2015-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-14
    • 1970-01-01
    相关资源
    最近更新 更多