2

私はpostgre sqlに非常に慣れていません。現在、私の会社では、開発ゲームの1つがクエリであり、それを調整するように言われています。

SELECT room_type_name, 
       Max(total), 
       Max(d1), 
       Max(d2), 
       Max(d3), 
       Max(d4), 
       Max(d5), 
       Max(d6), 
       Max(d7) 
FROM   (SELECT DISTINCT room_type_name, 
                        Sum(total_count) AS total, 
                        Sum(avail_count) 
                        || ' '           AS D1, 
                        '0'              AS D2, 
                        '0'              AS D3, 
                        '0'              AS D4, 
                        '0'              AS D5, 
                        '0'              AS D6, 
                        '0'              AS D7 
        FROM   tmp_avail_count_dtls 
        WHERE  To_char(week_date, 'DD-MON-YYYY') = To_char(To_date('20-03-     2013', 
        'DD-MM-YYYY'), 
           'DD-MON-YYYY') 
               AND user_id = 'APHYDRSA00010' 
        GROUP  BY room_type_name 
        UNION 
        SELECT DISTINCT room_type_name, 
                        Sum(total_count) AS total, 
                        '0'              AS D1, 
                        Sum(avail_count) 
                        || ' '           AS D2, 
                        '0'              AS D3, 
                        '0'              AS D4, 
                        '0'              AS D5, 
                        '0'              AS D6, 
                        '0'              AS D7 
        FROM   tmp_avail_count_dtls 
        WHERE  To_char(week_date, 'DD-MON-YYYY') = To_char( 
                      To_date('20-03-2013', 'DD-MM-YYYY') + 1, 'DD-MON-YYYY') 
               AND user_id = 'APHYDRSA00010' 
        GROUP  BY room_type_name 
        UNION 
        SELECT DISTINCT room_type_name, 
                        Sum(total_count) AS total, 
                        '0'              AS D1, 
                        '0'              AS D2, 
                        Sum(avail_count) 
                        || ' '           AS D3, 
                        '0'              AS D4, 
                        '0'              AS D5, 
                        '0'              AS D6, 
                        '0'              AS D7 
        FROM   tmp_avail_count_dtls 
        WHERE  To_char(week_date, 'DD-MON-YYYY') = To_char( 
                      To_date('20-03-2013', 'DD-MM-YYYY') + 2, 'DD-MON-YYYY') 
               AND user_id = 'APHYDRSA0     0010' 
        GROUP  BY room_type_name 
        UNION 
        SELECT DISTINCT room_type_name, 
                        Sum(total_count) AS total, 
                        '0'              AS D1, 
                        '0'              AS D2, 
                        '0'              AS D3, 
                        Sum(avail_count) 
                        || ' '           AS D4, 
                        '0'              AS D5, 
                        '0'              AS D6, 
                        '0'              AS D7 
        FROM   tmp_avail_count_dtls 
        WHERE  To_char(week_date, 'DD-MON-     YYYY') = To_char(To_date('20-03-2013', 
                             'DD-MM-YYYY') + 3, 
                            'DD-MON-YYYY') 
               AND user_id = 'APHYDRSA00010' 
        GROUP  BY room_type_name 
        UNION 
        SELECT DISTINCT room_type_name, 
                        Sum (total_count) AS total, 
                        '0'               AS D1, 
                        '0'               AS D2, 
                        '0'               AS D3, 
                        '0'               AS D4, 
                        Sum(avail_count) 
                        || ' '            AS D5, 
                        '0'               AS D6, 
                        '0'               AS D7 
        FROM   tmp_avail_count_dtls 
        WHERE  To_char(week_date, 'DD-MON-YYYY') = To_char( 
                      To_date('20-03-2013', 'DD-MM-YYYY') + 4, 'DD-MON-YYYY') 
               AND user_id = 'APHYDRSA00010' 
        GROUP  BY room_type_name 
        UNION 
        SELECT DISTINCT room_type_name, 
                        Sum(total_count) AS total, 
                        '0'              AS D1, 
                        '0'              AS D2, 
                        '0'              AS D3, 
                        '0'              AS D4, 
                        '0'              AS D5, 
                        Sum(avail_count) 
                        || ' '           AS D6, 
                        '0'              AS D7 
        FROM   tmp_avail_count_dtls 
        WHERE  To_char(week_date, 'DD-MON-YYYY') = To_char( 
                      To_date('20-03-2013', 'DD-MM-YYYY') + 5, 'DD-MON-     YYYY') 
               AND user_id = 'APHYDRSA00010' 
        GROUP  BY room_type_name 
        UNION 
        SELECT DISTINCT room_type_name, 
                        Sum(total_count) AS total, 
                        '0'              AS D1, 
                        '0'              AS D2, 
                        '0'              AS D3, 
                        '0'              AS D4, 
                        '0'              AS D5, 
                        '0'              AS D6, 
                        Sum(avail_count) 
                        || ' '           AS D7 
        FROM   tmp_avail_count_dtls 
        WHERE  To_char(week_date, 'DD-MON-YYYY') = To_char( 
                      To_date('20-03-2013', 'DD-MM-YYYY') + 6, 'DD-MON-YYYY') 
               AND user_id = 'APHYDRSA00010' 
        GROUP  BY room_type_name) AS a 
