使用限制子句连接和排序的表数量进行查询优化
我有一个查询,其中使用 distincct - left join - order by - limit 子句连接多个表。
查询如下所示:-
Select DISTINCT a.col1, b.col2, c.col3, d.col4, e.col5, f.col6, g.col7, h.col8,
i.col9, j.col10
From test_a a
left join test_b b on a.col1 = b.col2
left join test_c c on c.col1 = d.col2
left join test_d d on d.col1 = c.col2
left join test_e e on e.col1 = d.col2
left join test_f f on f.col1 = e.col2
left join test_g g on g.col1 = f.col2
left join test_h h on h.col1 = a.col1
left join test_i i on i.col1 = f.col2
left join test_j j on j.col1 = i.col2
Where a.col2 = 'Y'
and c.col4 = 1
Order by h.col5 desc
limit 50;
在条件中使用的所有列都有索引。这个查询的解释输出给出了结果集,我可以看到它正确使用了所有索引,并且从所有表扫描的总行数是 18000。
我在这个查询中想知道的是。如果我在没有 order by 子句的情况下运行它,它会在几秒钟内运行。例如:
Select DISTINCT a.col1, b.col2, c.col3, d.col4, e.col5, f.col6, g.col7, h.col8,
i.col9, j.col10
From test_a a
left join test_b b on a.col1 = b.col2
left join test_c c on c.col1 = d.col2
left join test_d d on d.col1 = c.col2
left join test_e e on e.col1 = d.col2
left join test_f f on f.col1 = e.col2
left join test_g g on g.col1 = f.col2
left join test_h h on h.col1 = a.col1
left join test_i i on i.col1 = f.col2
left join test_j j on j.col1 = i.col2
Where a.col2 = 'Y'
and c.col4 = 1
limit 50;
如果我使用 order by 子句运行它,那么执行需要 30-40 秒。
我尝试使用 mysql:- USE INDEX FOR ORDER BY (idx_h_col5) 提供的索引提示功能,但在执行此查询时遇到语法错误。错误消息显示语法不正确,
我在 order by 子句中使用的列上有一个复合索引。我还尝试在此列上创建单个索引,但没有任何效果。
I have a query which has multiple tables joined using distincct - left join - order by - limit clause.
The query looks like this:-
Select DISTINCT a.col1, b.col2, c.col3, d.col4, e.col5, f.col6, g.col7, h.col8,
i.col9, j.col10
From test_a a
left join test_b b on a.col1 = b.col2
left join test_c c on c.col1 = d.col2
left join test_d d on d.col1 = c.col2
left join test_e e on e.col1 = d.col2
left join test_f f on f.col1 = e.col2
left join test_g g on g.col1 = f.col2
left join test_h h on h.col1 = a.col1
left join test_i i on i.col1 = f.col2
left join test_j j on j.col1 = i.col2
Where a.col2 = 'Y'
and c.col4 = 1
Order by h.col5 desc
limit 50;
All the column used the in coditions has index on it. And explan output of this query gives resultset where I can see it uses all the index properly and total rows it scanned from all the tables is 18000.
What I am wondering in this query is. It runs within seconds if I run it without order by clause. Something like:
Select DISTINCT a.col1, b.col2, c.col3, d.col4, e.col5, f.col6, g.col7, h.col8,
i.col9, j.col10
From test_a a
left join test_b b on a.col1 = b.col2
left join test_c c on c.col1 = d.col2
left join test_d d on d.col1 = c.col2
left join test_e e on e.col1 = d.col2
left join test_f f on f.col1 = e.col2
left join test_g g on g.col1 = f.col2
left join test_h h on h.col1 = a.col1
left join test_i i on i.col1 = f.col2
left join test_j j on j.col1 = i.col2
Where a.col2 = 'Y'
and c.col4 = 1
limit 50;
And if I run it with order by clause then it takes 30-40 seconds to execute.
I tried using the index hint functionality provided by mysql:- USE INDEX FOR ORDER BY (idx_h_col5)
, but I am getting syntax error while executing this query. The error message says incorrect syntax near
I have one composite index on the column used in order by clause. I also tried creating a single index on this column but nothing really works.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
MySQL 可以使用键进行排序,而不是在获取数据后对结果进行排序,但前提是满足几个条件。
您可以在此处查看这些条件的列表:http:// /dev.mysql.com/doc/refman/5.0/en/order-by-optimization.html
在您的情况下,我认为多个 JOIN 会阻止快速排序。提到的 MySQL 无法使用索引进行排序的情况之一是:
我不确定是否有解决方法。这取决于表结构和实际查询。
要获得更多帮助,请尝试发布有序查询的解释输出。
MySQL can use keys for sorting instead of sorting the result after fetching the data, but only if several conditions are met.
You can see a list of these conditions here: http://dev.mysql.com/doc/refman/5.0/en/order-by-optimization.html
In your case, I think that the multiple JOINs prevent the quick sorting. One of the mentioned cases in which MySQL can't use an index for sorting is:
I am not sure if there is a way around it. It depends on the tables structure and the actual query.
To get more help, try posting the explain output of the ordered query.
我首先尝试添加复合索引:
和简单索引
I would first try adding compound indices on:
and a simple index on