[pgsql-jp: 41994] array_to_json 関数によるSQL実行時間

toshio_uchiyama @ mirror.ocn.ne.jp toshio_uchiyama @ mirror.ocn.ne.jp
2018年 12月 16日 (日) 01:44:08 UTC


JPUG 各位

 お世話にになります。内山と申します。
カンファレンスではお世話になりました。
ところで、SQL 実行結果に array_to_json 関数を
適用すると実行時間が、10倍以上になるのですが、
これを早くする方法はないでしょうか。
 実際に、SQL を実行した、explain ( analyze, buffers )
の結果を掲載します。

〇array_to_json 関数なし
moovle=# explain ( analyze, buffers ) SELECT id, video, track, stime,
content as c, 
theme FROM subtitled WHERE content &@~ 'algorithm search' 
ORDER BY vc DESC OFFSET 0 LIMIT 70;
 
QU
ERY PLAN

----------------------------------------------------------------------------
----
Limit  (cost=37767.56..37767.73 rows=70 width=1077) 
    (actual time=50.507..50.542 rows=70 loops=1)
   Buffers: shared hit=2705
   ->  Sort  (cost=37767.56..37855.40 rows=35137 width=1077) 
             (actual time=50.505..50.528 rows=70 loops=1)
         Sort Key: vc DESC
         Sort Method: top-N heapsort  Memory: 107kB
         Buffers: shared hit=2705
         ->  Index Scan using subtitled2_pgroonga_content_idx on subtitled  
(cost=0.00..36515.05 rows=35137 width=1077) 
(actual time=41.746..48.214 rows=2769 loops=1)
               Index Cond: (content &@~ 'algorithm search'::text)
               Buffers: shared hit=2705
 Planning time: 1.354 ms
 Execution time: 51.315 ms
(11 行)

〇array_to_json 関数あり
moovle=# explain ( analyze, buffers ) SELECT id, video, track, stime, 
array_to_json( content ) as c, 
theme FROM subtitled WHERE content &@~ 'algorithm search' 
ORDER BY vc DESC OFFSET 0 LIMIT 70;
 
QU
ERY PLAN

----------------------------------------------------------------------------
----
Limit  (cost=37855.40..37855.58 rows=70 width=751) 
       (actual time=966.402..966.689 rows=70 loops=1)
   Buffers: shared hit=21517
   ->  Sort  (cost=37855.40..37943.24 rows=35137 width=751) 
             (actual time=966.401..966.682 rows=70 loops=1)
         Sort Key: vc DESC
         Sort Method: top-N heapsort  Memory: 2996kB
         Buffers: shared hit=21517
         ->  Index Scan using subtitled2_pgroonga_content_idx on subtitled  
(cost=0.00..36602.89 rows=35137 width=751) 
(actual time=45.692..951.021 rows=2769 loops=1)
               Index Cond: (content &@~ 'algorithm search'::text)
               Buffers: shared hit=21517
 Planning time: 1.349 ms
 Execution time: 967.509 ms
(11 行)

よろしくお願いします。



pgsql-jp メーリングリストの案内