gpt4 book ai didi

mysql - 试图找出 LEFT JOIN MySQL 查询非常慢的原因

转载 作者:行者123 更新时间:2023-11-29 03:37:44 25 4
gpt4 key购买 nike

如果您能帮助我找出基本查询表连接返回时间非常慢的原因,我将不胜感激。

我遇到了问题,所以我打开了性能分析和查询,它用了 19.7903 秒返回,显示了以下配置文件详细信息:

Profiling
Status Time
starting 0.000044
Opening tables 0.000067
System lock 0.000002
Table lock 0.000006
init 0.000009
optimizing 0.000005
statistics 0.000011
preparing 0.000014
executing 0.000031
Sending data 0.000050
end 0.000004
end 0.000003
query end 0.000002
freeing items 0.000009
closing tables 0.000003
removing tmp table 0.000011
closing tables 0.000003
logging slow query 0.000002
cleaning up 0.000003

Showing rows 0 - 29 (2,200 total, Query took 19.7903 sec)

我不明白为什么配置文件时间加起来不等于“19.7903 秒”。

  • 'profile' 时间和'total, Query' 时间加起来吗?<<<<<<<

查询是:

SELECT OWNER.ID                                         OWNER_ID,
OWNER.LAST_NAME OWNER_LAST_NAME,
OWNER.FIRST_NAME OWNER_FIRST_NAME,
OWNER.PHONE_HOME_AREACODE OWNER_PHONE_HOME_AREACODE,
OWNER.PHONE_HOME_PREFIX OWNER_PHONE_HOME_PREFIX,
OWNER.PHONE_HOME_LINE_NUMBER OWNER_PHONE_HOME_LINE_NUMBER,
OWNER.PHONE_CELL_AREACODE OWNER_PHONE_CELL_AREACODE,
OWNER.PHONE_CELL_PREFIX OWNER_PHONE_CELL_PREFIX,
OWNER.PHONE_CELL_LINE_NUMBER OWNER_PHONE_CELL_LINE_NUMBER,
/*Some columns from OWNER removed for brevity*/
OWNER.CITY OWNER_CITY,
OWNER.PROVINCE OWNER_PROVINCE,
OWNER.POSTAL OWNER_POSTAL,
OWNER.NOTES OWNER_NOTES,
OWNER.REFERRED_BY OWNER_REFERRED_BY,
VISITOR.NAME VISITOR_NAME,
VISITOR.SIZE VISITOR_SIZE,
VISITOR.INACTIVE VISITOR_INACTIVE,
VISITOR.ID VISITOR_ID,
VISITOR.DELETED VISITOR_DELETED,
VACCINATIONS.CANINE_DISTEMPER_PARVOVIRUS_EXPIRES VACCINATIONS_CANINE_DISTEMPER_PARVOVIRUS_EXPIRES,
VACCINATIONS.CANINE_RABIES_EXPIRES VACCINATIONS_CANINE_RABIES_EXPIRES,
VACCINATIONS.CANINE_BORDETELLA_EXPIRES VACCINATIONS_CANINE_BORDETELLA_EXPIRES,
VACCINATIONS.FELINE_FVRCPC_EXPIRES VACCINATIONS_FELINE_FVRCPC_EXPIRES,
VACCINATIONS.FELINE_RABIES_EXPIRES VACCINATIONS_FELINE_RABIES_EXPIRES
FROM OWNER
LEFT JOIN VISITOR
ON VISITOR.OWNER_ID = OWNER.ID
LEFT JOIN VACCINATIONS
ON VACCINATIONS.VISITOR_ID = VISITOR.ID
ORDER BY VISITOR_NAME

解释的结果:

id  select_type     table   type    possible_keys   key     key_len     ref     rows    Extra
1 SIMPLE OWNER ALL NULL NULL NULL NULL 1483 Using temporary; Using filesort
1 SIMPLE VISITOR ref OWNER_ID OWNER_ID 4 wayDEV.OWNER.ID 1
1 SIMPLE VACCINATIONS ALL VISITOR_ID NULL NULL NULL 2059

索引:

VISITOR:
Indexes: Documentation Keyname Type Cardinality Action Field
PRIMARY PRIMARY 2227 Edit Drop ID
NAME INDEX 2227 Edit Drop NAME
OWNER_ID INDEX 2227 Edit Drop OWNER_ID


OWNER:
Indexes: Documentation Keyname Type Cardinality Action Field
PRIMARY PRIMARY 1601 Edit Drop ID
LAST_NAME INDEX 1601 Edit Drop LAST_NAME

VACCINATIONS:
Indexes: Documentation Keyname Type Cardinality Action Field
PRIMARY PRIMARY 2131 Edit Drop ID
VISITOR_ID UNIQUE 2131 Edit Drop VISITOR_ID

BOARDING:
Indexes: Documentation Keyname Type Cardinality Action Field
PRIMARY PRIMARY 2256 Edit Drop ID
OWNER INDEX 2256 Edit Drop OWNER_ID
VISITOR INDEX 2256 Edit Drop VISITOR_ID

GROOMING:
Indexes: Documentation Keyname Type Cardinality Action Field
PRIMARY PRIMARY 2077 Edit Drop ID
VISITOR_ID UNIQUE 2077 Edit Drop VISITOR_ID
OWNER INDEX 2077 Edit Drop OWNER_ID

此查询几个月来都表现良好。直到最近,它才间歇性地恢复缓慢。 大约 20% 的时间它会变慢。其余时间它返回得很好(显示第 0 - 29 行(总共 2,200,查询耗时 0.0018 秒))。 ? :\

  • 它是一个间歇性问题这一事实是否表明某些查询本身以外的问题??<<<<<<<

    正如我上面所问的...我不明白为什么配置文件时间加起来不等于“19.7903 秒”。

  • 'profile' 时间和'total, Query' 时间加起来吗? <<<<<<<


整个数据库只有几千条记录。

如有任何帮助,我们将不胜感激。我在 Godaddy 服务器上。

(我知道我的查询可能有问题.. 或者它可以被优化。但我在这里问一些非常具体的问题 - 注意到...“<<<<<<<")

老实说...我们在这里谈论的是 >>>>>>20<<<<<<< 秒!当然,这个对 2000 条记录数据库的查询不应该在 20 秒后返回???

最佳答案

问题是您在 LEFT JOIN 中按第二个表中的字段排序。那就是实际上没有索引可以有效地用于排序,因此 MySQL 对每行中包含大量数据的行进行排序。 MySQL 中对此类查询的通常解决方案是仅选择 id 并在子查询中进行排序,然后返回连接到其余列。当您也进行一些分页时尤其如此(LIMIT 也应该在子查询中)。

您需要一个针对 VISITOR 的复合覆盖索引 (owner_id, name),以及针对 VACCINATIONS 的 (visitor_id)。

现在重写查询如下:

SELECT ...
FROM (
SELECT o.ID as o_ID, v.ID as v_ID
FROM OWNER o
LEFT JOIN VISITOR v ON o.ID = v.OWNER_ID
ORDER BY v.NAME
) ids
JOIN OWNER o ON o.ID = ids.o_ID
LEFT JOIN VISITOR v ON v.ID = ids.v_ID
LEFT JOIN VACCINATIONS v2 ON v2.VISITOR_ID = v.ID;

关于mysql - 试图找出 LEFT JOIN MySQL 查询非常慢的原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18990776/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com