这是两个sqlite查询:
SELECT * FROM items JOIN licenses ON items.id=licenses.id OR items.type=licenses.type;
此查询不使用OR
,它使用UNION
SELECT * FROM items JOIN licenses ON items.id=licenses.id UNION SELECT * FROM items JOIN licenses ON items.type=licenses.type;
假设我在 id 上的 licenses 表中有一个索引,并且在 type 上的 licenses 表中有一个索引,第一个使用 an 的查询不OR
应该只慢一点吗?
我看到第一个查询比 Sqlite 中的第二个查询慢大约 20 倍,这是什么原因?
对于第一个查询,我希望内部计划看起来像这样:
- 对于
items
表中的每一行: id
从表的列中获取值并使用它来查找表items
中的所有行,调用该组匹配行 A。licenses
id
type
从表的列中获取值并使用它来查找表items
中的所有行,调用该组匹配行 A'。合并 A 和 A' 并消除任何重复的行。将结果添加到结果行列表中licenses
type