2

我将不胜感激任何帮助试图找到基本查询表连接的返回时间非常慢的原因。

我遇到了麻烦,所以我打开了需要 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 秒后返回???

4

2 回答 2

1

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

您需要一个复合覆盖索引 (owner_id, name) 在 VISITOR 上,和 (visitor_id) 在 VACCINATIONS 上。

现在重写查询如下:

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;
于 2013-09-24T21:55:52.280 回答
0

列的数据类型不一样 - 即

OWNER.ID - DATA TYPE = (int7)
VISITOR.OWNER_ID - DATA TYPE = (int7)
VACCINATIONS.VISITOR_ID  - DATA TYPE = (varchar7)

设置数据类型 - for VACCINATIONS.VISITOR_ID - (int7),解决了缓慢问题 - 此表连接超过 5000 条记录,现在与任何其他 MySQL 调用一样快。

感谢“大师” http://forums.phpfreaks.com/topic/173475-left-join-extremely-slow-any-way-to-remedy-16000-lines/

于 2014-11-04T23:07:48.817 回答