0
我意識到這是一個很大的查詢,而且顯然有很多事情要做。此查詢的第二行到最後一行比較MD5的散列text
字段。我試過這個而不是比較這些值,但它並沒有減少查詢執行時間。幫助優化查詢 - 運行需要15秒
字段中sob_datas
可以經由sob_datas_to_links
彼此連接和該查詢的值2點形成(每個都具有在sob_datas
517行)進行比較,並拉動鏈接那些不共享相同的值。
SELECT
sob_datas.id,
sob_datas.sob_field_name
FROM sob_datas
WHERE sob_form_id = '.$formId.' AND
EXISTS(SELECT
sob_datas_to_links.id
FROM sob_datas_to_links
INNER JOIN sob_datas AS sub_sob_datas ON
sub_sob_datas.id = sob_datas_to_links.sob_datas_id
INNER JOIN sob_forms ON (
sob_forms.id = sub_sob_datas.sob_form_id AND
sob_forms.is_proof = 0 AND
sob_forms.archived IS NULL
)
WHERE sob_datas_to_links.link_id = (
SELECT
link_id
FROM sob_datas_to_links AS sub_sob_datas_to_links
WHERE sub_sob_datas_to_links.sob_datas_id = sob_datas.id
) AND
sub_sob_datas.sob_field_name = sob_datas.sob_field_name AND
sub_sob_datas.hash != sob_datas.hash
)
。
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY sob_datas ref sob_form_id,sob_form_id_2 sob_form_id [BINARY - 1B] const 563 Using where
2 DEPENDENT SUBQUERY sob_forms ALL PRIMARY NULL NULL NULL 937 Using where
2 DEPENDENT SUBQUERY sub_sob_datas ref PRIMARY,sob_form_id,sob_form_id_2 sob_form_id [BINARY - 3B] summaries_dev.sob_forms.id,summaries_dev.sob_datas... 1 Using where
2 DEPENDENT SUBQUERY sob_datas_to_links ref sob_datas_id sob_datas_id [BINARY - 1B] summaries_dev.sub_sob_datas.id 1 Using where
3 DEPENDENT SUBQUERY sub_sob_datas_to_links ref sob_datas_id sob_datas_id [BINARY - 1B] summaries_dev.sob_datas.id 1 Using where
我們還可以有表定義嗎? – Wiseguy 2011-05-03 20:02:11
刪除子查詢並使用連接http://www.codersrevolution.com/index.cfm/2008/7/31/MySQL-performance-INNER-JOIN-vs-子查詢AND http://stackoverflow.com/questions/141278/subqueries-vs-joins – 2011-05-03 20:03:53
在一個不相關的說明,我希望'$ formId'被保護免受[SQL注入](http://en.wikipedia.org/wiki/SQL_injection) – Wiseguy 2011-05-03 20:04:42