2016-06-11 7 views
4

ここに私が走っているもののログがあります。 intarrayをオフにすると、56msで実行されます。 次に、intarrayを有効にして、同じクエリを実行すると、速度がはるかに遅くなります。PostgresSQLでintarray拡張を有効にすると、パフォーマンスが10倍低下するのはなぜですか?

拡張機能を有効にした後で、テーブルなどを再インデックスする必要がありますか?

test_int=# explain analyze select * from tutor_topic tt1 where tt1.topic_id @> '{5,7,8,9}'; 
                 QUERY PLAN              
----------------------------------------------------------------------------------------------------------------------- 
Seq Scan on tutor_topic tt1 (cost=0.00..29742.00 rows=1000 width=105) (actual time=24.903..1937.480 rows=68 loops=1) 
    Filter: (topic_id @> '{5,7,8,9}'::integer[]) 
    Rows Removed by Filter: 999932 
Planning time: 0.084 ms 
Execution time: 1937.521 ms 
(5 rows) 

Time: 1938.000 ms 
test_int=# DROP EXTENSION intarray;                                DROP EXTENSION 
Time: 10.516 ms 
test_int=# explain analyze select * from tutor_topic tt1 where tt1.topic_id @> '{5,7,8,9}'; 
                  QUERY PLAN                
---------------------------------------------------------------------------------------------------------------------------------- 
Bitmap Heap Scan on tutor_topic tt1 (cost=108.78..487.18 rows=100 width=105) (actual time=55.063..55.138 rows=68 loops=1) 
    Recheck Cond: (topic_id @> '{5,7,8,9}'::integer[]) 
    Heap Blocks: exact=68 
    -> Bitmap Index Scan on message_rdtree_idx (cost=0.00..108.75 rows=100 width=0) (actual time=55.047..55.047 rows=68 loops=1) 
     Index Cond: (topic_id @> '{5,7,8,9}'::integer[]) 
Planning time: 0.196 ms 
Execution time: 55.180 ms 
(7 rows) 

Time: 56.095 ms 
test_int=# 

これはtutor_topicテーブルのスキーマです。私は答えを考え出した

ALTER TABLE tutor_topic ADD FOREIGN KEY (tutor_id) REFERENCES tutor(tutor_id);  
CREATE INDEX ON tutor_topic (tutor_id);            
CREATE INDEX message_rdtree_idx ON tutor_topic USING GIN (topic_id) 

Schema |   Name   | Type | Owner | Table  
--------+---------------------------+-------+--------+------------- 
public | message_rdtree_idx  | index | xxxxxx | tutor_topic 
public | topic_pkey    | index | xxxxxx | topic 
public | tutor_pkey    | index | xxxxxx | tutor 
public | tutor_topic_tutor_id_idx | index | xxxxxx | tutor_topic 
public | tutor_topic_tutor_id_idx1 | index | xxxxxx | tutor_topic 

答えて

2

CREATE TABLE IF NOT EXISTS tutor_topic            
(                     
     tutor_id    INT NOT NULL,            
     topic_id    INT[]              
);   

これら

はインデックスであります!

インデックスを作成するときは、拡張子から関数を指定する必要があります。 CREATE INDEX message_rdtree_idx ON tutor_topic USING GIN(topic_id gin__int_ops);

パフォーマンスが一致しました。 Explain analyzeコールからは、インデックスをまったく使用していないようです。

関連する問題