选择查询比查看快 2-3 倍
此查询单独运行:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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.