我想優化MySQL查詢以加快Drupal視圖。Drupal的MySQL查詢優化
這些表格是節點,term_node和term_data。節點有大約500k行,term_node大約800k,term_data大約300k。
下面的查詢:
SELECT SQL_NO_CACHE DISTINCT(node.nid) AS nid
FROM drupal_node node
LEFT JOIN drupal_term_node term_node ON node.vid = term_node.vid
LEFT JOIN drupal_term_data term_data ON term_node.tid = term_data.tid
WHERE (node.status = 1) AND (node.type in ('foto_foto'))
AND (
(UPPER(term_data.name) LIKE UPPER('%Hanaa%') OR
UPPER(term_data.name) LIKE UPPER('%Bouchaib%')))
GROUP BY nid
LIMIT 0, 10;
這個查詢大約需要11秒展現出來的結果。 連接中涉及的所有列都被編入索引,以及term_data.name。 node.vid,term_node.vid,term_node.tid和term_data.tid是int(10),term_data.name是一個varchar(255)。
奇怪的是,運行構成查詢:
SELECT SQL_NO_CACHE DISTINCT(term_node.tid)
FROM drupal_term_data term_data
LEFT JOIN drupal_term_node term_node ON term_node.tid = term_data.tid
WHERE ((UPPER(term_data.name) LIKE UPPER('%Hanaa%')
OR UPPER(term_data.name) LIKE UPPER('%Bouchaib%')))
group by term_node.tid
和
SELECT SQL_NO_CACHE DISTINCT(node.nid) AS nid
FROM drupal_node node
LEFT JOIN drupal_term_node term_node ON node.vid = term_node.vid
WHERE (node.status = 1) AND (node.type in ('foto_foto'))
GROUP BY nid
需要0.23s的冷杉和0.12S爲上。所以我期望主查詢至少應該在0.5s以下運行。這就像優化器沒有在查詢上運行一樣。
我已經在涉及的表上運行了ANALYZE和OPTIMIZE,下面是EXPLAIN EXTENDED的結果。
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: node
type: range
possible_keys: vid,node_status_type,node_type
key: node_status_type
key_len: 102
ref: NULL
rows: 496217
filtered: 100.00
Extra: Using where
*************************** 2. row ***************************
id: 1
select_type: SIMPLE
table: term_node
type: ref
possible_keys: PRIMARY,vid
key: vid
key_len: 4
ref: drupal_foto.node.vid
rows: 7
filtered: 100.00
Extra: Using where
*************************** 3. row ***************************
id: 1
select_type: SIMPLE
table: term_data
type: eq_ref
possible_keys: PRIMARY
key: PRIMARY
key_len: 4
ref: drupal_foto.term_node.tid
rows: 1
filtered: 100.00
Extra: Using where
3 rows in set, 1 warning (0.00 sec)
警告只提供與代碼1003
[已解決]謝謝戴夫。你的解決方案是完全正確的,查詢已經過優化。 –