使用限制子句连接和排序的表数量进行查询优化

发布于 2024-11-18 06:40:53 字数 1513 浏览 1 评论 0原文

我有一个查询,其中使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉城凉梦凉人心 2024-11-25 06:40:53

MySQL 可以使用键进行排序,而不是在获取数据后对结果进行排序,但前提是满足几个条件。
您可以在此处查看这些条件的列表:http:// /dev.mysql.com/doc/refman/5.0/en/order-by-optimization.html

在您的情况下,我认为多个 JOIN 会阻止快速排序。提到的 MySQL 无法使用索引进行排序的情况之一是:

您正在加入许多表,并且
ORDER BY 中的列并非全部
从第一个非常量表可以看出
用于检索行。 (这是
EXPLAIN 输出中的第一个表
没有 const 连接类型。)

我不确定是否有解决方法。这取决于表结构和实际查询。

要获得更多帮助,请尝试发布有序查询的解释输出。

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:

You are joining many tables, and the
columns in the ORDER BY are not all
from the first nonconstant table that
is used to retrieve rows. (This is the
first table in the EXPLAIN output that
does not have a const join type.)

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.

神也荒唐 2024-11-25 06:40:53

我首先尝试添加复合索引:

Table a
    (col2, col1)

Table b
    (col4, col1)

和简单索引

Table h
    (col5)

I would first try adding compound indices on:

Table a
    (col2, col1)

Table b
    (col4, col1)

and a simple index on

Table h
    (col5)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文