1

質問があります。次のクエリは実行に 2 ~ 3 秒以上かかりますが、その理由はわかりません。アイテムのリストを持つテーブルと、各アイテムの属性のリストを持つテーブルの 2 つのテーブルがあります。項目テーブルには一意の主キーでインデックスが作成され、属性テーブルには外部キー制約があります。

項目テーブル間の関係は、属性に対して 1 対多です。

他にクエリを高速化する方法がわかりません。アドバイスをいただければ幸いです。

データベースは MYSQL inodb です

EXPLAIN SELECT * FROM eshop_items AS ite WHERE (SELECT attValue FROM eshop_items_attributes WHERE attItemId=ite.ItemId ANd attType=5 AND attValue='20')='20' ORDER BY itemAdded DESC LIMIT 0, 18;

id | select_type        | table                  | type | possible_keys | key       | key_len | ref                    | rows  | Extra
1   PRIMARY               ite                       ALL         NULL        NULL        NULL    NULL                     57179   Using where; Using   filesort
2   DEPENDENT SUBQUERY  eshop_items_attributes      ref     attItemId       attItemId    9      gabriel_new.ite.itemId      5    Using where

インデックス: eshop_items_attributes

Name        Fieldnames  Index Type  Index method
attItemId   attItemId   Normal      BTREE
attType     attType     Normal      BTREE
attValue    attValue    Normal      BTREE

インデックス: eshop_items

Name            Fieldnames      Index Type  Index method
itemCode        itemCode        Unique      BTREE
itemCodeOrig    itemCodeOrig    Unique      BTREE
itemConfig      itemConfig      Normal      BTREE
itemStatus      itemStatus      Normal      BTREE

item_attributes テーブルはキー -> 値のペア テーブルであるため、結合を使用できません。したがって、items_attributes テーブルの各レコードには、多くのアイテム ID が存在する可能性があります

ここにサンプルがあります

item_id    attribute_index   attribute_value
12345      10                true
12345      2                 somevalue
12345      6                 some other value
32456      10                true
32456      11                another value
32456      2                 somevalue

したがって、items_attributes テーブルの複数の行を items テーブルの 1 つの行に結合できないため、結合は機能しません。

attribute_index = to 2 AN attribute_index = 10 であるクエリを作成できません。常に結果が返されません。

:(

4

3 に答える 3

1

クエリを相関から IN に変更し、何が起こるかを確認します。

SELECT * 
  FROM eshop_items AS ite 
 WHERE ItemId IN (
       SELECT attItemId
         FROM eshop_items_attributes 
        WHERE attType=5 
          AND attValue='20')
 ORDER BY itemAdded DESC 
 LIMIT 0, 18 

eshop_items_attributes で btree をビットマップに変更すると、さらに効果が得られます。ただし、注意してください: ビットマップは INSERT/UPDATE に影響します。

于 2013-07-19T00:10:16.123 に答える