我有一个包含四个表(项目,属性,快照和值)的MySQL数据库。基本上,表值包含给定特定日期(快照)的项的某个属性的值。
表值包含以下列
现在我想为其他属性具有特定值的所有项目获取某个属性(-id)的值:
SELECT * FROM
(SELECT * FROM remian.remian_propertyvalue WHERE
FK_ItemId IN (SELECT FK_ItemId FROM (SELECT * FROM remian.remian_propertyvalue WHERE FK_PropertyId=1) AS trackerentries WHERE trackerentries.Value IN (12,11) AND
FK_ItemId IN (SELECT FK_ItemId FROM (SELECT * FROM remian.remian_propertyvalue WHERE FK_PropertyId=12) AS statustable WHERE statustable.Value NOT IN ("Rejected","Closed")) AND
FK_ItemId IN (SELECT FK_ItemId FROM (SELECT * FROM remian.remian_propertyvalue WHERE FK_PropertyId=2) AS projectentries WHERE projectentries.Value IN (73,74)))) AS queried
WHERE queried.FK_PropertyId=12 AND FK_SnapshotId = 1;
此查询确实有效但速度很慢(5000个条目为80秒)。有没有更有效的方法来做到这一点?
答案 0 :(得分:0)
EAV模式非常混乱。使用IN ( SELECT ... )
会使情况更糟且更慢。
您需要尝试在没有子查询的情况下执行此操作,只需JOINs
,例如
SELECT *
FROM remian_propertyvalue AS a
JOIN remian_propertyvalue AS b USING(FK_ItemId)
JOIN remian_propertyvalue AS c USING(FK_ItemId)
WHERE a.FK_PropertyId = 1 AND a.value IN (12,11)
AND b.FK_PropertyId = 12 AND b.value IN ("Rejected","Closed")
AND c.FK_PropertyId = 2 AND c.value IN (73,74)
AND a.FK_PropertyId = 12 AND a.FK_SnapshotId = 1;
此外,“属性”表上没有AUTO_INCREMENT
。取而代之的是
PRIMARY KEY(itemId, propertyId),
INDEX(propertyId, value)
这些索引可能存在问题;我真的需要SHOW CREATE TABLE
来帮忙。
更多: