Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MYSQL评测快-查询慢?_Mysql_Mysqli - Fatal编程技术网

MYSQL评测快-查询慢?

MYSQL评测快-查询慢?,mysql,mysqli,Mysql,Mysqli,我得到了一个MYSQL查询,它的配置非常快(0.0004 Sek),但执行时需要30秒以上。它是一个独立的专用服务器,所以背后有一些电源 您可以看到下面的查询。我相信它可以大大改进。我只是不明白分析值和实际执行时间之间的差异来自哪里 SELECT DISTINCT x.loc_id AS id_a, hier2.loc_id AS id_b, hier3.loc_id AS id_c, hier5.loc_id AS id_d, hier6.loc_id AS id_e, hier7.loc_

我得到了一个MYSQL查询,它的配置非常快(0.0004 Sek),但执行时需要30秒以上。它是一个独立的专用服务器,所以背后有一些电源

您可以看到下面的查询。我相信它可以大大改进。我只是不明白分析值和实际执行时间之间的差异来自哪里

SELECT DISTINCT x.loc_id AS id_a, 
hier2.loc_id AS id_b, hier3.loc_id AS id_c, hier5.loc_id AS id_d, hier6.loc_id AS id_e, hier7.loc_id AS id_f, hier8.loc_id AS id_g, 
hier2.loc_name AS name_b, hier3.loc_name AS name_c, hier5.loc_name AS name_d, hier6.loc_name AS name_e, hier7.loc_name AS name_f, hier8.loc_name AS name_g 

FROM objects AS o 

LEFT JOIN geo_xref AS x ON o.id = x.oid 
LEFT JOIN locations AS hier2 ON x.hier2 = hier2.loc_id 
LEFT JOIN locations AS hier3 ON x.hier3 = hier3.loc_id 
LEFT JOIN locations AS hier5 ON x.hier5 = hier5.loc_id 
LEFT JOIN locations AS hier6 ON x.hier6 = hier6.loc_id 
LEFT JOIN locations AS hier7 ON x.hier7 = hier7.loc_id 
LEFT JOIN locations AS hier8 ON x.hier8 = hier8.loc_id 

WHERE o.published = 1 AND x.loc_id IS NOT NULL 
说明


身份证件
选择类型
桌子
类型
可能的\u键
钥匙
基伦
裁判
排
额外的
1.
简单的
x
全部的
主,loc_id,xcon
无效的
无效的
无效的
187
在何处使用;使用临时
1.
简单的
o
等式参考
主要的,重要的
主要的,重要的
4.
db.x.oid
1.
在哪里使用
1.
简单的
层次2
裁判
地址
地址
4.
db.x.hier2
5.
无效的
1.
简单的
层次3
裁判
地址
地址
4.
db.x.hier3
5.
无效的
1.
简单的
层次结构5
裁判
地址
地址
4.
db.x.hier5
5.
无效的
1.
简单的
层次6
裁判
地址
地址
4.
db.x.hier6
5.
无效的
1.
简单的
层次7
裁判
地址
地址
4.
db.x.hier7
5.
无效的
1.
简单的
等级8
裁判
地址
地址
4.
db.x.hier8
5.
无效的

评测时,始终使用
SQL\u NO\u CACHE
关键字禁用返回缓存的查询结果

SELECT SQL_NO_CACHE DISTINCT x.loc_id AS id_a,
...

要提高查询性能,请缩小结果集(通过应用
WHERE
子句中的条件),并确保在
位置上有索引。loc\u id
geo\u xref.oid

是否可以包含
解释
结果?另外,您在上面提到的表上有索引吗?此外,您可能应该对
geo\u xref
表进行非规范化。像
hier2
hier3
hierN
,…,这样的列不符合使用关系数据库的目的。到目前为止,所有内容都已编制索引。不是评测,而是发布
EXPLAIN
结果。
EXPLAIN
结果将告诉您数据库将如何获得其结果。它将帮助您找出查询运行缓慢的原因。如果您不需要将输出列设置为
name.*
格式,请在locations.loc\u id IN(x.hier2,x.hier3,…,x.hier8)中使用单个
左连接locations.loc\u id
即可。posted EXPLAIN statement原始查询中有ofc where子句。还将在字段上设置索引。