mySQL真的很慢运行查询 - 60秒

时间:2014-10-22 20:27:09

标签: mysql drupal-7 query-optimization database-indexes

我对Drupal 7有一个查询,运行时间超过60秒。有很多连接,但是所有正在连接的项目都有索引(BTREE)所以我不确定它们为什么运行这么慢。也许这只是它的方式?我不是DBA,或者是这个查询的开发者,但我正在尝试诊断瓶颈,因为我们看到了大问题,例如谷歌索引我们。

感谢您提供的任何帮助。

*更新的查询和解释 请注意,在推荐更改后,我至少可以将处理时间缩短约20%。它仍然很慢,但速度越来越快。 where和order by子句中的所有字段都有索引。此外,我不知道我怎么能够停止使用“!=”而不是。我当然愿意接受建议。最后,我也不确定如何让它停止使用filesort或尊重我的索引。我很高兴发布索引,如果你认为它会有所帮助:)感谢大家到目前为止的帮助! *

   SELECT 
    n.type AS type,
    CASE
        WHEN n.type = 'book_new' THEN workid.field_workid_target_id
        ELSE n.nid
    END AS uuid,
    CASE
        WHEN n.type = 'book_new' THEN UNIX_TIMESTAMP(MIN(sale_date.field_onsaledate_value))
        ELSE n.created
    END AS sort_date
FROM
    node n
        LEFT OUTER JOIN
    field_data_field_related_books books ON n.nid = books.entity_id
        LEFT OUTER JOIN
    field_data_field_author_ref author_refs ON books.entity_id = author_refs.entity_id
        LEFT OUTER JOIN
    field_data_field_workid workid ON n.nid = workid.entity_id
        LEFT OUTER JOIN
    field_data_field_onsaledate sale_date ON workid.entity_id = sale_date.entity_id
        LEFT OUTER JOIN
    field_data_field_format format ON sale_date.entity_id = format.entity_id
        LEFT OUTER JOIN
    field_data_field_subjectcategories subjects ON format.entity_id = subjects.entity_id
        AND subjects.field_subjectcategories_tid = '48981'
        LEFT OUTER JOIN
    field_data_field_subjectcategories subjects2 ON subjects.entity_id = subjects2.entity_id
        AND subjects2.field_subjectcategories_tid = '54556'
        LEFT OUTER JOIN
    field_data_field_subjectcategories subjects3 ON subjects2.entity_id = subjects3.entity_id
        AND subjects3.field_subjectcategories_tid = '61091'
WHERE
    ((books.field_related_books_target_id IN ('874271'))
        OR (author_refs.field_author_ref_target_id IN ('874561' , '572716', '874551'))
        OR (((subjects.field_subjectcategories_tid IS NOT NULL)
        AND (subjects2.field_subjectcategories_tid IS NOT NULL))
        OR ((subjects2.field_subjectcategories_tid IS NOT NULL)
        AND (subjects3.field_subjectcategories_tid IS NOT NULL))
        OR ((subjects.field_subjectcategories_tid IS NOT NULL)
        AND (subjects3.field_subjectcategories_tid IS NOT NULL))))
        AND (n.status = '1')
        AND (n.type != 'wow')
        AND (n.type != 'event')
        AND (n.type != 'slice')
        AND (n.nid NOT IN ('874271'))
        AND (sale_date.field_onsaledate_value < CURDATE()
        OR sale_date.field_onsaledate_value IS NULL)
        AND ((format.field_format_tid NOT IN ('2296' , '4986', '1641', '1756'))
        OR (format.field_format_tid IS NULL))

GROUP BY uuid

ORDER BY    books.field_related_books_target_id = 874271 DESC , 
            author_refs.field_author_ref_target_id = 874561 DESC , 
            author_refs.field_author_ref_target_id = 572716 DESC , 
            author_refs.field_author_ref_target_id = 874551 DESC , 
            sort_date DESC
LIMIT 36 OFFSET 35;



   +----+-------------+-------------+------+---------------------------------------+------------------+---------+---------------------------+--------+----------+--------------------------------------------------------+
