Thread: problem with slow select
hi I have two tables: table: XXXX idcode varchar(15) -->references (idcode) table YYYY latitude float longitude float time timestamp p_a char(1) barcode.... address.... idprog serial... -->primary key flag boolean table YYYY idfact numeric(7,0) --->references ..... idcode varchar(15) --->primary key name varchar(20) I want to exctract the last operation of table XXXX for each idcode of table YYYY where idfact=123 I HAVE THIS QUERY BUT IT IS TOO SLOW (10 SECONDS): select XXXX.*,YYYY.name from YYYY,XXXX join (select XXXX.idcode,max(XXXX.tempo) as tempo from XXXX,YYYY where YYYY.idfact=123 and XXXX.idcode=YYYY.idcode group by XXXX.idcode) temptable on (temptable.tempo=XXXX.tempo and temptable.idcode=XXXX.idcode) where YYYY.idfact=123 and XXXX.idcode=YYYY.idcode order by YYYY.name; PLEASE HELP ME -- francescosaf ------------------------------------------------------------------------ Posted via http://www.webservertalk.com ------------------------------------------------------------------------ View this thread: http://www.webservertalk.com/message189539.html
On Wed, 2004-04-21 at 11:00, francescosaf wrote: > hi > > I have two tables: Please send results of EXPLAIN ANALYZE for the query in question. Thanks
thank you for answer and excuse me for later this is query plan: QUERY PLAN --------------------------------------------------------------------------------------------------------------------------------------------------------------- Nested Loop (cost=10564.42..10853.74 rows=1 width=150) (actual time=11626.89..12060.36 rows=6 loops=1) -> Merge Join (cost=10564.42..10820.69 rows=6 width=131) (actual time=11626.45..12058.84 rows=6 loops=1) Merge Cond: (("outer".idcode = "inner".idcode) AND ("outer".time = "inner".time)) -> Sort (cost=7550.07..7675.40 rows=50134 width=104) (actual time=8910.91..9031.23 rows=49293 loops=1) Sort Key: XXXX.idcode, XXXX.time -> Seq Scan on XXXX (cost=0.00..1586.34 rows=50134 width=104) (actual time=0.10..852.78 rows=50515 loops=1) -> Sort (cost=3014.35..3017.11 rows=1102 width=31) (actual time=2419.43..2419.45 rows=6 loops=1) Sort Key: temptable.idcode, temptable.time -> Subquery Scan temptable (cost=2876.08..2958.70 rows=1102 width=31) (actual time=2154.84..2419.15 rows=6 loops=1) -> Aggregate (cost=2876.08..2958.70 rows=1102 width=31) (actual time=2154.83..2419.10 rows=6 loops=1) -> Group (cost=2876.08..2931.16 rows=11016 width=31) (actual time=2066.58..2389.91 rows=19486 loops=1) -> Sort (cost=2876.08..2903.62 rows=11016 width=31) (actual time=2066.55..2106.39 rows=19486 loops=1) Sort Key: XXXX.idcode -> Hash Join (cost=36.50..2136.54 rows=11016 width=31) (actual time=3.41..765.49 rows=19486 loops=1) Hash Cond: ("outer".idcode = "inner".idcode) -> Seq Scan on XXXX (cost=0.00..1586.34 rows=50134 width=21) (actual time=0.12..301.87 rows=50515 loops=1) -> Hash (cost=36.48..36.48 rows=7 width=10) (actual time=3.10..3.10 rows=0 loops=1) -> Seq Scan on YYYY (cost=0.00..36.48 rows=7 width=10) (actual time=0.13..3.04 rows=6 loops=1) Filter: ((idazien = 8::numeric) OR (idazien = 1019::numeric)) -> Index Scan using YYYY_pkey on YYYY (cost=0.00..5.46 rows=1 width=19) (actual time=0.17..0.18 rows=1 loops=6) Index Cond: (YYYY.idcode = "outer".idcode) Filter: ((idazien = 8::numeric) OR (idazien = 1019::numeric)) Total runtime: 12065.05 msec (23 rows) -- francescosaf ------------------------------------------------------------------------ Posted via http://www.webservertalk.com ------------------------------------------------------------------------ View this thread: http://www.webservertalk.com/message189539.html