选择查询比查看快 2-3 倍

发布于 2024-12-18 02:38:36 字数 1068 浏览 1 评论 0原文

此查询单独运行:

SELECT 
    -- lots of columns 
FROM 
    table1 t1
    LEFT JOIN table2 t2
        ON t2.[userid] = t1.[userid]
    LEFT JOIN table3 t3
        ON t1.[orderid] = t3.[orderid]
    LEFT JOIN table4 t4
        ON t4.[orderitemlicenseid] = t3.[orderitemlicenseid]
    LEFT JOIN table5 t5
        ON t1.[orderid] = t5.[orderid]
    LEFT JOIN table6 t6
        ON t5.[transactionid] = t6.[transactionid]
    LEFT JOIN table7 t7
        ON t7.[transactionid] = t5.[transactionid]
    LEFT JOIN table8 t8
        ON t8.[voucherid] = t7.[voucherid]
    LEFT JOIN table9 t9
        ON t8.[voucherid] = t9.[voucherid]
    LEFT JOIN table10 t10
        ON t10.[vouchergroupid] = t9.[vouchergroupid]
        AND t10.[territoryid] = t2.[territoryid]
    LEFT JOIN table11 t11
        ON t11.[voucherid] = t8.[voucherid]
    LEFT JOIN table12 t12
        ON t12.[orderid] = t1.[orderid]
GROUP BY 
    t5.[transactionid]

大约需要 2.5 秒才能完成。当我将其保存到视图并将其运行为:

SELECT * FROM viewName;

需要 7 秒才能完成。

这是什么原因,怎样才能让浏览速度更快?

This query run alone:

SELECT 
    -- lots of columns 
FROM 
    table1 t1
    LEFT JOIN table2 t2
        ON t2.[userid] = t1.[userid]
    LEFT JOIN table3 t3
        ON t1.[orderid] = t3.[orderid]
    LEFT JOIN table4 t4
        ON t4.[orderitemlicenseid] = t3.[orderitemlicenseid]
    LEFT JOIN table5 t5
        ON t1.[orderid] = t5.[orderid]
    LEFT JOIN table6 t6
        ON t5.[transactionid] = t6.[transactionid]
    LEFT JOIN table7 t7
        ON t7.[transactionid] = t5.[transactionid]
    LEFT JOIN table8 t8
        ON t8.[voucherid] = t7.[voucherid]
    LEFT JOIN table9 t9
        ON t8.[voucherid] = t9.[voucherid]
    LEFT JOIN table10 t10
        ON t10.[vouchergroupid] = t9.[vouchergroupid]
        AND t10.[territoryid] = t2.[territoryid]
    LEFT JOIN table11 t11
        ON t11.[voucherid] = t8.[voucherid]
    LEFT JOIN table12 t12
        ON t12.[orderid] = t1.[orderid]
GROUP BY 
    t5.[transactionid]

Takes about 2.5 seconds to finish. When I save it to a view and run it as:

SELECT * FROM viewName;

It takes 7 seconds to finish.

What is the reason and how can I make the view faster?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

一页 2024-12-25 02:38:36

MySql 中处理视图的方式存在性能问题。 此处查看此答案。

基本上,MySql 中有两种不同的算法用于视图:Merge 和 Temptable。 Merge 通常表现得很好,但 Temptable 的表现可能很差。当视图包含某些构造(包括 GROUP BY)时,无法使用合并,因此您的视图将使用 Temptable 算法。

There are performance problems with the way views are handled in MySql. Check out this answer here.

Basically there are 2 different algorithms that are used for views in MySql: Merge and Temptable. Merge usually performs quite well but Temptable can perform quite poorly. Merge cannot be used when the view contains certain constructs including GROUP BY, so your view would be using the Temptable algorithm.

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