查找MySQL联接查询中分析的总行数

查找MySQL联接查询中分析的总行数,mysql,join,query-optimization,Mysql,Join,Query Optimization,我有一个MySQL查询,它有12个表的JOIN。当我解释查询时,一个表显示394699行,另一个表显示185368行。所有其他表都有1-3行。我仅从查询id 472行获得的总结果。但要做到这一点,需要1分钟以上的时间 有没有办法检查分析了多少行以产生这样的结果?这样,我就可以找到哪一张桌子花费的时间更高 我给出下面的查询结构。由于表格结构太高,我无法在此提供。 SELECT h.nid,h.attached_nid,h.created, s.field_species_value as spec

我有一个
MySQL
查询,它有12个表的
JOIN
。当我解释查询时,一个表显示394699行,另一个表显示185368行。所有其他表都有1-3行。我仅从查询id 472行获得的总结果。但要做到这一点,需要1分钟以上的时间

有没有办法检查分析了多少行以产生这样的结果?这样,我就可以找到哪一张桌子花费的时间更高

我给出下面的查询结构。由于表格结构太高,我无法在此提供。

SELECT h.nid,h.attached_nid,h.created, s.field_species_value as species, g.field_gender_value as gender, u.field_unique_id_value as unqid, n.title, dob.field_adult_healthy_weight_value as birth_date, dcolor.field_dog_primary_color_value as dogcolor, ccolor.field_primary_color_value as catcolor, sdcolor.field_dog_secondary_color_value as sdogcolor, sccolor.field_secondary_color_value as scatcolor, dpattern.field_dog_pattern_value as dogpattern, cpattern.field_cat_pattern_value as catpattern 
FROM table1 h 
JOIN table2 n ON n.nid = h.nid 
JOIN table3 s ON n.nid = s.entity_id 
JOIN table4 u ON n.nid = u.entity_id 
LEFT JOIN table5 g ON n.nid = g.entity_id 
LEFT JOIN table6 dob ON n.nid = dob.entity_id 
LEFT JOIN table7 AS dcolor ON n.nid = dcolor.entity_id 
LEFT JOIN table8 AS ccolor ON n.nid = ccolor.entity_id 
LEFT JOIN table9 AS sdcolor ON n.nid = sdcolor.entity_id 
LEFT JOIN table10 AS sccolor ON n.nid = sccolor.entity_id 
LEFT JOIN table11 AS dpattern ON n.nid = dpattern.entity_id 
LEFT JOIN table12 AS cpattern ON n.nid = cpattern.entity_id 
WHERE h.title = '4208'
AND ((h.created BETWEEN 1483257600 AND 1485935999)) 
AND h.uid!=1 
AND h.uid IN(
    SELECT etid 
    FROM `table` 
    WHERE gid=464 
    AND entity_type='user') 
AND h.attached_nid>0 
ORDER BY CAST(h.created as UNSIGNED) DESC;
下面是我得到的解释结果

    +------+--------------+---------------+--------+----------------------+---------------------+---------+----------------------+--------+----------------------------------------------+
| id   | select_type  | table         | type   | possible_keys        | key                 | key_len | ref                  | rows   | Extra                                        |
+------+--------------+---------------+--------+----------------------+---------------------+---------+----------------------+--------+----------------------------------------------+
|    1 | PRIMARY      | s             | index  | entity_id            | field_species_value | 772     | NULL                 | 394699 | Using index; Using temporary; Using filesort |
|    1 | PRIMARY      | u             | ref    | entity_id            | entity_id           | 4       | pantheon.s.entity_id |      1 |                                              |
|    1 | PRIMARY      | n             | eq_ref | PRIMARY              | PRIMARY             | 4       | pantheon.s.entity_id |      1 |                                              |
|    1 | PRIMARY      | g             | ref    | entity_id            | entity_id           | 4       | pantheon.s.entity_id |      1 |                                              |
|    1 | PRIMARY      | dob           | ref    | entity_id            | entity_id           | 4       | pantheon.s.entity_id |      1 |                                              |
|    1 | PRIMARY      | dcolor        | ref    | entity_id            | entity_id           | 4       | pantheon.s.entity_id |      1 |                                              |
|    1 | PRIMARY      | ccolor        | ref    | entity_id            | entity_id           | 4       | pantheon.s.entity_id |      1 |                                              |
|    1 | PRIMARY      | sdcolor       | ref    | entity_id            | entity_id           | 4       | pantheon.s.entity_id |      1 |                                              |
|    1 | PRIMARY      | sccolor       | ref    | entity_id            | entity_id           | 4       | pantheon.s.entity_id |      1 |                                              |
|    1 | PRIMARY      | dpattern      | ref    | entity_id            | entity_id           | 4       | pantheon.s.entity_id |      1 |                                              |
|    1 | PRIMARY      | cpattern      | ref    | entity_id            | entity_id           | 4       | pantheon.s.entity_id |      1 |                                              |
|    1 | PRIMARY      | h             | ref    | attached_nid,nid,uid | nid                 | 5       | pantheon.s.entity_id |      3 | Using index condition; Using where           |
|    1 | PRIMARY      | <subquery2>   | eq_ref | distinct_key         | distinct_key        | 4       | func                 |      1 | Using where                                  |
|    2 | MATERIALIZED | og_membership | ref    | entity,gid           | gid                 | 4       | const                | 185368 | Using where                                  |
+------+--------------+---------------+--------+----------------------+---------------------+---------+----------------------+--------+----------------------------------------------+
+------+--------------+---------------+--------+----------------------+---------------------+---------+----------------------+--------+----------------------------------------------+
|id |选择|类型|类型|可能的|键|键|列|参考|行|额外|
+------+--------------+---------------+--------+----------------------+---------------------+---------+----------------------+--------+----------------------------------------------+
|1 |主要| s |索引|实体| id |字段|物种|值| 772 |空| 394699 |使用索引;使用临时设备;使用文件排序|
|1 |主要| u |参考|实体| id |实体| id | 4 |万神殿美国实体| id | 1 ||
|1 | PRIMARY | n | eq | ref | PRIMARY | PRIMARY | 4 |万神殿美国实体| id | 1 ||
|1 |主要| g |参考|实体| id |实体| id | 4 |万神殿美国实体| id | 1 ||
|1 |主要| dob |参考|实体| id |实体| id | 4 |万神殿美国实体| id | 1 ||
|1 |主要|颜色|参考|实体| id |实体| id | 4 |万神殿美国实体| id | 1 ||
|1 |主要| ccolor |参考|实体| id |实体| id | 4 |万神殿美国实体| id | 1 ||
|1 |主要| sdcolor |参考|实体| id |实体| id | 4 |万神殿美国实体| id | 1 ||
|1 |主要| sccolor |参考|实体| id |实体| id | 4 |万神殿美国实体| id | 1 ||
|1 |主要| D模式|参考|实体| id |实体| id | 4 |万神殿美国实体| id | 1 ||
|1 |主要|模式|参考|实体|实体| id | 4 |万神殿美国实体| id | 1 ||
|1 |主要| h |参考|附| nid,nid | nid | 5 |万神殿美国实体| id | 3 |使用索引条件;在哪里使用|
|1 | PRIMARY | eq | ref | distinct | key | distinct | key | 4 | func | 1 |使用where|
|2 |物化| og|U成员|参考|实体,gid | gid | 4 | const | 185368 |使用where|
+------+--------------+---------------+--------+----------------------+---------------------+---------+----------------------+--------+----------------------------------------------+

