帮助优化查询 - 运行需要15秒

时间:2011-05-03 19:55:46

标签: mysql query-optimization

我意识到这是一个相当大的查询,显然有很多事情要发生。此查询的第2行到最后一行比较了text字段的MD5哈希值。我试过这个而不是比较值,但它根本没有减少查询执行时间。

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

1 个答案:

答案 0 :(得分:0)

我的脑袋即将爆炸(与这个问题并不完全相关,但与数据库有关),但我希望这会让你知道我在想什么。尝试一个自我加入(但不是这个,我确定它不会像我一样工作,因为我不能让自己试图找出你的表格模式和现在所有的爵士乐。

SELECT DISTINCT sob_datas.id, sob_datas.sob_field_name 
FROM sob_datas as A1 
  INNER JOIN sob_datas as A2 
    ON A1.sob_forms_ID = A2.sob_forms_ID
  JOIN sob_forms 
    ON A1.sob_forms_ID = sob_forms.id
    AND A2.sob_forms_ID = sob_forms.id
WHERE A1.hash != A2.hash 
  AND sob_forms.is_proof = 0
  AND sob_forms.archived IS NULL;