按日期降序排序时,“使用临时”会减慢查询速度

2024-03-29

我有一个日志条目表,以及大约 100 个可能的日志代码的描述表:

CREATE TABLE `log_entries` (
  `logentry_id` int(11) NOT NULL AUTO_INCREMENT,
  `date` datetime NOT NULL,
  `partner_id` smallint(4) NOT NULL,
  `log_code` smallint(4) NOT NULL,
  PRIMARY KEY (`logentry_id`),
  KEY `IX_code` (`log_code`),
  KEY `IX_partner_code` (`partner_id`,`log_code`)
) ENGINE=MyISAM ;

CREATE TABLE IF NOT EXISTS `log_codes` (
  `log_code` smallint(4) NOT NULL DEFAULT '0',
  `log_desc` varchar(255) DEFAULT NULL,
  `category_overview` tinyint(1) NOT NULL DEFAULT '0',
  `category_error` tinyint(1) NOT NULL DEFAULT '0',
  PRIMARY KEY (`log_code`),
  KEY `IX_overview_code` (`category_overview`,`log_code`),
  KEY `IX_error_code` (`category_error`,`log_code`)
) ENGINE=MyISAM ;

以下查询(匹配 20k 行中的 10k 行)在 0.0034 秒内执行(使用LIMIT 0,20):

SELECT log_entries.date, log_codes.log_desc FROM log_entries 
INNER JOIN log_codes ON log_codes.log_code = log_entries.log_code 
WHERE log_entries.partner_id = 1 AND log_codes.category_overview = 1;

但是添加时ORDER BY log_entries.logentry_id DESC,这当然是必要的,它减慢到0.6秒。可能是因为 log_codes 表上使用了“使用临时”?删除索引实际上使查询执行得更快,但仍然很慢(0.3 秒)。

EXPLAIN 不带 ORDER BY 的查询输出:



+----+-------------+-------------+------+----------------------------+------------------+---------+--------------------------+------+-------------+
| id | select_type | table       | type | possible_keys              | key              | key_len | ref                      | rows | Extra       |
+----+-------------+-------------+------+----------------------------+------------------+---------+--------------------------+------+-------------+
|  1 | SIMPLE      | log_codes   | ref  | PRIMARY,IX_overview_code   | IX_overview_code | 1       | const                    |   56 |             |
|  1 | SIMPLE      | log_entries | ref  | IX_code,IX_partner_code    | IX_partner_code  | 7       | const,log_codes.log_code |   25 | Using where |
+----+-------------+-------------+------+----------------------------+------------------+---------+--------------------------+------+-------------+
  

并包括 ORDER BY:



+----+-------------+-------------+------+----------------------------+------------------+---------+--------------------------+------+---------------------------------+
| id | select_type | table       | type | possible_keys              | key              | key_len | ref                      | rows | Extra                           |
+----+-------------+-------------+------+----------------------------+------------------+---------+--------------------------+------+---------------------------------+
|  1 | SIMPLE      | log_codes   | ref  | PRIMARY,IX_overview_code   | IX_overview_code | 1       | const                    |   56 | Using temporary; Using filesort |
|  1 | SIMPLE      | log_entries | ref  | IX_code,IX_partner_code    | IX_partner_code  | 7       | const,log_codes.log_code |   25 | Using where                     |
+----+-------------+-------------+------+----------------------------+------------------+---------+--------------------------+------+---------------------------------+
  

关于如何让这个查询执行得更快有什么提示吗?我不明白为什么需要“使用临时”,因为应该在获取和排序适当的日志条目之前选择日志代码?

更新@Eugen Rieck:



SELECT log_entries.date, lc.log_desc FROM log_entries INNER JOIN (SELECT log_desc, log_code FROM log_codes WHERE category_overview = 1) AS lc ON lc.log_code = log_entries.log_code WHERE log_entries.partner_id = 1 ORDER BY log_entries.logentry_id;
+----+-------------+-------------+------+-------------------------+------------------+---------+-------------------+------+---------------------------------+
| id | select_type | table       | type | possible_keys           | key              | key_len | ref               | rows | Extra                           |
+----+-------------+-------------+------+-------------------------+------------------+---------+-------------------+------+---------------------------------+
|  1 | PRIMARY     | <derived2>  | ALL  | NULL                    | NULL             | NULL    | NULL              |   57 | Using temporary; Using filesort |
|  1 | PRIMARY     | log_entries | ref  | IX_code,IX_partner_code | IX_partner_code  | 7       | const,lc.log_code |   25 | Using where                     |
|  2 | DERIVED     | log_codes   | ref  | IX_overview_code        | IX_overview_code | 1       |                   |   56 |                                 |
+----+-------------+-------------+------+-------------------------+------------------+---------+-------------------+------+---------------------------------+
  

