mysql查看超慢

标签 mysql sql database mysql-slow-query-log

这是对统一医学语言系统 (UMLS) 的查询,用于查找与规范化词相关的词。此查询结果为 165 毫秒,但如果我运行同一查询的 VIEW,则需要 70 秒。我是 mysql 的新手。请帮助我。

查询:

SELECT a.nwd as Normalized_Word, 
       b.str as String, 
       c.def as Defination, 
       d.sty as Semantic_type 
FROM mrxnw_eng a, mrconso b, mrdef c, mrsty d 
WHERE a.nwd = 'cold' 
     AND b.sab = 'Msh'
     AND a.cui = b.cui 
     AND a.cui = c.cui
     AND a.cui = d.cui
     AND a.lui = b.lui
     AND b.sui = a.sui
group by a.cui

查看定义:

create view nString_Sementic as 
SELECT a.nwd as Normalized_Word, 
       b.str as String, 
       c.def as Defination, 
       d.sty as Semantic_type 
FROM mrxnw_eng a, mrconso b, mrdef c, mrsty d 
WHERE b.sab = 'Msh'
     AND a.cui = b.cui 
     AND a.cui = c.cui
     AND a.cui = d.cui
     AND a.lui = b.lui
     AND b.sui = a.sui
group by a.cui   

从 View 中选择:

 select * nString_Sementic   
 where nwd = 'phobia'

最佳答案

您可以通过将 VIEW ALGORITHM 指定为 MERGE 来获得更好的性能。使用 MERGE MySQL 会将 View 与外部 SELECT 的 WHERE 语句结合起来,然后提出优化的执行计划。

然而,要做到这一点,您必须从您的 VIEW 中删除 GROUP BY 语句。实际上,在被您的 WHERE 语句过滤之前,首先会为整个 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)

这是包含更多信息的链接。 http://dev.mysql.com/doc/refman/8.0/en/view-algorithms.html

如果您可以将 View 更改为不包含 GROUP BY 语句,则指定 View 的算法语法为:

CREATE ALGORITHM = MERGE VIEW...

编辑:这个答案最初是基于 MySQL 5.0。我已经更新了指向当前文档的链接,但我没有以其他方式确认答案是否适用于版本 >5.0。

关于mysql查看超慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17681449/

相关文章:

mysql - 我想在nodejs中的mysql中添加一个变量

mysql - 复杂SQL排名查询

sql - 我可以在 SQL 中的另一个 "With"中做一个内部 "With"吗?

java - 全静态方法和应用单例模式有什么区别?

MySQL 检查 3 个或更多连续(特定)条目

php - while 循环结果中的表单提交仅出现在最后一行?

php - localhost/phpmyadmin 不工作,只看到一堆文本

php - 尝试从数据库检索数据时出错

sql - 从每个类别中至少选择一个但不超过一个,并且没有与另一列重复

python - 将项目插入 MongoDb 后,如何获取其 ObjectID?