我最近了解了 View 的概念,我发现它们对于将复杂查询分成多个部分有很大帮助。
我的问题是,当我开始从 View 进行查询时,它们又从其他 View 进行查询,等等,是否存在任何效率劣势......
所以我会:
view1 -> query from tables A, B & C
view2 -> query from tables D, E & F
view3 -> query joining view1 & view2
当查询 view3 而不是设计连接表 A、B、C、D、E 和 F 的单个查询时,是否会存在速度劣势?
如果我选择使用 View 方法,我在 view1、view2 和 view3 的设计中是否有 ORDER BY 子句重要吗?还是最好不要在任何 View 中放置任何 ORDER BY 子句?的 View ,我在查询 view3 时只使用 ORDER BY?
非常感谢您的帮助! 博加。
最佳答案
对于排序依据
,请参阅 CREATE VIEW Syntax
ORDER BY is permitted in a view definition, but it is ignored if you select from a view using a statement that has its own ORDER BY.
这里是View Processing Algorithms ,您可以看到 MySQL 如何处理 View 上的选择。一如既往,这取决于。 ;-)
看起来MERGE
算法是最有效的,因为算法temptable
首先将 View 结果复制到临时表并对其进行查询。但你不能总是使用合并,请参阅最后一节
If the MERGE algorithm cannot be used, a temporary table must be used instead. MERGE cannot be used if the view contains any of the following constructs:
- Aggregate functions (SUM(), MIN(), MAX(), COUNT(), and so forth)
- DISTINCT
- GROUP BY
- HAVING
- LIMIT
- UNION or UNION ALL
- Subquery in the select list
- Refers only to literal values (in this case, there is no underlying table)
关于mysql - 浅谈MySQL中 View 的 View 的使用效率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13700331/