更新@RolandoMySQLDBA:

使用我的原始索引,ORDER BY date DESC:



SELECT log_entries.date, log_codes.log_desc FROM (SELECT log_code,date FROM log_entries WHERE partner_id = 1) log_entries INNER JOIN (SELECT log_code,log_desc FROM log_codes WHERE category_overview = 1) log_codes USING (log_code) ORDER BY log_entries.date DESC;
+----+-------------+-------------+------+------------------+------------------+---------+------+-------+---------------------------------+
| id | select_type | table       | type | possible_keys    | key              | key_len | ref  | rows  | Extra                           |
+----+-------------+-------------+------+------------------+------------------+---------+------+-------+---------------------------------+
|  1 | PRIMARY     | <derived3>  | ALL  | NULL             | NULL             | NULL    | NULL |    57 | Using temporary; Using filesort |
|  1 | PRIMARY     | <derived2>  | ALL  | NULL             | NULL             | NULL    | NULL | 21937 | Using where; Using join buffer  |
|  3 | DERIVED     | log_codes   | ref  | IX_overview_code | IX_overview_code | 1       |      |    56 |                                 |
|  2 | DERIVED     | log_entries | ALL  | IX_partner_code  | NULL             | NULL    | NULL | 22787 | Using where                     |
+----+-------------+-------------+------+------------------+------------------+---------+------+-------+---------------------------------+
  

使用您的索引,无需排序:



SELECT log_entries.date, log_codes.log_desc FROM (SELECT log_code,date FROM log_entries WHERE partner_id = 1) log_entries INNER JOIN (SELECT log_code,log_desc FROM log_codes WHERE category_overview = 1) log_codes USING (log_code);
+----+-------------+-------------+-------+-----------------------+-----------------------+---------+------+-------+--------------------------------+
| id | select_type | table       | type  | possible_keys         | key                   | key_len | ref  | rows  | Extra                          |
+----+-------------+-------------+-------+-----------------------+-----------------------+---------+------+-------+--------------------------------+
|  1 | PRIMARY     | <derived3>  | ALL   | NULL                  | NULL                  | NULL    | NULL |    57 |                                |
|  1 | PRIMARY     | <derived2>  | ALL   | NULL                  | NULL                  | NULL    | NULL | 21937 | Using where; Using join buffer |
|  3 | DERIVED     | log_codes   | index | IX_overview_code_desc | IX_overview_code_desc | 771     | NULL |    80 | Using where; Using index       |
|  2 | DERIVED     | log_entries | index | IX_partner_code_date  | IX_partner_code_date  | 15      | NULL | 22787 | Using where; Using index       |
+----+-------------+-------------+-------+-----------------------+-----------------------+---------+------+-------+--------------------------------+
  

使用您的索引,ORDER BY date DESC:



SELECT log_entries.date, log_codes.log_desc FROM (SELECT log_code,date FROM log_entries WHERE partner_id = 1) log_entries INNER JOIN (SELECT log_code,log_desc FROM log_codes WHERE category_overview = 1) log_codes USING (log_code) ORDER BY log_entries.date DESC;
+----+-------------+-------------+-------+-----------------------+-----------------------+---------+------+-------+---------------------------------+
| id | select_type | table       | type  | possible_keys         | key                   | key_len | ref  | rows  | Extra                           |
+----+-------------+-------------+-------+-----------------------+-----------------------+---------+------+-------+---------------------------------+
|  1 | PRIMARY     | <derived3>  | ALL   | NULL                  | NULL                  | NULL    | NULL |    57 | Using temporary; Using filesort |
|  1 | PRIMARY     | <derived2>  | ALL   | NULL                  | NULL                  | NULL    | NULL | 21937 | Using where; Using join buffer  |
|  3 | DERIVED     | log_codes   | index | IX_overview_code_desc | IX_overview_code_desc | 771     | NULL |    80 | Using where; Using index        |
|  2 | DERIVED     | log_entries | index | IX_partner_code_date  | IX_partner_code_date  | 15      | NULL | 22787 | Using where; Using index        |
+----+-------------+-------------+-------+-----------------------+-----------------------+---------+------+-------+---------------------------------+
  