| id | select_type | table       | type | possible_keys                         | key              | key_len | ref                       | rows   | filtered | Extra                                                  |
+----+-------------+-------------+------+---------------------------------------+------------------+---------+---------------------------+--------+----------+--------------------------------------------------------+
|  1 | SIMPLE      | n           | ref  | PRIMARY,node_status_type,node_type    | node_status_type | 4       | const                     | 342627 |      100 | Using index condition; Using temporary; Using filesort |
|  1 | SIMPLE      | books       | ref  | entity_id                             | entity_id        | 4       | prhc2.n.nid               |      1 |      100 | NULL                                                   |
|  1 | SIMPLE      | author_refs | ref  | entity_id                             | entity_id        | 4       | prhc2.books.entity_id     |      1 |      100 | NULL                                                   |
|  1 | SIMPLE      | workid      | ref  | entity_id                             | entity_id        | 4       | prhc2.n.nid               |      1 |      100 | NULL                                                   |
|  1 | SIMPLE      | sale_date   | ref  | entity_id                             | entity_id        | 4       | prhc2.workid.entity_id    |      1 |      100 | Using where                                            |
|  1 | SIMPLE      | format      | ref  | entity_id                             | entity_id        | 4       | prhc2.sale_date.entity_id |      1 |      100 | Using where                                            |
|  1 | SIMPLE      | subjects    | ref  | entity_id,field_subjectcategories_tid | entity_id        | 4       | prhc2.format.entity_id    |      1 |      100 | Using where                                            |
|  1 | SIMPLE      | subjects2   | ref  | entity_id,field_subjectcategories_tid | entity_id        | 4       | prhc2.subjects.entity_id  |      1 |      100 | Using where                                            |
|  1 | SIMPLE      | subjects3   | ref  | entity_id,field_subjectcategories_tid | entity_id        | 4       | prhc2.subjects2.entity_id |      1 |      100 | Using where                                            |
+----+-------------+-------------+------+---------------------------------------+------------------+---------+---------------------------+--------+----------+--------------------------------------------------------+



describe node;
+-----------+------------------+------+-----+---------+----------------+
| Field     | Type             | Null | Key | Default | Extra          |
+-----------+------------------+------+-----+---------+----------------+
| nid       | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| vid       | int(10) unsigned | YES  | UNI | NULL    |                |
| type      | varchar(32)      | NO   | MUL |         |                |
| language  | varchar(12)      | NO   | MUL |         |                |
| title     | varchar(255)     | NO   | MUL |         |                |
| uid       | int(11)          | NO   | MUL | 0       |                |
| status    | int(11)          | NO   | MUL | 1       |                |
| created   | int(11)          | NO   | MUL | 0       |                |
| changed   | int(11)          | NO   | MUL | 0       |                |
| comment   | int(11)          | NO   |     | 0       |                |
| promote   | int(11)          | NO   | MUL | 0       |                |
| sticky    | int(11)          | NO   |     | 0       |                |
| tnid      | int(10) unsigned | NO   | MUL | 0       |                |
| translate | int(11)          | NO   | MUL | 0       |                |
| downgrade | int(11)          | NO   |     | 0       |                |
+-----------+------------------+------+-----+---------+----------------+

describe field_data_field_related_books;
+-------------------------------+------------------+------+-----+---------+-------+
| Field                         | Type             | Null | Key | Default | Extra |
+-------------------------------+------------------+------+-----+---------+-------+
| entity_type                   | varchar(128)     | NO   | PRI |         |       |
| bundle                        | varchar(128)     | NO   | MUL |         |       |
| deleted                       | tinyint(4)       | NO   | PRI | 0       |       |
| entity_id                     | int(10) unsigned | NO   | PRI | NULL    |       |
| revision_id                   | int(10) unsigned | YES  | MUL | NULL    |       |
| language                      | varchar(32)      | NO   | PRI |         |       |
| delta                         | int(10) unsigned | NO   | PRI | NULL    |       |
| field_related_books_target_id | int(10) unsigned | NO   | MUL | NULL    |       |
+-------------------------------+------------------+------+-----+---------+-------+

