次のテーブル構造を使用します。
アイテム (~20,000 レコード)
- item_id
プロパティ (~30 レコード)
- property_id
Item_properties (~40,000 レコード)
- ID
- property_id
- item_id
ユーザーは、items
テーブル自体のフィールドの数によってアイテムをフィルター処理することを選択できます。またproperties
、アイテムに必要な任意の数を選択することもできます。検索では、1 つのプロパティだけでなく、すべてのプロパティを持つアイテムを選択する必要があります。現在使用しているフォーマット
SELECT item.field...
FROM items
INNER JOIN item_properties AS ip1 ON ip1.item_id=item.item_id and ip1.property_id=3
INNER JOIN item_properties AS ip2 ON ip2.item_id=item.item_id and ip2.property_id=4
INNER JOIN item_properties AS ip3 ON ip3.item_id=item.item_id and ip3.property_id=5
INNER JOIN item_properties AS ip4 ON ip4.item_id=item.item_id and ip4.property_id=6
etc...
WHERE item.something_else='words'
GROUP BY item_id
JOINではなくWHEREだけで検索を指定する方法として、私も試しました
SELECT item.field...
FROM items
WHERE item.something_else='words'
and item_id IN (select item_id from item_properties where property_id=3)
and item_id IN (select item_id from item_properties where property_id=4)
and item_id IN (select item_id from item_properties where property_id=5)
and item_id IN (select item_id from item_properties where property_id=6)
etc...
ただし、このアプローチは、どちらかといえば、セットのクエリにさらに時間がかかるように見えました。約 4 つのプロパティを選択すると、クエリ時間は約 4 ~ 5 秒になり、さらに多くのクエリが強制終了されるか、MySQL サーバーが完全にダウンする傾向があります。
私の知る限り、すべての _id フィールドは各テーブルでインデックス化されており、それぞれのテーブルの主キーでもあります。
クエリを改善する方法はありますか、またはクエリできるオプションの数を制限する必要があるのでしょうか?