SQL查询长时间执行时间

时间:2015-11-08 13:26:50

标签: mysql drupal

我有一个drupal网站,它有一个特定的SQL查询,大约需要大约15-16秒才能执行,有没有办法减少执行时间?

以下是查询

SELECT node.title AS node_title, node.nid AS nid, field_events_categories_taxonomy_term_data.nid AS field_events_categories_taxonomy_term_data_nid
FROM 
node node
LEFT JOIN field_data_field_events_categories field_data_field_events_categories ON node.nid = field_data_field_events_categories.entity_id AND (field_data_field_events_categories.entity_type = :views_join_condition_? AND field_data_field_events_categories.deleted = :views_join_condition_?)
LEFT JOIN taxonomy_term_data taxonomy_term_data_field_data_field_events_categories ON field_data_field_events_categories.field_events_categories_tid = taxonomy_term_data_field_data_field_events_categories.tid
LEFT JOIN field_data_field_events_categories taxonomy_term_data_field_data_field_events_categories__field_data_field_events_categories ON taxonomy_term_data_field_data_field_events_categories.tid = taxonomy_term_data_field_data_field_events_categories__field_data_field_events_categories.field_events_categories_tid AND (taxonomy_term_data_field_data_field_events_categories__field_data_field_events_categories.entity_type = :views_join_condition_? AND taxonomy_term_data_field_data_field_events_categories__field_data_field_events_categories.deleted = :views_join_condition_?)
LEFT JOIN node field_events_categories_taxonomy_term_data ON taxonomy_term_data_field_data_field_events_categories__field_data_field_events_categories.entity_id = field_events_categories_taxonomy_term_data.nid
INNER JOIN taxonomy_index field_events_categories_taxonomy_term_data__taxonomy_index ON field_events_categories_taxonomy_term_data.nid = field_events_categories_taxonomy_term_data__taxonomy_index.nid
WHERE (( (field_events_categories_taxonomy_term_data.nid != :node_nid OR field_events_categories_taxonomy_term_data.nid IS NULL) AND (field_events_categories_taxonomy_term_data__taxonomy_index.tid = :db_condition_placeholder_?) )AND(( (node.status = :db_condition_placeholder_?) AND (node.type IN  (:db_condition_placeholder_?)) )))
ORDER BY field_events_categories_taxonomy_term_data_nid DESC
LIMIT ? OFFSET ?

2 个答案:

答案 0 :(得分:0)

  1. 您在此查询中使用的每个表都必须包含索引。检查它们并在必要时创建。
  2. 尽量避免使用JOIN,因为它们非常慢。

答案 1 :(得分:0)

如果我没有遗漏某些内容,可能会是这样的

SELECT 
node.title AS node_title, node.nid AS nid, field_events_categories_taxonomy_term_data.nid AS field_events_categories_taxonomy_term_data_nid
FROM 
(select * from node 
        where  
            (node.status = :db_condition_placeholder_?) 
            AND 
            (node.type IN  (:db_condition_placeholder_?))) node 
LEFT JOIN 
    field_data_field_events_categories 
        ON 
            node.nid = field_data_field_events_categories.entity_id 
        AND 
            (field_data_field_events_categories.entity_type = :views_join_condition_? 
            AND 
            field_data_field_events_categories.deleted = :views_join_condition_?)
LEFT JOIN 
    taxonomy_term_data taxonomy_term_data_field_data_field_events_categories 
        ON 
            field_data_field_events_categories.field_events_categories_tid = taxonomy_term_data_field_data_field_events_categories.tid
LEFT JOIN 
    field_data_field_events_categories taxonomy_term_data_field_data_field_events_categories__field_data_field_events_categories 
        ON 
            taxonomy_term_data_field_data_field_events_categories.tid = taxonomy_term_data_field_data_field_events_categories__field_data_field_events_categories.field_events_categories_tid 
        AND 
            (taxonomy_term_data_field_data_field_events_categories__field_data_field_events_categories.entity_type = :views_join_condition_? 
            AND 
            taxonomy_term_data_field_data_field_events_categories__field_data_field_events_categories.deleted = :views_join_condition_?)
LEFT JOIN 
    node field_events_categories_taxonomy_term_data 
        ON 
            taxonomy_term_data_field_data_field_events_categories__field_data_field_events_categories.entity_id = field_events_categories_taxonomy_term_data.nid
INNER JOIN 
    taxonomy_index field_events_categories_taxonomy_term_data__taxonomy_index 
        ON 
            field_events_categories_taxonomy_term_data.nid = field_events_categories_taxonomy_term_data__taxonomy_index.nid
WHERE 
    (    
        (field_events_categories_taxonomy_term_data.nid != :node_nid OR field_events_categories_taxonomy_term_data.nid IS NULL) 
        AND 
        (field_events_categories_taxonomy_term_data__taxonomy_index.tid = :db_condition_placeholder_?)      
    )
ORDER BY field_events_categories_taxonomy_term_data_nid DESC
LIMIT ? OFFSET ?