更新@Joe Stefanelli:



SELECT log_entries.date, log_codes.log_desc FROM log_entries INNER JOIN log_codes ON log_codes.log_code = log_entries.log_code WHERE log_entries.partner_id = 1 AND log_codes.category_overview = 1 ORDER BY date DESC;
+----+-------------+-------------+------+--------------------------+-----------------+---------+--------------------------+------+----------------------------------------------+
| id | select_type | table       | type | possible_keys            | key             | key_len | ref                      | rows | Extra                                        |
+----+-------------+-------------+------+--------------------------+-----------------+---------+--------------------------+------+----------------------------------------------+
|  1 | SIMPLE      | log_codes   | ALL  | PRIMARY,IX_code_overview | NULL            | NULL    | NULL                     |   80 | Using where; Using temporary; Using filesort |
|  1 | SIMPLE      | log_entries | ref  | IX_code,IX_code_partner  | IX_code_partner | 7       | log_codes.log_code,const |   25 | Using where                                  |
+----+-------------+-------------+------+--------------------------+-----------------+---------+--------------------------+------+----------------------------------------------+
  

我认为,这里的大多数问题以及类似的问题都来自于对 MySQL(和其他数据库)如何使用索引进行排序的误解。答案是:MySQL不使用索引进行排序,它只是可以按照索引的顺序或相反的方向读取数据。如果您碰巧希望按照当前使用的索引的顺序对数据进行排序 - 您很幸运,否则结果将被排序(因此 EXPLAIN 中的文件排序)

也就是说,整个结果的顺序主要取决于哪个表是连接中的第一个表。如果您查看 EXPLAIN,您会发现连接从“log_codes”表开始(因为它小得多)。

基本上,您需要的是“log_entries”上的复合索引(partner_id,date),“log_codes”的覆盖复合索引(log_code,category_overview,log_desc),将“INNER JOIN”更改为“STRAIGHT_JOIN”以强制连接顺序,并按“日期”DESC 排序(幸运的是,该索引也将被覆盖)。

UPD1:抱歉,我输错了第一个表的索引:它应该是(partner_id, log_code, date).

但我仍然很难理解当我尝试对另一个表中的列进行排序时,为什么 MySQL 选择在 log_codes 表上“使用临时”(和 100 倍的查询时间)?

MySQL可以直接输出数据,只要你同意它获取数据的顺序,或者将数据放在临时表中,然后排序并输出。当您在连接中按任何非第一个表中的字段排序时,MySQL 必须对数据进行排序(不仅仅是按索引的顺序输出),并且为了对数据进行排序,它需要一个临时表。

但当我进一步了解数据集时,它会变慢(LIMIT 50000,25 为 6 秒)。你知道为什么吗?

要输出第 50000,25 行,MySQL 无论如何都需要获取前 50000 行并跳过它们。由于我错过了索引中的一列,MySQL 不仅扫描了索引,而且还为每个项目进行了额外的磁盘查找log_code价值。使用覆盖索引应该会更快,因为所有数据都可以从索引中获取。

UPD2:尝试强制索引:

SELECT log_entries.date, log_codes.log_desc
FROM log_entries FORCE INDEX (IX_partner_code_date)
STRAIGHT_JOIN log_codes
  ON log_codes.log_code = log_entries.log_code
WHERE log_entries.partner_id = 1
  AND log_codes.category_overview = 1
ORDER BY log_entries.date DESC;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

