我是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
上面的查询只给出输出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"**
答案 0 :(得分:6)
首先,这是一个怪物查询,功能索引真的不是开始的地方。首先要做的是摆脱不必要的函数调用,以便开始使用普通索引。
要做的第一件事是摆脱那些to_char调用。它们什么都不做,只是使索引更加困难并使用CPU周期。
要做的第二件事就是用一个带CASE语句的查询来取代那些UNION。
如果这些不能解决您的性能问题,请发布EXPLAIN ANALYZE结果,以便我们查看。
答案 1 :(得分:1)
要在表达式上创建索引,表达式应始终为同一输入提供相同的输出。至于PostgreSQL只知道标记为不可变的函数那样做。 to_char不是不可变的,因为它的输出取决于当前会话中的设置。
话虽如此,我强烈建议您遵循Chris Travers的建议。