describe field_data_field_author_ref;
+----------------------------+------------------+------+-----+---------+-------+
| Field                      | Type             | Null | Key | Default | Extra |
+----------------------------+------------------+------+-----+---------+-------+
| entity_type                | varchar(128)     | NO   | PRI |         |       |
| bundle                     | varchar(128)     | NO   | MUL |         |       |
| deleted                    | tinyint(4)       | NO   | PRI | 0       |       |
| entity_id                  | int(10) unsigned | NO   | PRI | NULL    |       |
| revision_id                | int(10) unsigned | YES  | MUL | NULL    |       |
| language                   | varchar(32)      | NO   | PRI |         |       |
| delta                      | int(10) unsigned | NO   | PRI | NULL    |       |
| field_author_ref_target_id | int(10) unsigned | NO   | MUL | NULL    |       |
+----------------------------+------------------+------+-----+---------+-------+

describe field_data_field_workid;
+------------------------+------------------+------+-----+---------+-------+
| Field                  | Type             | Null | Key | Default | Extra |
+------------------------+------------------+------+-----+---------+-------+
| entity_type            | varchar(128)     | NO   | PRI |         |       |
| bundle                 | varchar(128)     | NO   | MUL |         |       |
| deleted                | tinyint(4)       | NO   | PRI | 0       |       |
| entity_id              | int(10) unsigned | NO   | PRI | NULL    |       |
| revision_id            | int(10) unsigned | YES  | MUL | NULL    |       |
| language               | varchar(32)      | NO   | PRI |         |       |
| delta                  | int(10) unsigned | NO   | PRI | NULL    |       |
| field_workid_target_id | int(10) unsigned | NO   | MUL | NULL    |       |
+------------------------+------------------+------+-----+---------+-------+

describe field_data_field_onsaledate;
+------------------------+------------------+------+-----+---------+-------+
| Field                  | Type             | Null | Key | Default | Extra |
+------------------------+------------------+------+-----+---------+-------+
| entity_type            | varchar(128)     | NO   | PRI |         |       |
| bundle                 | varchar(128)     | NO   | MUL |         |       |
| deleted                | tinyint(4)       | NO   | PRI | 0       |       |
| entity_id              | int(10) unsigned | NO   | PRI | NULL    |       |
| revision_id            | int(10) unsigned | YES  | MUL | NULL    |       |
| language               | varchar(32)      | NO   | PRI |         |       |
| delta                  | int(10) unsigned | NO   | PRI | NULL    |       |
| field_onsaledate_value | datetime         | YES  |     | NULL    |       |
+------------------------+------------------+------+-----+---------+-------+

describe field_data_field_format;
+------------------+------------------+------+-----+---------+-------+
| Field            | Type             | Null | Key | Default | Extra |
+------------------+------------------+------+-----+---------+-------+
| entity_type      | varchar(128)     | NO   | PRI |         |       |
| bundle           | varchar(128)     | NO   | MUL |         |       |
| deleted          | tinyint(4)       | NO   | PRI | 0       |       |
| entity_id        | int(10) unsigned | NO   | PRI | NULL    |       |
| revision_id      | int(10) unsigned | YES  | MUL | NULL    |       |
| language         | varchar(32)      | NO   | PRI |         |       |
| delta            | int(10) unsigned | NO   | PRI | NULL    |       |
| field_format_tid | int(10) unsigned | YES  | MUL | NULL    |       |
+------------------+------------------+------+-----+---------+-------+

describe field_data_field_subjectcategories;
+-----------------------------+------------------+------+-----+---------+-------+
| Field                       | Type             | Null | Key | Default | Extra |
+-----------------------------+------------------+------+-----+---------+-------+
| entity_type                 | varchar(128)     | NO   | PRI |         |       |
| bundle                      | varchar(128)     | NO   | MUL |         |       |
| deleted                     | tinyint(4)       | NO   | PRI | 0       |       |
| entity_id                   | int(10) unsigned | NO   | PRI | NULL    |       |
| revision_id                 | int(10) unsigned | YES  | MUL | NULL    |       |
| language                    | varchar(32)      | NO   | PRI |         |       |
| delta                       | int(10) unsigned | NO   | PRI | NULL    |       |
| field_subjectcategories_tid | int(10) unsigned | YES  | MUL | NULL    |       |
+-----------------------------+------------------+------+-----+---------+-------+

2 个答案:

答案 0 :(得分:0)