GROUP  BY room_type_name

上記のクエリの説明計画から私が観察したことは、上記のクエリはインデックスを使用していないということです。だから私は関数を作成してインデックスを付けたい

to_char(week_date,'DD-MON-YYYY') 

しかし、私がコマンドを与えたとき

create index week_date_index 
on TMP_AVAIL_COUNT_DTLS(TO_CHAR(week_date,'DD-MON-YYYY'))

それは言う

エラー: インデックス式の関数は IMMUTABLE とマークする必要があります

私もSQLがとても苦手です。上記のクエリの関数ベースのインデックスを作成する方法を教えてください。私の仕事を救うのを手伝ってください。データベースで直接ヒットできるように、クエリを教えてください。他に何か必要かどうか聞いてください。

table definition :  week_date date,
  division_code character varying(3),
  unit_code character varying(5),
  status_month character varying(2),
  status_year character varying(4),
  room_type_name character varying(30),
  avail_count numeric(3,0),
  total_count numeric(3,0),
  user_id character varying(30),
  status character(1) DEFAULT 'A'::bpchar
)

分析は次のように説明します

**"HashAggregate  (cost=293807.83..293808.04 rows=7 width=288) (actual time=20762.133..20762.133 rows=0 loops=1)"
"  ->  Unique  (cost=293807.42..293807.60 rows=7 width=35) (actual time=20762.129..20762.129 rows=0 loops=1)"
"        ->  Sort  (cost=293807.42..293807.44 rows=7 width=35) (actual time=20762.127..20762.127 rows=0 loops=1)"
"              Sort Key: room_type_name, total, d1, d2, d3, d4, d5, d6, d7"
"              ->  Append  (cost=41972.44..293807.33 rows=7 width=35) (actual time=20762.066..20762.066 rows=0 loops=1)"
"                    ->  Subquery Scan "*SELECT* 1"  (cost=41972.44..41972.47 rows=1 width=35) (actual time=2985.603..2985.603 rows=0 loops=1)"
"                          ->  Unique  (cost=41972.44..41972.46 rows=1 width=35) (actual time=2985.601..2985.601 rows=0 loops=1)"
"                                ->  Sort  (cost=41972.44..41972.44 rows=1 width=35) (actual time=2985.599..2985.599 rows=0 loops=1)"
"                                      Sort Key: room_type_name, sum(total_count), ((sum(avail_count))::text || ' '::text), '0'::text, '0'::text, '0'::text, '0'::text, '0'::text, '0'::text"
"                                      ->  HashAggregate  (cost=41972.41..41972.43 rows=1 width=35) (actual time=2985.579..2985.579 rows=0 loops=1)"
"                                            ->  Seq Scan on tmp_avail_count_dtls  (cost=0.00..41972.40 rows=1 width=35) (actual time=2985.576..2985.576 rows=0 loops=1)"
"                                                  Filter: ((to_char((week_date)::timestamp with time zone, 'DD-MON-YYYY'::text) = to_char(('2013-03-20'::date)::timestamp with time zone, 'DD-MON-YYYY'::text)) AND ((user_id)::text = 'APHYDRSA00010'::text))"
"                    ->  Subquery Scan "*SELECT* 2"  (cost=41972.44..41972.47 rows=1 width=35) (actual time=2944.060..2944.060 rows=0 loops=1)"
"                          ->  Unique  (cost=41972.44..41972.46 rows=1 width=35) (actual time=2944.056..2944.056 rows=0 loops=1)"
"                                ->  Sort  (cost=41972.44..41972.44 rows=1 width=35) (actual time=2944.054..2944.054 rows=0 loops=1)"
"                                      Sort Key: room_type_name, sum(total_count), '0'::text, ((sum(avail_count))::text || ' '::text), '0'::text, '0'::text, '0'::text, '0'::text, '0'::text"
"                                      ->  HashAggregate  (cost=41972.41..41972.43 rows=1 width=35) (actual time=2944.019..2944.019 rows=0 loops=1)"
"                                            ->  Seq Scan on tmp_avail_count_dtls  (cost=0.00..41972.40 rows=1 width=35) (actual time=2944.015..2944.015 rows=0 loops=1)"
"                                                  Filter: ((to_char((week_date)::timestamp with time zone, 'DD-MON-YYYY'::text) = to_char(('2013-03-21'::date)::timestamp with time zone, 'DD-MON-YYYY'::text)) AND ((user_id)::text = 'APHYDRSA00010'::text))"
"                    ->  Subquery Scan "*SELECT* 3"  (cost=41972.44..41972.47 rows=1 width=35) (actual time=2954.656..2954.656 rows=0 loops=1)"
"                          ->  Unique  (cost=41972.44..41972.46 rows=1 width=35) (actual time=2954.653..2954.653 rows=0 loops=1)"
"                                ->  Sort  (cost=41972.44..41972.44 rows=1 width=35) (actual time=2954.651..2954.651 rows=0 loops=1)"
"                                      Sort Key: room_type_name, sum(total_count), '0'::text, '0'::text, ((sum(avail_count))::text || ' '::text), '0'::text, '0'::text, '0'::text, '0'::text"
"                                      ->  HashAggregate  (cost=41972.41..41972.43 rows=1 width=35) (actual time=2954.617..2954.617 rows=0 loops=1)"
"                                            ->  Seq Scan on tmp_avail_count_dtls  (cost=0.00..41972.40 rows=1 width=35) (actual time=2954.614..2954.614 rows=0 loops=1)"
"                                                  Filter: ((to_char((week_date)::timestamp with time zone, 'DD-MON-YYYY'::text) = to_char(('2013-03-22'::date)::timestamp with time zone, 'DD-MON-YYYY'::text)) AND ((user_id)::text = E'APHYDRSA0\015\0120010'::text))"
"                    ->  Subquery Scan "*SELECT* 4"  (cost=41972.44..41972.47 rows=1 width=35) (actual time=2998.072..2998.072 rows=0 loops=1)"
"                          ->  Unique  (cost=41972.44..41972.46 rows=1 width=35) (actual time=2998.070..2998.070 rows=0 loops=1)"
"                                ->  Sort  (cost=41972.44..41972.44 rows=1 width=35) (actual time=2998.068..2998.068 rows=0 loops=1)"
"                                      Sort Key: room_type_name, sum(total_count), '0'::text, '0'::text, '0'::text, ((sum(avail_count))::text || ' '::text), '0'::text, '0'::text, '0'::text"
"                                      ->  HashAggregate  (cost=41972.41..41972.43 rows=1 width=35) (actual time=2998.034..2998.034 rows=0 loops=1)"
"                                            ->  Seq Scan on tmp_avail_count_dtls  (cost=0.00..41972.40 rows=1 width=35) (actual time=2998.031..2998.031 rows=0 loops=1)"
"                                                  Filter: ((to_char((week_date)::timestamp with time zone, E'DD-MON-\015\012YYYY'::text) = to_char(('2013-03-23'::date)::timestamp with time zone, 'DD-MON-YYYY'::text)) AND ((user_id)::text = 'APHYDRSA00010'::text))"
"                    ->  Subquery Scan "*SELECT* 5"  (cost=41972.44..41972.47 rows=1 width=35) (actual time=2933.656..2933.656 rows=0 loops=1)"
"                          ->  Unique  (cost=41972.44..41972.46 rows=1 width=35) (actual time=2933.653..2933.653 rows=0 loops=1)"
"                                ->  Sort  (cost=41972.44..41972.44 rows=1 width=35) (actual time=2933.651..2933.651 rows=0 loops=1)"
"                                      Sort Key: room_type_name, sum(total_count), '0'::text, '0'::text, '0'::text, '0'::text, ((sum(avail_count))::text || ' '::text), '0'::text, '0'::text"
"                                      ->  HashAggregate  (cost=41972.41..41972.43 rows=1 width=35) (actual time=2933.616..2933.616 rows=0 loops=1)"
"                                            ->  Seq Scan on tmp_avail_count_dtls  (cost=0.00..41972.40 rows=1 width=35) (actual time=2933.613..2933.613 rows=0 loops=1)"
"                                                  Filter: ((to_char((week_date)::timestamp with time zone, 'DD-MON-YYYY'::text) = to_char(('2013-03-24'::date)::timestamp with time zone, 'DD-MON-YYYY'::text)) AND ((user_id)::text = 'APHYDRSA00010'::text))"
"                    ->  Subquery Scan "*SELECT* 6"  (cost=41972.44..41972.47 rows=1 width=35) (actual time=3003.702..3003.702 rows=0 loops=1)"
"                          ->  Unique  (cost=41972.44..41972.46 rows=1 width=35) (actual time=3003.698..3003.698 rows=0 loops=1)"
"                                ->  Sort  (cost=41972.44..41972.44 rows=1 width=35) (actual time=3003.696..3003.696 rows=0 loops=1)"
"                                      Sort Key: room_type_name, sum(total_count), '0'::text, '0'::text, '0'::text, '0'::text, '0'::text, ((sum(avail_count))::text || ' '::text), '0'::text"
"                                      ->  HashAggregate  (cost=41972.41..41972.43 rows=1 width=35) (actual time=3003.663..3003.663 rows=0 loops=1)"
"                                            ->  Seq Scan on tmp_avail_count_dtls  (cost=0.00..41972.40 rows=1 width=35) (actual time=3003.658..3003.658 rows=0 loops=1)"
"                                                  Filter: ((to_char((week_date)::timestamp with time zone, 'DD-MON-YYYY'::text) = to_char(('2013-03-25'::date)::timestamp with time zone, E'DD-MON-\015\012YYYY'::text)) AND ((user_id)::text = 'APHYDRSA00010'::text))"
"                    ->  Subquery Scan "*SELECT* 7"  (cost=41972.44..41972.47 rows=1 width=35) (actual time=2942.304..2942.304 rows=0 loops=1)"
"                          ->  Unique  (cost=41972.44..41972.46 rows=1 width=35) (actual time=2942.300..2942.300 rows=0 loops=1)"
"                                ->  Sort  (cost=41972.44..41972.44 rows=1 width=35) (actual time=2942.299..2942.299 rows=0 loops=1)"
"                                      Sort Key: room_type_name, sum(total_count), '0'::text, '0'::text, '0'::text, '0'::text, '0'::text, '0'::text, ((sum(avail_count))::text || ' '::text)"
"                                      ->  HashAggregate  (cost=41972.41..41972.43 rows=1 width=35) (actual time=2942.264..2942.264 rows=0 loops=1)"
"                                            ->  Seq Scan on tmp_avail_count_dtls  (cost=0.00..41972.40 rows=1 width=35) (actual time=2942.260..2942.260 rows=0 loops=1)"
"                                                  Filter: ((to_char((week_date)::timestamp with time zone, 'DD-MON-YYYY'::text) = to_char(('2013-03-26'::date)::timestamp with time zone, 'DD-MON-YYYY'::text)) AND ((user_id)::text = 'APHYDRSA00010'::text))"
"Total runtime: 20762.843 ms"**

