尝试解决此问题:Sql: choose all baskets containing a set of particular items
换句话说,有一张表:
tbl_basket_item
--
basketId itemId
1 2
1 3
1 4
2 3
2 4
3 2
3 4
itemId 已编入索引。
如果我在 itemId = 2 上执行扫描,我会得到:
SELECT basketId FROM tbl_basket_item WHERE itemId = 2
1
3
如果我在 itemId = 4 上执行扫描,我会得到:
SELECT basketId FROM tbl_basket_item WHERE itemId = 4
1
2
3
我现在可以将这两个扫描相交:
SELECT basketId FROM tbl_basket WHERE
basketId IN (SELECT basketId FROM tbl_basket_item WHERE itemId = 2) AND
basketId IN (SELECT basketId FROM tbl_basket_item WHERE itemId = 4)
1
3
使用一些PostgeSQL高级索引技术,比如位图索引?
答案 0 :(得分:1)
通过在INTERSECT
上执行标准SELECT
,您可以有效地获得“交叉索引扫描”(无论有效是什么):
SELECT basketId FROM tbl_basket_item WHERE itemId = 2
INTERSECT
SELECT basketId FROM tbl_basket_item WHERE itemId = 4;