mysql - 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 索引:
- ID
- 可乐
- 积极的
- t2_ID
表 t2 索引:
- ID
- 可乐
- col_B
在现实生活中,表不共享列名。
这里最重要的是ORDER BY
,没有它,查询需要 0.001 秒,根据表的大小需要 9 到 10 秒。
我在确定如何优化此查询时遇到问题。
按要求编辑,添加EXPLAIN
输出:
解决方案
试试这个,我希望它会有所作为
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);
推荐阅读
- c++ - std::cout 不打印浮点数作为例外
- c# - WPF 在鼠标悬停时更改 ListBoxItem 的背景
- swift - 当 CV contentInset 更新时(带视频),尽管 .isHidden = true,仍会显示 collectionViewCell 子视图
- excel - 跟踪工作表上的更改,复制活动单元格更改列标题和 NewColumnValue
- python - 如何在python中将时间序列数据移动一个月
- kubernetes - Ingress 不会路由到 EKS 上的服务
- javascript - 使用 R 的 bsplus、Shiny 和 JavaScript 停止轮播自动播放
- python - ImportError:无法从部分初始化的模块“src.windows”导入名称“RegisterWindow”
- asynchronous - 在 tokio::select 中生锈 lazy_static 和 tokio::sync::mpsc::channel
- javascript - content.select() 不适用于
element