这是工作的MySQL查询,是否可以将其转换为单个平面连接查询以提高性能?而不是使用下面的子查询。感谢
SELECT * from catAtt ca LEFT JOIN att a ON ca.attId = a.attId
WHERE ca.catId = 53 AND a.attCatId =
(
SELECT DISTINCT eav.catId FROM ent LEFT JOIN eav ON ent.entId = eav.entId
WHERE ent.key = 'somekey'
)
答案 0 :(得分:2)
我将从简化查询开始。由于left join
子句,where
s都是内连接。此外,子查询中不应该需要select distinct
。
select ca.*, att.*
from catAtt ca join
att a
on ca.attId = a.attId
where ca.catId = 53 and
a.attCatId = (select eav.catId
from ent join
eav
on ent.entId = eav.entId
where ent.key = 'somekey'
);
这建议使用索引:catAtt(catId, attId)
,att(attId, attCatId)
,ent(key, entId)
和eav(entId, catId)
。
您还可以将where
中的子查询移动到from
。我认为这不会对性能产生太大影响。它不相关,因此它运行一次,并返回一个值。
答案 1 :(得分:0)
let disposable = Observable.combineLatest(objectA.asObservable().skip(1),
objectB.asObservable().skip(1))
.throttle(30,
scheduler: ConcurrentDispatchQueueScheduler(qos: .background))
.subscribe(onNext: { [unowned self] _ in
self.update()
})
尝试以上代码。
希望这会对你有所帮助。
答案 2 :(得分:0)
我在sql server上尝试了三种类型的查询。
SELECT * from catAtt ca JOIN att a ON ca.attId = a.attId
WHERE ca.catId = 53 AND a.attCatId =
(
SELECT DISTINCT eav.catId FROM ent LEFT JOIN eav ON ent.entId = eav.entId
WHERE ent.key = 'somekey'
)
SELECT * from catAtt ca
JOIN att a ON ca.attId = a.attId
JOIN
(
SELECT DISTINCT eav.catId FROM ent
LEFT JOIN eav ON ent.entId = eav.entId
WHERE ent.key = 'somekey'
)Z ON a.attCatId = Z.catId
WHERE ca.catId = 53
SELECT * from catAtt ca JOIN att a ON ca.attId = a.attId
WHERE ca.catId = 53 AND
EXISTS
(
SELECT 1 FROM ent LEFT JOIN eav ON ent.entId = eav.entId
WHERE ent.key = 'somekey' AND a.attCatId = eav.catId
)
每个查询的查询成本相同,为33%。
猜猜数据库比我们更聪明。