2

我对 postgre sql 非常陌生,现在在我的公司中,我处于一个开发游戏查询并告诉我调整它的位置。

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

上面的查询给出的输出只有 20 毫秒,但不知道它是否会给应用程序前端提供正确的输出?我可以将它发送给开发人员并要求他保留在应用程序中吗?请帮助我没有时间..解释新查询的计划..

"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 周期。

要做的第二件事是摆脱使用 CASE 语句的单个查询替换这些 UNION。

如果这些不能解决您的性能问题,请发布解释分析结果,以便我们查看。

于 2013-03-21T06:11:45.097 回答
1

要在表达式上创建索引,表达式应始终为相同的输入提供相同的输出。据 PostgreSQL 所知,只有标记为不可变的函数才能这样做。to_char 不是不可变的,因为它的输出取决于当前会话中的设置。

话虽如此,我强烈建议您遵循 Chris Travers 的建议。

于 2013-03-21T06:40:17.083 回答