MySQL View 性能

标签 mysql performance view

我有一张可以容纳大约 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/

相关文章:

mysql - 在 SQL 查询中搜索一维 JSON 数组(["1"、 "2"、...])

php - 调用成员函数是否为null?

php - MySQL Select 数据分隔字符串

c# - MySQL 和 LINQ to SQL 通过 Connector/Net | C#

php - 在 MySQL 数据库中执行 REGEXP 查找和替换的最快方法是什么?

Java2D : BufferedImage not accelerated on Ubuntu

android - 两个 ImageViews 出现在导航标题中,而我只在 xml 中设置一个

SQL 'case when' 对比 'where' 效率

sql - 如何使用 Alter 在 SQL-Server 2014 的现有 View 中添加新列

javascript - 使用 emberjs 选择 View 填充选择框