开发者

Query checking too many rows, how can I reduce?

开发者 https://www.devze.com 2023-02-26 20:34 出处:网络
The following query isn\'t efficient and I need to make it run much faster. idselect_type tabletypepossible_keyskey key_len ref rowsExtra

The following query isn't efficient and I need to make it run much faster.

id  select_type table   type    possible_keys   key key_len ref rows    Extra
1   PRIMARY sob_datas   ALL sob_form_id,sob_form_id_2   NULL    NULL    NULL    365990  Using where
2   DEPENDENT SUBQUERY  sub_sob_datas   ALL sob_form_id,sob_form_id_2   NULL    NULL    开发者_StackOverflow社区NULL    365990  Using where

How can I optimize this query? sob_field_value is a text field

I'm pulling fields in each of these forms that have different values.

SELECT
                sob_datas.id,
                sob_datas.sob_field_name,
                sob_datas.sob_field_value
            FROM sob_datas
            WHERE sob_form_id = '.$proof['SobForm']['id'].' AND
            EXISTS(
                    SELECT
                        sub_sob_datas.id
                    FROM sob_datas AS sub_sob_datas
                    WHERE sub_sob_datas.sob_form_id = '.$original['SobForm']['id'].' AND
                    sub_sob_datas.sob_field_name = sob_datas.sob_field_name AND
                    sub_sob_datas.sob_field_value != sob_datas.sob_field_value
            )

I should also point out, I'm going to update only the changed fields with the new values


It is hard to wrap your head around what is trying to be done here :) So hopefully I caught the point and this is what you are looking for / works. If not let me know and I will try and figure it out.

A bit of test data may help the process along (5-10 rows or so) and what is expected to be fetched from those rows. But here is my shot at it:

    SELECT
            sob_datas.id,
            sob_datas.sob_field_name,
            sob_datas.sob_field_value
    FROM sob_datas sd 
        JOIN sob_datas ssd ON sd.sob_field_name = ssd.sob_field_name
    WHERE sd.sob_form_id = '.$proof['SobForm']['id'].' 
        AND sd.sob_field_value != ssd.sob_datas.sob_field_value
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号