首页 > 解决方案 > MySQL - 查询优化 - 按来自不同连接表的多个字段排序

问题描述

SQL查询:

SELECT t1.col_A, t2.col_A, t2.col_B
FROM t1
INNER JOIN t2.ID = t1.t2_ID
WHERE t1.active = 1
ORDER BY t2.col_A ASC, t2.col_B ASC, t1.col_A ASC
LIMIT 0,100

表 t1 索引:

表 t2 索引:

在现实生活中,表不共享列名。

这里最重要的是ORDER BY,没有它,查询需要 0.001 秒,根据表的大小需要 9 到 10 秒。

我在确定如何优化此查询时遇到问题。

按要求编辑,添加EXPLAIN输出:

在此处输入图像描述

标签: mysqlsqlsql-order-byquery-optimizationinner-join

解决方案


试试这个,我希望它会有所作为

      ANALYZE TABLE t1;
      ANALYZE TABLE t2;
      create index idx_2 on t2 (col_a asc, col_b asc);
      create index idx_1 on t1(col_a asc);

推荐阅读