簡體   English   中英

MySQL:選擇條件適用於多行的記錄時超時

[英]MySQL : Timeout when selecting records with conditions that applies to multiple rows

在大型數據庫中,當我組合多個左連接以查找具有匹配條件的多行的對象時,我會立即超時。

我想在不同的表中找到所有具有“紅色”、“怪物引擎”、“閃爍”和“咆哮”屬性的對象。

所以我有一個 MYSQL 表,其中的聯系人對象稱為“對象”:

| id |deleted|
===============
| 1  | false |
| 2  | false |  
| 3  | false |

每個 object 都有存儲在另一個表“properties_relations”中的屬性

|  id  |  objectId  |  propertyId  |  valueString     |
=======================================================
| 12   |   1        |   43         |    Red           |
| 13   |   1        |   44         |    Monster Engine|
| 14   |   1        |   45         |    Blinker       |
| 15   |   1        |   46         |    Roaring       |

現在我想 select 所有具有值為“Red”和值為“Monster Engine”的屬性的對象。

我根據這篇文章MySQL: Select 記錄以適用於多行的條件這樣做

select * from `objects` 
    left join `properties_relations` as `properties_relations` on `objects`.`id` = `properties_relations`.`objectId` 
    left join `properties_relations` as `properties_relations_b` on `objects`.`id` = `properties_relations`.`objectId` 
        
    where (`properties_relations`.`propertyId` = 43 and (`properties_relations`.`valueString` = "Red") 
            and `properties_relations_b`.`propertyId` = 44 and (`properties_relations_b`.`valueString` = "Monster Engine") 
          ) 
           and `objects`.`deleted` = 0

然而,這是他的工作。 但是只要我添加第三個或第四個條件,我就會超時。 我看到我添加的連接越多,行數就會呈指數增長。

不工作的查詢如下所示:

    select * from `objects` 
    left join `properties_relations` as `properties_relations` on `objects`.`id` = `properties_relations`.`objectId` 
    left join `properties_relations` as `properties_relations_b` on `objects`.`id` = `properties_relations`.`objectId` 

    left join `properties_relations` as `properties_relations_c` on `objects`.`id` = `properties_relations`.`objectId` 

    left join `properties_relations` as `properties_relations_d` on `objects`.`id` = `properties_relations`.`objectId` 
        
    where (`properties_relations`.`propertyId` = 43 and 
 (`properties_relations`.`valueString` = "Red") 
            and `properties_relations_b`.`propertyId` = 44 and (`properties_relations_b`.`valueString` = "Monster Engine") 

  and `properties_relations_c`.`propertyId` = 45 and (`properties_relations_c`.`valueString` = "Blinker") 

  and `properties_relations_d`.`propertyId` = 46 and (`properties_relations_d`.`valueString` = "Roaring") 
          ) 
           and `objects`.`deleted` = 0

那么我能做些什么呢?

select * 從離開的對象加入 properties_relations as properties_relations on objects.id = properties_relations.objectId where properties_relations.valueString IN ("Red","Monster Engine") and objects.deleted = 0;

似乎用於objectsproperties_relations之間關系的連接使用了錯誤的名稱。

您正在為properties_relations表使用別名,而實際表名是用於連接條件的表名。

這是我修改的查詢:

select
    *
from
    `objects`
    left join `properties_relations` as `properties_relations` on `objects`.`id` = `properties_relations`.`objectId`
        and `properties_relations`.`propertyId` = 43
    left join `properties_relations` as `properties_relations_b` on `objects`.`id` = `properties_relations_b`.`objectId`
        and `properties_relations_b`.`propertyId` = 44
    left join `properties_relations` as `properties_relations_c` on `objects`.`id` = `properties_relations_c`.`objectId`
        and `properties_relations_c`.`propertyId` = 45
    left join `properties_relations` as `properties_relations_d` on `objects`.`id` = `properties_relations_d`.`objectId`
        and `properties_relations_d`.`propertyId` = 46
where
    (
        and (`properties_relations`.`valueString` = "Red")
        and (`properties_relations_b`.`valueString` = "Monster Engine")
        and (`properties_relations_c`.`valueString` = "Blinker")
        and (`properties_relations_d`.`valueString` = "Roaring")
    )
    and `objects`.`deleted` = 0

在上面的查詢中,我還將propertyId字段從 where 條件移到了 join 條件。 這是為了減少返回的行數,以便查詢可以更快地運行。

您正在使用 AND 操作數而不是 OR。 因為所有這些 valueStrings 不在同一行。 直到永遠,它將返回 NULL 因為所有這些都不在同一行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM