MySQL 视图比普通查询更快吗?
我有一个使用许多连接(实际上是 8 个)的复杂查询。我正在考虑将其简化为视图。经过一番研究后,我可以看到简单性和安全性方面的好处。但我没有看到任何提到速度的内容。
视图的工作方式是否类似于预编译查询的预编译语句?使用视图是否有显着的性能提升?
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.
Do views work like prepared statements, where the query is pre-compiled? Is there any notable performance gain from using views?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不,视图只是一个存储的文本查询。您可以对其应用
WHERE
和ORDER
,执行计划将根据这些子句进行计算。No, a view is simply a stored text query. You can apply
WHERE
andORDER
against it, the execution plan will be calculated with those clauses taken into consideration.视图基本上是一个存储的子查询。 之间本质上没有区别,
和
只是视图更便携,因为您不必每次想要使用它返回的任何数据时都编写底层查询。
A view's basically a stored subquery. There's essentially no difference between:
and
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.
有时它会有所帮助,但这并不是灵丹妙药。我见过视图有助于性能,但我也见过它损害性能。视图可以强制实现,如果 MySQL 没有为您选择一个好的访问路径,这有时可以为您提供更好的访问路径。
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.