mysql - 如何加速复杂的ORDER BY?

标签 mysql sql

我有 4 个表:translationKeystranslationSuggestionstranslationCommentstranslationSuggestionActivities。每个都包含一个created列。

我的查询(简化)如下所示:

SELECT 
a.created, 
c.created, 
s.created
FROM translationKeys k
JOIN translationSuggestions s ON k.id = s.translationKeyId
LEFT JOIN translationComments c ON c.translationSuggestionId = s.id
LEFT JOIN translationSuggestionActivities a ON a.translationSuggestionId = s.id

WHERE k.gameId = 11

ORDER BY a.created DESC, c.created DESC, s.created DESC
LIMIT 20

我的执行计划如下所示(对困惑的索引名称表示歉意): Execution Plan

执行时间约为 400 毫秒。如果我删除 ORDER BY 创建的 DESC ,执行时间会立即降至 10 毫秒。我知道这是因为排序发生在临时表中。

我尝试为每个表设置索引,包括 created 列和所有引用的列,但没有区别。

我的问题是:是否有可能提高这种排序的性能,还是我试图完成一些不可能的事情?

最佳答案

由于 ORDER BY 列是由不同表和列合并而成,因此您无法使用任何索引来修复此问题。

但是您只是选择 20 行。为什么不在代码中简单地对结果进行排序呢?

关于mysql - 如何加速复杂的ORDER BY?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59856242/

相关文章:

mysql - 优化大数据的 'having count (distinct ) ' 查询

php - 如何使用 PHP 打破 MySQL 查询的束缚?

sql - HH :MM:SS:Msec to HH:MM:SS in stored procedure

mysql - SQL: child 鞋码排序列表

sql - 我怎么知道在flft sqflite中是否存在行?

php - 大量数据和 PHP 错误

sql - 需要一个sql查询

PHP Codeigniter 数据库助手类 "No tables used"

sql - 生成存储在 MySQL 中的类别面包屑

php - mySQL 查询,数据作为 php 变量?