您可以使用性能模式找到所检查的

下面是指向性能模式快速入门指南的链接。

这是我在PHP应用程序中运行的查询,以找出需要优化的查询。你应该能够很容易地适应它

查询查找在此之前运行的查询的统计信息。因此,在我的应用程序中,我在每次运行查询之后运行查询,存储结果,然后在PHP脚本结束时,输出请求期间运行的每个查询的统计数据

            SELECT `EVENT_ID`, TRUNCATE(`TIMER_WAIT`/1000000000000,6) as Duration,
            `SQL_TEXT`, `DIGEST_TEXT`, `NO_INDEX_USED`, `NO_GOOD_INDEX_USED`, `ROWS_AFFECTED`, `ROWS_SENT`, `ROWS_EXAMINED` 
            FROM `performance_schema`.`events_statements_history` 
            WHERE 
            `CURRENT_SCHEMA` = '{$database}' AND `EVENT_NAME` LIKE 'statement/sql/%'
            AND `THREAD_ID` = (SELECT `THREAD_ID` FROM `performance_schema`.`threads` WHERE `performance_schema`.`threads`.`PROCESSLIST_ID` = CONNECTION_ID()) 
            ORDER BY `EVENT_ID` DESC LIMIT 1;

您可以使用性能模式找到所检查的

下面是指向性能模式快速入门指南的链接。

这是我在PHP应用程序中运行的查询,以找出需要优化的查询。你应该能够很容易地适应它

查询查找在此之前运行的查询的统计信息。因此,在我的应用程序中,我在每次运行查询之后运行查询,存储结果,然后在PHP脚本结束时,输出请求期间运行的每个查询的统计数据

            SELECT `EVENT_ID`, TRUNCATE(`TIMER_WAIT`/1000000000000,6) as Duration,
            `SQL_TEXT`, `DIGEST_TEXT`, `NO_INDEX_USED`, `NO_GOOD_INDEX_USED`, `ROWS_AFFECTED`, `ROWS_SENT`, `ROWS_EXAMINED` 
            FROM `performance_schema`.`events_statements_history` 
            WHERE 
            `CURRENT_SCHEMA` = '{$database}' AND `EVENT_NAME` LIKE 'statement/sql/%'
            AND `THREAD_ID` = (SELECT `THREAD_ID` FROM `performance_schema`.`threads` WHERE `performance_schema`.`threads`.`PROCESSLIST_ID` = CONNECTION_ID()) 
            ORDER BY `EVENT_ID` DESC LIMIT 1;

要减少从
og_成员身份
访问的行数,请尝试添加包含
gid
实体类型
etid
字段的索引。包括
gid
entity\u type
应使查找更有效,包括
etid
将使索引成为覆盖索引

添加索引后,再次运行
EXPLAIN
查看结果。根据新的解释计划,保留索引、删除索引和/或添加其他索引。继续这样做,直到你得到满意的结果

当然,您会希望尝试使用temporary
或使用filesort
来消除对
的任何提及<代码>使用临时表
意味着使用临时表进行此查询,可能是因为其大小太大