从本质上讲,由于where条件的结构,一些注释,你的索引没有被击中;

  • NOT IN非常低效,请考虑将此更改为其他内容
  • &#34;使用索引条件&#34;在节点表上,指示where子句不包含足以使用索引的内容,并且选择执行全表扫描。我可以看到你有n.type索引,考虑索引n.nid已经没有了,并使用其他运算符而不是!=
  • 您的订单导致文件排序,查看您的mysql配置并确保它已针对文件排序进行了优化; http://dev.mysql.com/doc/refman/5.0/en/order-by-optimization.html

答案 1 :(得分:0)

我对连接的顺序做了一些更改,我不确定这是否与缓慢有关,但是当连接多个表时,我尝试从第一个表开始以逻辑方式链接它们并移动到下一个,下一个,下一个,...我不会从之前的连接跳回到同一个表,除非它是一个自连接并且我别名它。它可以帮助你更好地跟踪你的联接,如果你有更好的组织,这里是:

    SELECT 
    n.type AS type,
    CASE
        WHEN n.type = 'book_new' THEN workid.field_workid_target_id
        ELSE n.nid
    END AS uuid,
    CASE
        WHEN n.type = 'book_new' THEN UNIX_TIMESTAMP(MIN(sale_date.field_onsaledate_value))
        ELSE n.created
    END AS sort_date
FROM
    node n
        LEFT OUTER JOIN
    field_data_field_related_books books ON n.nid = books.entity_id
        LEFT OUTER JOIN
    field_data_field_author_ref author_refs ON books.entity_id = author_refs.entity_id
        LEFT OUTER JOIN
    field_data_field_workid workid ON workid.nid = author_refs.entity_id
        LEFT OUTER JOIN
    field_data_field_onsaledate sale_date ON workid.entity_id = sale_date.entity_id
        LEFT OUTER JOIN
    field_data_field_format format ON sale_date.entity_id = format.entity_id
        LEFT OUTER JOIN
    field_data_field_subjectcategories subjects ON format.entity_id = subjects.entity_id
        AND subjects.field_subjectcategories_tid = '48981'
        LEFT OUTER JOIN
    field_data_field_subjectcategories subjects2 ON subjects.entity_id = subjects2.entity_id
        AND subjects2.field_subjectcategories_tid = '54556'
        LEFT OUTER JOIN
    field_data_field_subjectcategories subjects3 ON subjects2.entity_id = subjects3.entity_id
        AND subjects3.field_subjectcategories_tid = '61091'
WHERE
    ((books.field_related_books_target_id IN ('874271'))
        OR (author_refs.field_author_ref_target_id IN ('874561' , '572716', '874551'))
        OR (((subjects.field_subjectcategories_tid IS NOT NULL)
        AND (subjects2.field_subjectcategories_tid IS NOT NULL))
        OR ((subjects2.field_subjectcategories_tid IS NOT NULL)
        AND (subjects3.field_subjectcategories_tid IS NOT NULL))
        OR ((subjects.field_subjectcategories_tid IS NOT NULL)
        AND (subjects3.field_subjectcategories_tid IS NOT NULL))))
        AND (n.status = '1')
        AND (n.nid NOT IN ('874271'))
        AND (n.type != 'wow')
        AND (n.type != 'event')
        AND (n.type != 'slice')
        AND (sale_date.field_onsaledate_value < CURDATE()
        OR sale_date.field_onsaledate_value IS NULL)
        AND ((format.field_format_tid NOT IN ('2296' , '4986', '1641', '1756'))
        OR (format.field_format_tid IS NULL))
GROUP BY uuid
ORDER BY books.field_related_books_target_id = 874271 DESC , author_refs.field_author_ref_target_id = 874561 DESC , author_refs.field_author_ref_target_id = 572716 DESC , author_refs.field_author_ref_target_id = 874551 DESC , sort_date DESC
LIMIT 36 OFFSET 35;

我要检查的另一件事是你在桌子上的索引。所有条件都应编入索引的人。索引是另一件必须正确完成的事情,你不想过度做,你希望它是合乎逻辑的,订单会有所作为。

我在Explain查询中注意到你有342627,这是你在数据库中的总记录吗?如果是,那意味着它正在进行全表扫描而不尊重索引,我只看到2“条件”和“临时”。