按日期降序排序时,“使用临时”会减慢查询速度 的相关文章

  • PDO在mysql性能中的作用

    最近我在浏览一篇博客 注意到有关在mysql中使用PDO的一些要点 它改变了我对PDO的看法 要点是 本机准备好的语句无法利用查询缓存 从而导致性能降低 本机准备好的语句无法执行某些类型的查询 例如 SHOW TABLES 本机准备好的语句
  • Over() 函数没有覆盖表中的所有行

    我正在使用 MySQL 练习 SQL 并在 SQL 中遇到了奇怪的行为 假设我有一张这样的表 Delivery table delivery id customer id order date customer pref delivery
  • 我需要异常排序 mysql 结果

    我正在尝试从当前日期开始对结果进行升序排序 这就是我现在使用的 SELECT FROM friends JOIN bdays ON bdays user friends friendname WHERE username userid OR
  • 按“计数(列不为空)”排序

    我正在寻找一种方法 通过值不为空的列的计数来对 MySQL 结果进行排序 所以 id 1 1 0 1 1 4 id 0 1 1 1 0 3 id 0 0 0 1 1 2 id 1 0 0 0 0 1 在上面的例子中 我忽略了 ID 列 但实
  • 致命错误:无法在functions.php第25行中重新声明session_start()

    当我尝试让登录部分正常工作时遇到问题 我不断遇到的问题是 致命错误 无法在 public html login functions php 第 25 行重新声明 session start
  • Android:Json 无法从 mysql 数据库检索任何文件,它是空的

    我是 android 新手 我正在使用 mysql 数据库 其中我链接 php 文件进行连接 工作正常 但我的代码没有显示任何内容 它只显示背景色黑色 而不是显示数据库中的数据 public class HomeFragment exten
  • 标签系统:Toxi 解决方案问题

    我对标签数据库模式的 Toxi 解决方案感到有点困惑 我正在开发一个系统 用户可以向该系统提交项目 并且这些项目可以具有与其关联的标签 在阅读了 tagchemas 后 我发现 Toxi 解决方案最适合我的需求 但是 我不完全确定我的计划是
  • CodeIgniter 中使用 Active Record 的查询中的 DATE_FORMAT 不起作用

    编码员 我在这里遇到一个小问题 找不到解决方案 我正在使用 CI 的 Active Record 构建查询 这是查询的代码 this gt db gt select u id AS user id u email p display nam
  • 如何在查询中获取 MySQL 状态

    是否可以在 MySQL 查询中使用服务器状态变量 我可以从 显示状态 中看到各种指标 但如何计算派生值 例如查询缓存命中率 show global status like Qcache inserts show global status
  • mysql 部分索引、反向索引

    我有一张包含单词列表的表格 字 VARCHAR 16 我需要创建一个反向索引 IE 单词 apple 将索引为 elppa 单词 banana 将索引为 ananab 依此类推 另外 是否可以索引单词的一部分 例如 跳过第一个 最后一个或
  • gcc 不会编译和运行 MySQL C 库

    include
  • 使用 mysql_real_escape_string() 时出现访问被拒绝错误

    我试图在数据进入我的数据库之前转义一些数据 但我不断收到此错误 Warning mysql real escape string Access denied for user 现在 这通常表明我尚未连接到数据库 它还声明 使用密码 NO 我
  • sqlalchemy:每次提交都需要回滚吗?

    我正在为我的网络应用程序使用 SQLAlchemy 我见过很多这样的代码 try session commit except session rollback 我想知道是否每个人都有必要commit 手术 如果是 那么为什么它不属于comm
  • postgresql中插入语句中的加密密码抛出错误(需要添加显式类型转换)

    刚接触 postgresql 这可能很简单 但我不明白 我在 mysql 中有这个 insert into APP USERS VALUES 1 admin adminemailid System Administrator SysAdmi
  • 无法打开 mysql-workbench

    我开始使用 mysql 但无法打开 mysql workbench 当我尝试通过 ubuntu 命令行打开它时 出现以下错误 usr lib mysql workbench mysql workbench bin 符号查找错误 usr li
  • 获取标签包含 GROUP_CONCAT 字段的所有文章

    我有一张桌子articles 其他tags 第三个称为article tags 我想生成一个页面 其中列出特定标签的所有文章 我的查询如下所示 SELECT headline GROUP CONCAT tags tag name AS al
  • Symfony2 - 访问被拒绝(用户未经过完全身份验证)

    我正在使用 Symfony2 开发一个网站 直到今天 登录没有问题 但现在登录时我没有正确验证 Symfony 分析器将我列为logged in as anon而不是我登录的用户 我还被重定向回登录页面而不是目标路径 登录过程由传统的登录表
  • 缺少 /var/lib/mysql/mysql.sock 文件

    我正在尝试访问 mysql 当我运行 mysql 命令时 我得到以下信息 root ip 10 229 65 166 tpdatabase 1 8 0 28356 mysql 错误 2002 HY000 无法连接到 通过socket本地My
  • mysql连接3个表

    如何连接三个具有一个公共列 id 的mysql表 例如 从表1中选择a b 从表2中选择c d 从表3中选择e f 其中id x 谢谢 SELECT t1 a t1 b t2 c t2 d t3 e t3 f FROM table1 t1
  • 基于 MySQL 中的另一列创建计算列

    我的表中有 2 列 varchar 8 and an int 我想要auto increment the int column当我这样做时 我想将值复制到varchar 8 column 但用 0 填充它直到它达到 8 个字符长 因此例如

随机推荐