変更後のクエリ

SELECT a.room_type_name,MAX(total),MAX(D1),MAX(D2),MAX(D3),MAX(D4),MAX(D5),MAX(D6),MAX(D7) FROM 
(SELECT DISTINCT room_type_name,SUM(total_count) as total,SUM(avail_count)||
' ' as D1,'0' as D2,'0' as D3,'0' as D4,'0' as D5,'0' as D6,'0' as D7 FROM TMP_AVAIL_COUNT_DTLS WHERE week_date=TO_DATE('20-03-2013','DD-MM-YYYY') AND user_id='APHYDRSA00010' GROUP BY room_type_name 
UNION all
SELECT DISTINCT room_type_name,SUM(total_count) as total,'0' as
D1,SUM(avail_count)||' ' as D2,'0' as D3,'0' as D4,'0' as D5,'0' as D6,'0' as D7
FROM TMP_AVAIL_COUNT_DTLS WHERE week_date=TO_DATE('20-03-2013','DD-MM-YYYY')+1 AND user_id='APHYDRSA00010' GROUP BY room_type_name
UNION all
SELECT DISTINCT room_type_name,SUM(total_count) as total,'0' as D1,'0' as D2,SUM(avail_count)||' ' as D3,'0' as D4,'0' as D5,'0' as D6
,'0' as D7 FROM TMP_AVAIL_COUNT_DTLS WHERE week_date=TO_DATE('20-03-2013','DD-MM-YYYY')+2 AND user_id='APHYDRSA0
0010' GROUP BY room_type_name
UNION all
SELECT DISTINCT room_type_name,SUM(total_count) as total,'0' as D1,'0' as D2,'0' as D3,SUM(avail_count)||' ' as D4,'0' as D5
,'0' as D6,'0' as D7 FROM TMP_AVAIL_COUNT_DTLS WHERE week_date=TO_DATE('20-03-2013','DD-MM-YYYY')+3 AND user_id=
'APHYDRSA00010' GROUP BY room_type_name 
UNION all
SELECT DISTINCT room_type_name,SUM
(total_count) as total,'0' as D1,'0' as D2,'0' as D3,'0' as D4,SUM(avail_count)||' ' as D5,'0' as D6,'0' as D7 FROM TMP_AVAIL_COUNT_DTLS WHERE week_date=TO_DATE('20-03-2013','DD-MM-YYYY')+4 AND
user_id='APHYDRSA00010' GROUP BY room_type_name
UNION all
SELECT DISTINCT room_type_name,SUM(total_count) as total,'0' as D1,'0' as D2,'0' as D3,'0' as D4,'0' as
D5,SUM(avail_count)||' ' as D6,'0' as D7 FROM TMP_AVAIL_COUNT_DTLS WHERE week_date=TO_DATE('20-03-2013','DD-MM-YYYY')+5
AND user_id='APHYDRSA00010' GROUP BY room_type_name
UNION all
SELECT DISTINCT room_type_name,SUM(total_count) as total,'0' as D1,'0' as D2,'0' as D3,'0' as
D4,'0' as D5,'0' as D6,SUM(avail_count)||' ' as D7 FROM TMP_AVAIL_COUNT_DTLS WHERE week_date=TO_DATE('20-03-2013','DD-MM-YYYY')+
6 AND user_id='APHYDRSA00010' GROUP BY room_type_name) as a GROUP BY room_type_name

