Re: [SQL] problem with slow select
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: .idcode, .time -> Seq Scan on (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: .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 (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 (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 _pkey on (cost=0.00..5.46 rows=1 width=19) (actual time=0.17..0.18 rows=1 loops=6) Index Cond: (.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 ---(end of broadcast)--- TIP 4: Don't 'kill -9' the postmaster
[SQL] problem with slow select
hi I have two tables: table: idcode varchar(15) -->references (idcode) table latitude float longitude float time timestamp p_a char(1) barcode address idprog serial... -->primary key flag boolean table idfact numeric(7,0) --->references . idcode varchar(15) --->primary key name varchar(20) I want to exctract the last operation of table for each idcode of table where idfact=123 I HAVE THIS QUERY BUT IT IS TOO SLOW (10 SECONDS): select .*,.name from , join (select .idcode,max(.tempo) as tempo from , where .idfact=123 and .idcode=.idcode group by .idcode) temptable on (temptable.tempo=.tempo and temptable.idcode=.idcode) where .idfact=123 and .idcode=.idcode order by .name; PLEASE HELP ME -- francescosaf Posted via http://www.webservertalk.com View this thread: http://www.webservertalk.com/message189539.html ---(end of broadcast)--- TIP 8: explain analyze is your friend