我有一个名为“元”的表,有 220 万条记录。该表包含 3 列:productId、key 和 value。键和值是包含产品元描述的两列。
以下查询需要 2.6 秒并返回 676 个结果(PostgreSQL 8.4.13,CentOS 6.4 64 位)。此查询用于从某个过滤器(大小)中检索所有可能的元描述,其中用户已经过滤了其他两个过滤器(年份和来源)。
我尝试了这个主题的数组解决方案,但它只会让情况变得更糟:PostgreSQL IN operator with subquery bad performance
这两个子查询非常快(75 毫秒和 178 毫秒),但是将它们组合起来会导致性能问题。有没有办法重写查询?
这是当前查询:
SELECT DISTINCT ON(value) key, value
FROM "meta"
WHERE key = 'size'
AND "productId" IN (SELECT "productId"
FROM "meta"
WHERE "value" = 'ibm'
AND "key" = 'source' )
AND "productId" IN (SELECT "productId"
FROM "meta"
WHERE "value" >= '1920'
AND "value" <= '2010'
AND "key" = 'year' )
ORDER BY value
使用以下解释分析:
Unique (cost=38829.46..38843.19 rows=564 width=15) (actual time=2674.474..2690.856 rows=676 loops=1)
-> Sort (cost=38829.46..38836.32 rows=2745 width=15) (actual time=2674.471..2681.333 rows=66939 loops=1)
Sort Key: public."meta".value
Sort Method: quicksort Memory: 8302kB
-> Hash Join (cost=32075.86..38672.69 rows=2745 width=15) (actual time=472.158..2472.002 rows=66939 loops=1)
Hash Cond: (public."meta"."originalId" = public."meta"."productId")
-> Nested Loop (cost=15079.41..21563.33 rows=13109 width=23) (actual time=113.873..1013.113 rows=104307 loops=1)
-> HashAggregate (cost=15079.41..15089.21 rows=980 width=4) (actual time=113.802..163.805 rows=105204 loops=1)
-> Bitmap Heap Scan on "meta" (cost=315.39..15051.42 rows=11196 width=4) (actual time=24.540..68.237 rows=105204 loops=1)
Recheck Cond: (((key)::text = 'source'::text) AND ((value)::text = 'KADASTER_WOII_RAF_USAAF'::text))
-> Bitmap Index Scan on "productMetadataKeyValueIndex" (cost=0.00..312.60 rows=11196 width=0) (actual time=23.506..23.506 rows=105204 loops=1)
Index Cond: (((key)::text = 'source'::text) AND ((value)::text = 'ibm'::text))
-> Index Scan using "idx_productId" on "meta" (cost=0.00..6.59 rows=1 width=19) (actual time=0.006..0.008 rows=1 loops=105204)
Index Cond: (public."meta"."productId" = public."meta"."productId")
Filter: ((public."meta".key)::text = 'size'::text)
-> Hash (cost=16954.58..16954.58 rows=3350 width=4) (actual time=358.214..358.214 rows=184571 loops=1)
-> HashAggregate (cost=16921.08..16954.58 rows=3350 width=4) (actual time=258.149..319.154 rows=184571 loops=1)
-> Bitmap Heap Scan on "meta" (cost=1172.62..16825.39 rows=38273 width=4) (actual time=86.725..167.110 rows=184571 loops=1)
Recheck Cond: (((key)::text = 'year'::text) AND ((value)::text >= '1920'::text) AND ((value)::text <= '2010'::text))
-> Bitmap Index Scan on "productMetadataKeyIndex" (cost=0.00..1163.05 rows=38273 width=0) (actual time=83.992..83.992 rows=184571 loops=1)
Index Cond: (((key)::text = 'year'::text) AND ((value)::text >= '1920'::text) AND ((value)::text <= '2010'::text))
Total runtime: 2696.276 ms
定义的索引:
idx_productId CREATE INDEX "idx_productId" ON "meta" USING btree ("productId")
productMetaUnique_id CREATE UNIQUE INDEX "productMetaUnique_id" ON "meta" USING btree ("productId", key)
productMetadataKeyIndex CREATE INDEX "productMetadataKeyIndex" ON "meta" USING btree (key)
productMetadataKeyValueIndex CREATE INDEX "productMetadataKeyValueIndex" ON "meta" USING btree (key, value)