我有一张可以容纳大约 100,000 个用户的表格。
第一种情况:
explain select state, count(*) as cnt from users where state = 'ca'
当我为上述查询做一个解释计划时,我得到的成本是 5200
第二种情况:
Create or replace view vw_users as select state, count(*) as cnt from users
Explain select cnt from vw_users where state = 'ca'
当我对第二个查询执行解释计划时,我得到的成本是 100,000。
View 中的 where 子句如何工作? View 检索所有行后是否应用 where 子句?我该如何解决这个问题?
最佳答案
关于 view algorithm已经用过了。
merge 算法适用于大多数表索引,而诸如此类 - temptable 算法则不行 - 在许多情况下,您的索引将完全不使用.
还有很多合并不支持的废话
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 View 性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2760475/