我使用了一个查询(由于性能更好,有OR块而不是大的query_int):
SELECT *
FROM products AS p
WHERE ((
p.description && '{791,11705,22921,29515,36965,41021,47203,49459,54602,60499}' :: INT [] OR
p.description && '{62033,73399,75438,76195,78038,79370,82347,85446,85633,92320}' :: INT [] OR
p.description && '{94803,95210,103635,104413,110362,114901,115482,118053,144377,144592}' :: INT [] OR
p.description && '{162314,174062,177685,179717,181979,196348,201003,213047,223458,231169}' :: INT [] OR
p.description && '{237073,242931,244061,245314,246673,257988,260198,260664,263250,263957}' :: INT [] OR
p.description && '{271730,271817,273768,275097,279056,288771,289375,294934,304523,311319}' :: INT [] OR
p.description && '{312113,322217,322889,324356,325774,326251,338673,347436,347467,349276}' :: INT [] OR
p.description && '{352233,353920,356966,364539,364601,385678,394335,396345,404511,409874}' :: INT [] OR
p.description && '{423310,441817,445483,453401,460896,461693,462106,466680,474877,478839}' :: INT [] OR
p.description && '{484941,489442,489946,490523,490883,494463,496539,497198,503385,507944}' :: INT [] OR
p.description && '{512199,529380,529715,533907,534796,547913,551711,570389,574545,582169}' :: INT [] OR
p.description && '{586486,591756,597798,598733,601138,604477,606417,607399,609051,612431}' :: INT [] OR
p.description && '{627519,632265,642944,644396,652624,657323,660675,674114,680471,694340}' :: INT [] OR
p.description && '{696385,9709,41582,114552,279064,325285,342008,351284,628528,653153}' :: INT [] OR
p.description && '{656846,4316,10946,22093,24018,27702,33375,46878,65895,77003}' :: INT [] OR
p.description && '{95393,114783,118457,141371,150094,152895,162732,163157,163367,203877}' :: INT [] OR
p.description && '{210856,217923,241203,249221,253779,267201,272252,273653,278116,293802}' :: INT [] OR
p.description && '{295701,300599,302252,326873,358918,363728,383700,384585,424374,438758}' :: INT [] OR
p.description && '{447823,462266,470243,478988,480508,505702,509906,515737,564044,564152}' :: INT [] OR
p.description && '{571872,600824,608280,613624,646110,683957,689644,690990,47933,72118}' :: INT [] OR
p.description && '{254118,606448,642630,24354,133966,135193,286475,323715,343992,498832}' :: INT [] OR
p.description && '{499785,513456,618107,23542,27702,36048,69975,77818,98085,152727}' :: INT [] OR
p.description && '{171208,174406,186215,232375,277012,332626,369601,377201,503875,515732}' :: INT [] OR
p.description && '{519901,554731,586945,607591,20419,49442,69838,78897,83455,113858}' :: INT [] OR
p.description && '{134734,177867,193400,198939,201042,223104,242510,251089,270295,293557}' :: INT [] OR
p.description && '{328628,331289,396160,412975,458708,467312,473574,479129,507406,518668}' :: INT [] OR
p.description && '{522569,535130,611787,622383,635147,651673,664061,673324,691806,9709}' :: INT [] OR
p.description && '{41582,114552,279064,325285,342008,351284,628528,653153,656846,4316}' :: INT [] OR
p.description && '{10946,22093,24018,27702,33375,46878,65895,77003,95393,114783}' :: INT [] OR
p.description && '{118457,141371,150094,152895,162732,163157,163367,203877,210856,217923}' :: INT [] OR
p.description && '{241203,249221,253779,267201,272252,273653,278116,293802,295701,300599}' :: INT [] OR
p.description && '{302252,326873,358918,363728,383700,384585,424374,438758,447823,462266}' :: INT [] OR
p.description && '{470243,478988,480508,505702,509906,515737,564044,564152,571872,600824}' :: INT [] OR
p.description && '{608280,613624,646110,683957,689644,690990,427652,3396,5468,7071}' :: INT [] OR
p.description && '{11337,24554,28045,34050,34411,40342,49553,53281,56183,59036}' :: INT [] OR
p.description && '{59323,60385,67818,73546,78048,78495,83428,94363,96511,97090}' :: INT [] OR
p.description && '{105677,106812,110743,112667,116389,116389,121806,126249,126795,127471}' :: INT [] OR
p.description && '{129954,133423,134354,135154,138988,142529,147237,147802,149142,159859}' :: INT [] OR
p.description && '{160280,162042,162081,164552,165180,168690,171724,178865,186808,189770}' :: INT [] OR
p.description && '{191313,201193,203208,205776,212311,214999,217923,223830,223830,227350}' :: INT [] OR
p.description && '{230855,234335,235881,241681,244872,246560,246775,265180,270338,271211}' :: INT [] OR
p.description && '{274693,276959,279186,279393,289182,293996,294960,296449,298136,302773}' :: INT [] OR
p.description && '{302773,303951,305471,325206,330296,332384,336039,338215,339423,339516}' :: INT [] OR
p.description && '{341659,348117,349369,351365,351455,358337,371858,372312,373674,377031}' :: INT [] OR
p.description && '{377740,379629,384052,387662,395829,400033,400033,400840,402101,403762}' :: INT [] OR
p.description && '{404439,405690,408812,425888,428446,430709,432931,440178,445270,447227}' :: INT [] OR
p.description && '{448502,450834,453581,455663,464793,468955,471005,473375,475847,479237}' :: INT [] OR
p.description && '{480483,480483,489445,490632,491125,498006,503936,504569,508429,510547}' :: INT [] OR
p.description && '{516074,523310,530674,549031,551605,553582,554012,564755,567326,568098}' :: INT [] OR
p.description && '{570145,571846,576153,576696,578487,582018,582302,586374,587243,588502}' :: INT [] OR
p.description && '{589031,589574,595438,595678,597467,597556,605129,605494,611735,612377}' :: INT [] OR
p.description && '{612467,615794,617161,631021,632541,633465,640483,642678,664766,677814}' :: INT [] OR
p.description && '{678893,679507,689935,691793,693498,701870,44857,47744,149194,635437}' :: INT [] OR
p.description && '{678341,687444,9709,41582,114552,279064,325285,342008,351284,628528}' :: INT [] OR
p.description && '{653153,656846,4316,10946,22093,24018,27702,33375,46878,65895}' :: INT [] OR
p.description && '{77003,95393,114783,118457,141371,150094,152895,162732,163157,163367}' :: INT [] OR
p.description && '{203877,210856,217923,241203,249221,253779,267201,272252,273653,278116}' :: INT [] OR
p.description && '{293802,295701,300599,302252,326873,358918,363728,383700,384585,424374}' :: INT [] OR
p.description && '{438758,447823,462266,470243,478988,480508,505702,509906,515737,564044}' :: INT [] OR
p.description && '{564152,571872,600824,608280,613624,646110,683957,689644,690990,10651}' :: INT [] OR
p.description && '{19421,20884,26683,34732,74659,218430,228747,234480,319665,572147}' :: INT [] OR
p.description && '{669237,1421,3161,12427,19721,28154,31160,35426,37289,38423}' :: INT [] OR
p.description && '{42631,54527,57861,72521,73760,81702,83410,83651,91361,92351}' :: INT [] OR
p.description && '{93268,98744,103015,104011,112667,115648,121362,121362,123556,137986}' :: INT [] OR
p.description && '{140833,156239,157732,164100,164326,169961,183304,183641,185167,201635}' :: INT [] OR
p.description && '{206847,213795,214047,215641,218148,229596,242246,245073,250061,252552}' :: INT [] OR
p.description && '{253313,264925,265020,265469,267188,270462,272294,273558,274989,275584}' :: INT [] OR
p.description && '{277219,280289,287867,294551,301174,304102,312987,314320,316688,320693}' :: INT [] OR
p.description && '{323204,324561,324561,325586,330911,337556,341611,342303,343187,345029}' :: INT [] OR
p.description && '{347527,348614,357527,357574,359225,370530,376234,379821,393586,394888}' :: INT [] OR
p.description && '{398613,399253,410485,412016,416022,434669,445023,451774,453222,456413}' :: INT [] OR
p.description && '{458007,460434,464534,466956,471096,474085,477351,480873,488478,493273}' :: INT [] OR
p.description && '{499989,507214,509932,512565,518521,523187,525811,539965,542990,544503}' :: INT [] OR
p.description && '{549556,557349,558828,559533,560062,563538,576377,578254,581474,586154}' :: INT [] OR
p.description && '{590178,592106,592607,595291,602985,606274,608227,612767,630647,638911}' :: INT [] OR
p.description && '{651972,654823,655993,664629,670034,670034,674441,682640,682978,685971}' :: INT [] OR
p.description && '{691824,696822,702920,330909,9709,41582,114552,279064,325285,342008}' :: INT [] OR
p.description && '{351284,628528,653153,656846,4316,10946,22093,24018,27702,33375}' :: INT [] OR
p.description && '{46878,65895,77003,95393,114783,118457,141371,150094,152895,162732}' :: INT [] OR
p.description && '{163157,163367,203877,210856,217923,241203,249221,253779,267201,272252}' :: INT [] OR
p.description && '{273653,278116,293802,295701,300599,302252,326873,358918,363728,383700}' :: INT [] OR
p.description && '{384585,424374,438758,447823,462266,470243,478988,480508,505702,509906}' :: INT [] OR
p.description && '{515737,564044,564152,571872,600824,608280,613624,646110,683957,689644}' :: INT [] OR
p.description && '{690990}' :: INT []));
查询的EXPLAIN ANALYZE:
Gather (cost=1000.00..89666.80 rows=207290 width=139) (actual time=2.431..6942.170 rows=76101 loops=1)
Workers Planned: 2
Workers Launched: 2
-> Parallel Seq Scan on products p (cost=0.00..67937.80 rows=86371 width=139) (actual time=0.586..6853.961 rows=25367 loops=3)
Filter: ((description && '{791,11705,22921,29515,36965,41021,47203,49459,54602,60499}'::integer[]) OR (description && '{62033,73399,75438,76195,78038,79370,82347,85446,85633,92320}'::integer[]) OR (description && '{94803,95210,103635,104413,110362,114901,115482,118053,144377,144592}'::integer[]) OR ...
Rows Removed by Filter: 61185
Planning time: 1.918 ms
Execution time: 6950.584 ms
和EXPLAIN ANALYZE,ENABLE_SEQSCAN = FALSE;选项:
Gather (cost=19139.18..107805.98 rows=207290 width=139) (actual time=512.903..571.331 rows=76101 loops=1)
Workers Planned: 2
Workers Launched: 2
-> Parallel Bitmap Heap Scan on products p (cost=18139.18..86076.98 rows=86371 width=139) (actual time=502.441..517.985 rows=25367 loops=3)
Recheck Cond: ((description && '{791,11705,22921,29515,36965,41021,47203,49459,54602,60499}'::integer[]) OR (description && '{62033,73399,75438,76195,78038,79370,82347,85446,85633,92320}'::integer[]) OR (description && '{94803,95210,103635,104413,110362,114901,115482,118053,144377,144592}'::integer[]) OR (description && '{162314,174062,177685,179717,181979,196348,201003,213047,223458,231169}'::integer[]) OR (description && '{237073,242931,244061,245314,246673,257988,260198,260664,263250,263957}'::integer[]) OR (description && '{271730,271817,273768,275097,279056,288771,289375,294934,304523,311319}'::integer[]) OR (description && '{312113,322217,322889,324356,325774,326251,338673,347436,347467,349276}'::integer[]) OR (description && '{352233,353920,356966,364539,364601,385678,394335,396345,404511,409874}'::integer[]) OR (description && '{423310,441817,445483,453401,460896,461693,462106,466680,474877,478839}'::integer[]) OR (description && '{484941,489442,489946,490523,490883,494463,496539,497198,503385,507944}'::integer[]) OR (description && '{512199,529380,529715,533907,534796,547913,551711,570389,574545,582169}'::integer[]) OR (description && '{586486,591756,597798,598733,601138,604477,606417,607399,609051,612431}'::integer[]) OR (description && '{627519,632265,642944,644396,652624,657323,660675,674114,680471,694340}'::integer[]) OR (description && '{696385,9709,41582,114552,279064,325285,342008,351284,628528,653153}'::integer[]) OR (description && '{656846,4316,10946,22093,24018,27702,33375,46878,65895,77003}'::integer[]) OR (description && '{95393,114783,118457,141371,150094,152895,162732,163157,163367,203877}'::integer[]) OR (description && '{210856,217923,241203,249221,253779,267201,272252,273653,278116,293802}'::integer[]) OR (description && '{295701,300599,302252,326873,358918,363728,383700,384585,424374,438758}'::integer[]) OR (description && '{447823,462266,470243,478988,480508,505702,509906,515737,564044,564152}'::integer[]) OR ...)
Heap Blocks: exact=91
-> BitmapOr (cost=18139.18..18139.18 rows=259656 width=0) (actual time=504.150..504.150 rows=0 loops=1)
-> Bitmap Index Scan on products_gin_index (cost=0.00..157.01 rows=3869 width=0) (actual time=39.016..39.016 rows=0 loops=1)
Index Cond: (description && '{791,11705,22921,29515,36965,41021,47203,49459,54602,60499}'::integer[])
-> Bitmap Index Scan on products_gin_index (cost=0.00..157.01 rows=3869 width=0) (actual time=10.940..10.940 rows=0 loops=1)
Index Cond: (description && '{62033,73399,75438,76195,78038,79370,82347,85446,85633,92320}'::integer[])
-> Bitmap Index Scan on products_gin_index (cost=0.00..157.01 rows=3869 width=0) (actual time=10.610..10.610 rows=0 loops=1)
Index Cond: (description && '{94803,95210,103635,104413,110362,114901,115482,118053,144377,144592}'::integer[])
-> Bitmap Index Scan on products_gin_index (cost=0.00..157.01 rows=3869 width=0) (actual time=11.452..11.452 rows=0 loops=1)
Index Cond: (description && '{162314,174062,177685,179717,181979,196348,201003,213047,223458,231169}'::integer[])
-> Bitmap Index Scan on products_gin_index (cost=0.00..157.01 rows=3869 width=0) (actual time=3.429..3.429 rows=0 loops=1)
Index Cond: (description && '{237073,242931,244061,245314,246673,257988,260198,260664,263250,263957}'::integer[])
-> Bitmap Index Scan on products_gin_index (cost=0.00..157.01 rows=3869 width=0) (actual time=2.290..2.290 rows=0 loops=1)
...
Planning time: 1.378 ms
Execution time: 581.867 ms
正如您所见,尽管计划成本,第二次查询的速度要快得多。 该查询是较大查询(作为CTE)的一部分,因此我无法对整个查询禁用顺序扫描。
有一个产品专栏:
prepared_description INT4[]
它的GIN指数:
CREATE INDEX products_gin_index ON products USING GIN(description gin__int_ops);
表中有~300 000行,描述数组
平均为21.6 ints有没有办法强制在此查询中使用索引?