上記のクエリは出力を 20ms しか与えていませんが、アプリケーションのフロントエンドに正しい出力を与えるかどうかはわかりませんか? 開発者に送信して、アプリケーションに残すように依頼できますか? 時間がありません..新しいクエリの計画を説明してください..

"HashAggregate  (cost=4288.08..4288.11 rows=1 width=334) (actual time=0.641..0.641 rows=0 loops=1)"
"  ->  Append  (cost=612.52..4287.93 rows=7 width=334) (actual time=0.638..0.638 rows=0 loops=1)"
"        ->  Unique  (cost=612.52..612.55 rows=1 width=35) (actual time=0.104..0.104 rows=0 loops=1)"
"              ->  Sort  (cost=612.52..612.53 rows=1 width=35) (actual time=0.102..0.102 rows=0 loops=1)"
"                    Sort Key: public.tmp_avail_count_dtls.room_type_name, sum(public.tmp_avail_count_dtls.total_count), ((sum(public.tmp_avail_count_dtls.avail_count))::text || ' '::text), '0'::text, '0'::text, '0'::text, '0'::text, '0'::text, '0'::text"
"                    ->  HashAggregate  (cost=612.49..612.51 rows=1 width=35) (actual time=0.090..0.090 rows=0 loops=1)"
"                          ->  Index Scan using week_date_index on tmp_avail_count_dtls  (cost=0.00..612.48 rows=1 width=35) (actual time=0.088..0.088 rows=0 loops=1)"
"                                Index Cond: (week_date = to_date('20-03-2013'::text, 'DD-MM-YYYY'::text))"
"                                Filter: ((user_id)::text = 'APHYDRSA00010'::text)"
"        ->  Unique  (cost=612.52..612.55 rows=1 width=35) (actual time=0.069..0.069 rows=0 loops=1)"
"              ->  Sort  (cost=612.52..612.53 rows=1 width=35) (actual time=0.067..0.067 rows=0 loops=1)"
"                    Sort Key: public.tmp_avail_count_dtls.room_type_name, sum(public.tmp_avail_count_dtls.total_count), '0'::text, ((sum(public.tmp_avail_count_dtls.avail_count))::text || ' '::text), '0'::text, '0'::text, '0'::text, '0'::text, '0'::text"
"                    ->  HashAggregate  (cost=612.49..612.51 rows=1 width=35) (actual time=0.056..0.056 rows=0 loops=1)"
"                          ->  Index Scan using week_date_index on tmp_avail_count_dtls  (cost=0.00..612.49 rows=1 width=35) (actual time=0.054..0.054 rows=0 loops=1)"
"                                Index Cond: (week_date = (to_date('20-03-2013'::text, 'DD-MM-YYYY'::text) + 1))"
"                                Filter: ((user_id)::text = 'APHYDRSA00010'::text)"
"        ->  Unique  (cost=612.52..612.55 rows=1 width=35) (actual time=0.071..0.071 rows=0 loops=1)"
"              ->  Sort  (cost=612.52..612.53 rows=1 width=35) (actual time=0.069..0.069 rows=0 loops=1)"
"                    Sort Key: public.tmp_avail_count_dtls.room_type_name, sum(public.tmp_avail_count_dtls.total_count), '0'::text, '0'::text, ((sum(public.tmp_avail_count_dtls.avail_count))::text || ' '::text), '0'::text, '0'::text, '0'::text, '0'::text"
"                    ->  HashAggregate  (cost=612.49..612.51 rows=1 width=35) (actual time=0.059..0.059 rows=0 loops=1)"
"                          ->  Index Scan using week_date_index on tmp_avail_count_dtls  (cost=0.00..612.49 rows=1 width=35) (actual time=0.057..0.057 rows=0 loops=1)"
"                                Index Cond: (week_date = (to_date('20-03-2013'::text, 'DD-MM-YYYY'::text) + 2))"
"                                Filter: ((user_id)::text = 'APHYDRSA0
"
"0010'::text)"
"        ->  Unique  (cost=612.52..612.55 rows=1 width=35) (actual time=0.108..0.108 rows=0 loops=1)"
"              ->  Sort  (cost=612.52..612.53 rows=1 width=35) (actual time=0.106..0.106 rows=0 loops=1)"
"                    Sort Key: public.tmp_avail_count_dtls.room_type_name, sum(public.tmp_avail_count_dtls.total_count), '0'::text, '0'::text, '0'::text, ((sum(public.tmp_avail_count_dtls.avail_count))::text || ' '::text), '0'::text, '0'::text, '0'::text"
"                    ->  HashAggregate  (cost=612.49..612.51 rows=1 width=35) (actual time=0.071..0.071 rows=0 loops=1)"
"                          ->  Index Scan using week_date_index on tmp_avail_count_dtls  (cost=0.00..612.49 rows=1 width=35) (actual time=0.069..0.069 rows=0 loops=1)"
"                                Index Cond: (week_date = (to_date('20-03-2013'::text, 'DD-MM-YYYY'::text) + 3))"
"                                Filter: ((user_id)::text = 'APHYDRSA00010'::text)"
"        ->  Unique  (cost=612.52..612.55 rows=1 width=35) (actual time=0.093..0.093 rows=0 loops=1)"
"              ->  Sort  (cost=612.52..612.53 rows=1 width=35) (actual time=0.091..0.091 rows=0 loops=1)"
"                    Sort Key: public.tmp_avail_count_dtls.room_type_name, sum(public.tmp_avail_count_dtls.total_count), '0'::text, '0'::text, '0'::text, '0'::text, ((sum(public.tmp_avail_count_dtls.avail_count))::text || ' '::text), '0'::text, '0'::text"
"                    ->  HashAggregate  (cost=612.49..612.51 rows=1 width=35) (actual time=0.080..0.080 rows=0 loops=1)"
"                          ->  Index Scan using week_date_index on tmp_avail_count_dtls  (cost=0.00..612.49 rows=1 width=35) (actual time=0.078..0.078 rows=0 loops=1)"
"                                Index Cond: (week_date = (to_date('20-03-2013'::text, 'DD-MM-YYYY'::text) + 4))"
"                                Filter: ((user_id)::text = 'APHYDRSA00010'::text)"
"        ->  Unique  (cost=612.52..612.55 rows=1 width=35) (actual time=0.084..0.084 rows=0 loops=1)"
"              ->  Sort  (cost=612.52..612.53 rows=1 width=35) (actual time=0.082..0.082 rows=0 loops=1)"
"                    Sort Key: public.tmp_avail_count_dtls.room_type_name, sum(public.tmp_avail_count_dtls.total_count), '0'::text, '0'::text, '0'::text, '0'::text, '0'::text, ((sum(public.tmp_avail_count_dtls.avail_count))::text || ' '::text), '0'::text"
"                    ->  HashAggregate  (cost=612.49..612.51 rows=1 width=35) (actual time=0.070..0.070 rows=0 loops=1)"
"                          ->  Index Scan using week_date_index on tmp_avail_count_dtls  (cost=0.00..612.49 rows=1 width=35) (actual time=0.068..0.068 rows=0 loops=1)"
"                                Index Cond: (week_date = (to_date('20-03-2013'::text, 'DD-MM-YYYY'::text) + 5))"
"                                Filter: ((user_id)::text = 'APHYDRSA00010'::text)"
"        ->  Unique  (cost=612.52..612.55 rows=1 width=35) (actual time=0.099..0.099 rows=0 loops=1)"
"              ->  Sort  (cost=612.52..612.53 rows=1 width=35) (actual time=0.097..0.097 rows=0 loops=1)"
"                    Sort Key: public.tmp_avail_count_dtls.room_type_name, sum(public.tmp_avail_count_dtls.total_count), '0'::text, '0'::text, '0'::text, '0'::text, '0'::text, '0'::text, ((sum(public.tmp_avail_count_dtls.avail_count))::text || ' '::text)"
"                    ->  HashAggregate  (cost=612.49..612.51 rows=1 width=35) (actual time=0.084..0.084 rows=0 loops=1)"
"                          ->  Index Scan using week_date_index on tmp_avail_count_dtls  (cost=0.00..612.49 rows=1 width=35) (actual time=0.082..0.082 rows=0 loops=1)"
"                                Index Cond: (week_date = (to_date('20-03-2013'::text, 'DD-MM-YYYY'::text) + 6))"
"                                Filter: ((user_id)::text = 'APHYDRSA00010'::text)"
"Total runtime: 0.935 ms"**
4

2 に答える 2

6

まず、これはモンスター クエリであり、関数インデックスは実際には開始する場所ではありません。まず、不要な関数呼び出しを取り除き、通常のインデックスを使用できるようにします。

最初にすべきことは、これらの to_char 呼び出しを取り除くことです。それらは、インデックス作成をより困難にし、CPU サイクルを使用するだけで、何もしません。

2 番目にすべきことは、これらの UNION を削除して、CASE ステートメントを使用した単一のクエリに置き換えることです。

それでもパフォーマンスの問題が解決しない場合は、EXPLAIN ANALYZE の結果を投稿してください。

于 2013-03-21T06:11:45.097 に答える
1

式にインデックスを作成するには、式は常に同じ入力に対して同じ出力を与える必要があります。PostgreSQL が知る限り、不変とマークされた関数だけがそれを行います。to_char は、出力が現在のセッションの設定に依存するため、不変ではありません。

そうは言っても、Chris Travers のアドバイスに従うことを強くお勧めします。

于 2013-03-21T06:40:17.083 に答える