0

我有很多查询,例如以下查询。我如何优化它们-

SELECT DISTINCT
  n.nid AS nid, n.title AS title, n.created AS created, n.changed AS changed,
  n.type AS type, n.status AS status, n.uid AS uid, u.name AS name,
  fit.field_item_topic_tid AS field_item_topic_tid,
  fui.field_upload_item_fid AS field_upload_item_fid,
  fist.field_item_sub_topic_tid AS field_item_sub_topic_tid,
  asid.field_item_asset_id_value AS field_item_asset_id_value,
  fur.field_item_urgent_value AS field_item_urgent_value,
  scrubbing.field_item_scrubbing_value AS field_item_scrubbing_value,
  wsts.field_item_workflow_status_value AS field_item_workflow_status_value,
  wfs.state AS state
FROM 
node n
LEFT OUTER JOIN field_data_field_item_topic fit ON n.nid=fit.entity_id
LEFT OUTER JOIN field_data_field_upload_item fui ON fui.entity_id= n.nid
LEFT OUTER JOIN field_data_field_item_sub_topic fist ON n.nid=fist.entity_id
LEFT OUTER JOIN field_data_field_item_asset_id asid ON n.nid=asid.entity_id
LEFT OUTER JOIN field_data_field_item_urgent fur ON fur.entity_id= n.nid
LEFT OUTER JOIN field_data_field_item_scrubbing scrubbing ON scrubbing.entity_id= n.nid
LEFT OUTER JOIN field_data_field_item_workflow_status wsts ON wsts.entity_id = n.nid
LEFT OUTER JOIN workflow_states wfs ON wfs.sid = wsts.field_item_workflow_status_value
LEFT OUTER JOIN users u ON u.uid = n.uid
WHERE ( (n.type = 'item') )
AND( (wsts.field_item_workflow_status_value IN  ('All', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10')) )
AND(    (fit.field_item_topic_tid IN  ('NULL', '46', '47', '48', '52', '73', '90', '118', '142', '150', '154', '175', '196', '362', '363', '366', '367', '383', '403', '406', '533', '535', '543'))
     OR (fist.field_item_sub_topic_tid IN  ('NULL', '50', '51', '66', '67', '70', '71', '74', '75', '76', '93', '143', '151', '197', '198', '206', '211', '304', '364', '368', '384', '404', '407', '534', '536'))
)
ORDER BY n.changed desc
LIMIT 20 OFFSET 0

EXPLAIN-
id  select_type     table   type    possible_keys   key     key_len     ref     rows    Extra
1   SIMPLE  wsts    range   entity_id,field_item_workflow_status_value  field_item_workflow_status_value    5   NULL    1708    Using where; Using temporary; Using filesort
1   SIMPLE  wfs     eq_ref  PRIMARY     PRIMARY     4   lightening1.wsts.field_item_workflow_status_value   1    
1   SIMPLE  n   eq_ref  PRIMARY,node_type,nid   PRIMARY     4   lightening1.wsts.entity_id  1   Using where
1   SIMPLE  u   eq_ref  PRIMARY     PRIMARY     4   lightening1.n.uid   1    
1   SIMPLE  fit     ref     entity_id   entity_id   4   lightening1.wsts.entity_id  1    
1   SIMPLE  fui     ref     entity_id   entity_id   4   lightening1.wsts.entity_id  1    
1   SIMPLE  fist    ref     entity_id   entity_id   4   lightening1.n.nid   1   Using where
1   SIMPLE  asid    ref     entity_id   entity_id   4   lightening1.wsts.entity_id  1    
1   SIMPLE  fur     ref     entity_id   entity_id   4   lightening1.wsts.entity_id  1    
1   SIMPLE  scrubbing   ref     entity_id   entity_id   4   lightening1.n.nid   1    
4

1 回答 1

0

您没有包含表结构(尤其是索引),所以我只能提供一半的答案。首先,在输出中注意这一行:

1   SIMPLE  wsts    range   entity_id,field_item_workflow_status_value  field_item_workflow_status_value    5   NULL    1708    Using where; Using temporary; Using filesort

这意味着它必须手动查看 1708 条记录。使用了索引等,但保留了 1708 个。这些记录将使用已知速度较慢的文件进行排序。

在查询中

LEFT OUTER JOIN field_data_field_item_workflow_status wsts ON wsts.entity_id = n.nid

根据表 field_data_field_item_workflow_status 进行一些选择。如果你可以在那里添加索引,我认为你必须集中精力。如果索引存在,也许可以更好地描述它?如果有索引,也许字段长度可以缩短?索引随着数据类型的增加而变得越来越大。因此,尽可能使用 TINYINT 而不是 INT。

无论如何你很接近:)

于 2012-06-25T06:54:34.930 回答