MySQL 视图是否比普通查询更快?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4789332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 18:29:01  来源:igfitidea点击:

Is a MySQL view faster than a normal query?

mysqloptimizationview

提问by DisgruntledGoat

I have a complex query using many joins (8 in fact). I was thinking of simplifying it into a view. After a little research I can see the benefits in simplicity and security. But I didn't see any mentions of speed.

我有一个使用许多连接的复杂查询(实际上是 8 个)。我正在考虑将其简化为视图。经过一些研究,我可以看到简单性和安全性的好处。但是我没有看到任何提及速度的内容。

Do views work like prepared statements, where the query is pre-compiled? Is there any notable performance gain from using views?

视图是否像准备好的语句一样工作,其中查询是预编译的?使用视图是否有任何显着的性能提升?

采纳答案by Larry Lustig

No, a view is simply a stored text query. You can apply WHEREand ORDERagainst it, the execution plan will be calculated with those clauses taken into consideration.

不,视图只是存储的文本查询。您可以申请WHEREORDER反对它,执行计划将在考虑这些条款的情况下计算。

回答by Marc B

A view's basically a stored subquery. There's essentially no difference between:

视图基本上是一个存储的子查询。两者之间基本上没有区别:

SELECT *
FROM someview

and

SELECT *
FROM (
    SELECT somestuff 
    FROM underlying table
);

except the view's a bit more portable, as you don't have to write out the underlying query each time you want to work with whatever data it returns.

除了视图更具可移植性,因为每次要处理它返回的任何数据时,您都不必写出基础查询。

回答by Ray

It can sometimes help, but it's no silver bullet. I've seen a view help performance, but I've also seen it hurt it. A view can force materialization, which can sometimes get you a better access path if MySQL isn't choosing a good one for you.

它有时会有所帮助,但它不是灵丹妙药。我看过一个视图有助于性能,但我也看到它伤害了它。视图可以强制实现,如果 MySQL 没有为您选择一个好的访问路径,它有时可以为您提供更好的访问路径。