Thank for the reply.
I will move this onto the perfromance group, except that for some odd reason I cant see a
performance group on usenet. I will subscribe to the mailing list....
I will gladly upload the schema (script) or anything else that anyone wants, but it may not be
appropriate for a newsgroup or mailing list. If you want anything else please let me know where to
send it.
This is not the only query that is causing me a problem. I have about 30 stored procedures to move
from SQLServer. 15 of the 21 so far moved over to PG have similar problems. Very fast when submitted
as a complete SQL statement with constants. Very slow when used as functions.
Here is the explain for ths query using constants:
QUERY PLAN
Hash Join (cost=629.34..2 13908.73 rows=34 width=151) (actual time=110.000..1 518.000 rows=98
loops=1)
Hash Cond: ("outer".staff_ id = "inner".staff_i d)
Join Filter: (subplan)
InitPlan
-> Seq Scan on wruserarea (cost=1.46..3.2 9 rows=1 width=4) (actual time=0.000..0.0 00 rows=1
loops=1)
Filter: ((uid = $4) AND (area_id = 1))
InitPlan
-> Seq Scan on wruser (cost=0.00..1.4 6 rows=1 width=4) (actual time=0.000..0.0 00
rows=1 loops=1)
Filter: ((username)::na me = "current_user"( ))
-> Seq Scan on staff_contract sc (cost=0.00..10. 35 rows=335 width=34) (actual time=0.000..1.0 00
rows=335 loops=1)
-> Hash (cost=625.88..6 25.88 rows=66 width=125) (actual time=10.000..10 .000 rows=0 loops=1)
-> Nested Loop (cost=56.55..62 5.88 rows=66 width=125) (actual time=6.000..10. 000 rows=98
loops=1)
-> Merge Join (cost=56.55..73 .06 rows=101 width=111) (actual time=6.000..7.0 00
rows=98 loops=1)
Merge Cond: ("outer".staff_ id = "inner".staff_i d)
-> Index Scan using staff_pkey on staff (cost=7.74..21. 90 rows=332 width=107)
(actual time=4.000..4.0 00 rows=332 loops=1)
Filter: ((hashed subplan) OR $5)
SubPlan
-> Seq Scan on staff_area (cost=3.16..7.5 2 rows=88 width=4) (actual
time=0.000..0.0 00 rows=18 loops=1)
Filter: ((hashed subplan) OR (area_id = 1))
SubPlan
-> Seq Scan on wruserarea (cost=1.46..3.1 6 rows=3 width=4)
(actual time=0.000..0.0 00 rows=1 loops=1)
Filter: (uid = $1)
InitPlan
-> Seq Scan on wruser (cost=0.00..1.4 6 rows=1 width=4)
(actual time=0.000..0.0 00 rows=1 loops=1)
Filter: ((username)::na me = "current_user"( ))
-> Sort (cost=48.81..49 .06 rows=101 width=4) (actual time=2.000..2.0 00 rows=98
loops=1)
Sort Key: sr.staff_id
-> Seq Scan on search_reqt_res ult sr (cost=0.00..45. 45 rows=101 width=4)
(actual time=0.000..2.0 00 rows=98 loops=1)
Filter: (search_id = 143)
-> Index Scan using location_pkey on "location" (cost=0.00..5.4 6 rows=1 width=18)
(actual time=0.000..0.0 00 rows=1 loops=98)
Index Cond: ("location".loc ation_id = "outer".locatio n_id)
Filter: ((area_id = 1) OR (subplan))
SubPlan
-> Seq Scan on wruserarea (cost=1.46..3.4 4 rows=2 width=4) (never executed)
Filter: ((uid = $6) AND ((area_id = 1) OR (area_id = $7)))
InitPlan
-> Seq Scan on wruser (cost=0.00..1.4 6 rows=1 width=4) (never
executed)
Filter: ((username)::na me = "current_user"( ))
SubPlan
-> GroupAggregate (cost=3.10..639 .23 rows=1 width=4) (actual time=1.765..1.7 65 rows=1
loops=98)
Filter: (count(contract _id) = $9)
InitPlan
-> Aggregate (cost=1.55..1.5 5 rows=1 width=4) (actual time=0.000..0.0 00 rows=1
loops=1)
-> Seq Scan on search_order_re qt (cost=0.00..1.5 5 rows=1 width=4) (actual
time=0.000..0.0 00 rows=1 loops=1)
Filter: (search_id = 143)
-> Nested Loop IN Join (cost=1.55..637 .67 rows=1 width=4) (actual time=1.439..1.7 65
rows=1 loops=98)
Join Filter: ("outer".produc t_id = "inner".product _id)
-> Nested Loop (cost=0.00..631 .93 rows=186 width=8) (actual time=0.347..1.3 78
rows=245 loops=98)
-> Index Scan using staff_contract_ pkey on staff_contract c
(cost=0.00..15. 77 rows=1 width=4) (actual time=0.255..0.4 49 rows=1 loops=98)
Filter: ((staff_id = $8) AND (avail_date_fro m <= '2003-06-12'::date) AND
(avail_date_to >= '2003-06-18'::date))
-> Index Scan using staff_product_c ontract_id_key on staff_product p
(cost=0.00..613 .80 rows=189 width=8) (actual time=0.061..0.5 71 rows=245 loops=98)
Index Cond: (p.contract_id = "outer".contrac t_id)
-> Materialize (cost=1.55..1.5 6 rows=1 width=4) (actual time=0.000..0.0 00 rows=1
loops=23972)
-> Seq Scan on search_order_re qt (cost=0.00..1.5 5 rows=1 width=4) (actual
time=0.000..0.0 00 rows=1 loops=1)
Filter: (search_id = 143)
-> Aggregate (cost=2252.27.. 2252.27 rows=1 width=2) (actual time=12.673..12 .673 rows=1
loops=98)
-> Nested Loop (cost=2049.93.. 2252.19 rows=28 width=2) (actual time=8.959..12. 612
rows=13 loops=98)
-> Hash Join (cost=2049.93.. 2167.47 rows=28 width=4) (actual time=8.908..12. 337
rows=13 loops=98)
Hash Cond: ("outer".bookin g_id = "inner".booking _id)
-> Index Scan using booking_plan_id x2 on booking_plan bp (cost=0.00..98. 15
rows=3822 width=4) (actual time=0.755..6.5 20 rows=4693 loops=98)
Index Cond: ((booking_date >= '2003-06-12'::date) AND (booking_date <=
'2003-06-18'::date))
-> Hash (cost=2048.37.. 2048.37 rows=624 width=8) (actual time=2.694..2.6 94
rows=0 loops=98)
-> Index Scan using staff_book_idx5 on staff_booking b
(cost=0.00..204 8.37 rows=624 width=8) (actual time=0.041..2.0 31 rows=358 loops=98)
Index Cond: (contract_id = $0)
-> Index Scan using order_reqt_pkey on order_reqt r (cost=0.00..3.0 1 rows=1
width=6) (actual time=0.014..0.0 14 rows=1 loops=1312)
Index Cond: ("outer".reqt_i d = r.reqt_id)
-> Aggregate (cost=3387.38.. 3387.38 rows=1 width=0) (actual time=0.806..0.8 06 rows=1 loops=98)
-> Nested Loop (cost=0.00..338 7.37 rows=2 width=0) (actual time=0.765..0.7 86 rows=3
loops=98)
-> Index Scan using main_order_idx on main_order (cost=0.00..5.8 6 rows=2 width=4)
(actual time=0.041..0.0 51 rows=1 loops=98)
Index Cond: (client_id = 1011001947)
-> Index Scan using timesheet_detai l_idx on timesheet_detai l (cost=0.00..169 0.72
rows=3 width=4) (actual time=0.714..0.7 35 rows=3 loops=98)
Index Cond: ("outer".order_ id = timesheet_detai l.order_id)
Filter: (contract_id = $0)
Total runtime: 1530.000 ms
-------------------------------------------------------------------------------------------------------------------------------------------------
and here is the explain for the same query with parameters:
Hash Join (cost=651.46..4 50445.01 rows=35 width=151) (actual time=304.000..2 3814.000 rows=98
loops=1)
Hash Cond: ("outer".staff_ id = "inner".staff_i d)
Join Filter: (subplan)
InitPlan
-> Seq Scan on wruserarea (cost=1.46..3.2 9 rows=1 width=4) (actual time=0.000..0.0 00 rows=1
loops=1)
Filter: ((uid = $4) AND (area_id = 1))
InitPlan
-> Seq Scan on wruser (cost=0.00..1.4 6 rows=1 width=4) (actual time=0.000..0.0 00
rows=1 loops=1)
Filter: ((username)::na me = "current_user"( ))
-> Seq Scan on staff_contract sc (cost=0.00..10. 35 rows=335 width=34) (actual time=0.000..1.0 00
rows=335 loops=1)
-> Hash (cost=648.00..6 48.00 rows=68 width=125) (actual time=8.000..8.0 00 rows=0 loops=1)
-> Nested Loop (cost=56.71..64 8.00 rows=68 width=125) (actual time=4.000..8.0 00 rows=98
loops=1)
-> Merge Join (cost=56.71..73 .28 rows=105 width=111) (actual time=3.000..6.0 00
rows=98 loops=1)
Merge Cond: ("outer".staff_ id = "inner".staff_i d)
-> Index Scan using staff_pkey on staff (cost=7.74..21. 90 rows=332 width=107)
(actual time=1.000..2.0 00 rows=332 loops=1)
Filter: ((hashed subplan) OR $5)
SubPlan
-> Seq Scan on staff_area (cost=3.16..7.5 2 rows=88 width=4) (actual
time=0.000..0.0 00 rows=18 loops=1)
Filter: ((hashed subplan) OR (area_id = 1))
SubPlan
-> Seq Scan on wruserarea (cost=1.46..3.1 6 rows=3 width=4)
(actual time=0.000..0.0 00 rows=1 loops=1)
Filter: (uid = $1)
InitPlan
-> Seq Scan on wruser (cost=0.00..1.4 6 rows=1 width=4)
(actual time=0.000..0.0 00 rows=1 loops=1)
Filter: ((username)::na me = "current_user"( ))
-> Sort (cost=48.97..49 .24 rows=105 width=4) (actual time=2.000..2.0 00 rows=98
loops=1)
Sort Key: sr.staff_id
-> Seq Scan on search_reqt_res ult sr (cost=0.00..45. 45 rows=105 width=4)
(actual time=0.000..2.0 00 rows=98 loops=1)
Filter: (search_id = $4)
-> Index Scan using location_pkey on "location" (cost=0.00..5.4 6 rows=1 width=18)
(actual time=0.010..0.0 10 rows=1 loops=98)
Index Cond: ("location".loc ation_id = "outer".locatio n_id)
Filter: ((area_id = 1) OR (subplan))
SubPlan
-> Seq Scan on wruserarea (cost=1.46..3.4 4 rows=2 width=4) (never executed)
Filter: ((uid = $6) AND ((area_id = 1) OR (area_id = $7)))
InitPlan
-> Seq Scan on wruser (cost=0.00..1.4 6 rows=1 width=4) (never
executed)
Filter: ((username)::na me = "current_user"( ))
SubPlan
-> GroupAggregate (cost=3.10..652 .99 rows=1 width=4) (actual time=1.847..1.8 47 rows=1
loops=98)
Filter: (count(contract _id) = $9)
InitPlan
-> Aggregate (cost=1.55..1.5 5 rows=1 width=4) (actual time=0.000..0.0 00 rows=1
loops=1)
-> Seq Scan on search_order_re qt (cost=0.00..1.5 5 rows=1 width=4) (actual
time=0.000..0.0 00 rows=1 loops=1)
Filter: (search_id = $4)
-> Nested Loop IN Join (cost=1.55..651 .43 rows=1 width=4) (actual time=1.449..1.8 37
rows=1 loops=98)
Join Filter: ("outer".produc t_id = "inner".product _id)
-> Nested Loop (cost=0.00..645 .69 rows=186 width=8) (actual time=0.429..1.3 06
rows=245 loops=98)
-> Index Scan using staff_contract_ pkey on staff_contract c
(cost=0.00..17. 45 rows=1 width=4) (actual time=0.276..0.3 98 rows=1 loops=98)
Filter: ((staff_id = $8) AND ((avail_date_fr om)::timestamp without time
zone <= $1) AND ((avail_date_to )::timestamp without time zone >= $1))
-> Index Scan using staff_product_c ontract_id_key on staff_product p
(cost=0.00..625 .85 rows=191 width=8) (actual time=0.133..0.5 82 rows=245 loops=98)
Index Cond: (p.contract_id = "outer".contrac t_id)
-> Materialize (cost=1.55..1.5 6 rows=1 width=4) (actual time=0.000..0.0 01 rows=1
loops=23972)
-> Seq Scan on search_order_re qt (cost=0.00..1.5 5 rows=1 width=4) (actual
time=0.000..0.0 00 rows=1 loops=1)
Filter: (search_id = $4)
-> Aggregate (cost=8760.23.. 8760.24 rows=1 width=2) (actual time=236.245..2 36.245 rows=1
loops=98)
-> Nested Loop (cost=2009.53.. 8760.21 rows=9 width=2) (actual time=226.378..2 36.194
rows=14 loops=98)
-> Hash Join (cost=2009.53.. 8732.97 rows=9 width=4) (actual time=226.286..2 35.755
rows=14 loops=98)
Hash Cond: ("outer".bookin g_id = "inner".booking _id)
-> Seq Scan on booking_plan bp (cost=0.00..671 6.98 rows=1274 width=4)
(actual time=209.684..2 29.684 rows=4704 loops=98)
Filter: (((booking_date )::timestamp without time zone >= $1) AND
((booking_date) ::timestamp without time zone <= $2))
-> Hash (cost=2008.02.. 2008.02 rows=606 width=8) (actual time=3.357..3.3 57
rows=0 loops=98)
-> Index Scan using staff_book_idx5 on staff_booking b
(cost=0.00..200 8.02 rows=606 width=8) (actual time=0.163..3.0 61 rows=358 loops=98)
Index Cond: (contract_id = $0)
-> Index Scan using order_reqt_pkey on order_reqt r (cost=0.00..3.0 1 rows=1
width=6) (actual time=0.024..0.0 26 rows=1 loops=1326)
Index Cond: ("outer".reqt_i d = r.reqt_id)
-> Aggregate (cost=3443.91.. 3443.91 rows=1 width=0) (actual time=4.745..4.7 45 rows=1 loops=98)
-> Nested Loop (cost=0.00..344 3.90 rows=2 width=0) (actual time=4.531..4.7 24 rows=3
loops=98)
-> Index Scan using main_order_idx on main_order (cost=0.00..5.8 7 rows=2 width=4)
(actual time=0.143..0.1 53 rows=1 loops=98)
Index Cond: (client_id = $3)
-> Index Scan using timesheet_detai l_idx on timesheet_detai l (cost=0.00..171 8.97
rows=3 width=4) (actual time=4.378..4.5 71 rows=3 loops=98)
Index Cond: ("outer".order_ id = timesheet_detai l.order_id)
Filter: (contract_id = $0)
Total runtime: 23853.000 ms
The only real difference I can see is the booking_plan table using a sequential scan.
Any help appreciated
Thanks,
Gary.
On Sat, 21 Feb 2004 11:15:50 -0500,
tg*@sss.pgh.pa. us (Tom Lane) wrote:
Gary Doades <gp*@cwcom.ne t> writes: Is there someway to force the use of an index. Or at least get the
backend to substitue the parameters in a function before doing the
first query plan so it has more typical values to work with?
Could we see the EXPLAIN ANALYZE output for your problem query?
Table schemas (column data types and available indexes) are necessary
background for this type of question as well.
You might want to take the question to pgsql-performance, too ...
it's a tad off topic for -general.
regards, tom lane
---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match