pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedQuery Performance...
47+ messages / 21 participants
[nested] [flat]
* Query Performance...
@ 2002-07-17 20:31 Jeffrey Hood <jhood@hmcon.com>
2002-07-18 12:10 ` Re: Query Performance... Martijn van Oosterhout <kleptog@svana.org>
2002-07-18 14:08 ` Re: Query Performance... Tom Lane <tgl@sss.pgh.pa.us>
2002-07-18 15:43 ` Re: Query Performance... Jeffrey Hood <jhood@hmcon.com>
0 siblings, 3 replies; 47+ messages in thread
From: jhood@hmcon.com @ 2002-07-17 20:31 UTC (permalink / raw)
To: pgsql-general
We have recently converted from MS SQLServer 7 to Postgres 7.1... The
following query runs perfectly fine in SQL Server with any where
clause attached, but in Postgres runs fine until you put a where
clause that has *no* criteria for Patients... then it runs forever...
the table sizes are approx:
Patients: 1.5M rows
Dr: 2000 rows
Dr Groups: 500 rows
Rx: 750000 rows
All of the fields that are joined and used in where clauses are
indexed, and the query that has problems is:
SELECT
Pharm.Name, Pharm.City, Pharm.Phone,
Dr.LastName, Dr.City, Dr.Phone,
DrGroup.Name,
P.LastName, P.FirstName,P.DOB,
Rx.Medication, Rx.Unit, Rx.Qty
FROM Rx
INNER JOIN Pharm ON Rx.PharmID = Pharm.PharmID
INNER JOIN Dr ON Rx.DrID = Dr.DrID
INNER JOIN Patient P ON Rx.PatientID = P.PatientID
LEFT OUTER JOIN DrGroup ON Dr.DrGroupID = DrGroup.DrGroupID
I figure that there must be some other way to get it to run...
Any help is appreciated...
JH
^ permalink raw reply [nested|flat] 47+ messages in thread
* Re: Query Performance...
2002-07-17 20:31 Query Performance... Jeffrey Hood <jhood@hmcon.com>
@ 2002-07-18 12:10 ` Martijn van Oosterhout <kleptog@svana.org>
2 siblings, 0 replies; 47+ messages in thread
From: Martijn van Oosterhout @ 2002-07-18 12:10 UTC (permalink / raw)
To: Jeffrey Hood <jhood@hmcon.com>; +Cc: pgsql-general
On Wed, Jul 17, 2002 at 01:31:29PM -0700, Jeffrey Hood wrote:
> We have recently converted from MS SQLServer 7 to Postgres 7.1... The
> following query runs perfectly fine in SQL Server with any where
> clause attached, but in Postgres runs fine until you put a where
> clause that has *no* criteria for Patients... then it runs forever...
> the table sizes are approx:
EXPLAIN ANALYSE output please.
--
Martijn van Oosterhout <kleptog@svana.org> http://svana.org/kleptog/
> There are 10 kinds of people in the world, those that can do binary
> arithmetic and those that can't.
^ permalink raw reply [nested|flat] 47+ messages in thread
* Re: Query Performance...
2002-07-17 20:31 Query Performance... Jeffrey Hood <jhood@hmcon.com>
@ 2002-07-18 14:08 ` Tom Lane <tgl@sss.pgh.pa.us>
2 siblings, 0 replies; 47+ messages in thread
From: Tom Lane @ 2002-07-18 14:08 UTC (permalink / raw)
To: jhood@hmcon.com; +Cc: pgsql-general
jhood@hmcon.com (Jeffrey Hood) writes:
> We have recently converted from MS SQLServer 7 to Postgres 7.1... The
> following query runs perfectly fine in SQL Server with any where
> clause attached, but in Postgres runs fine until you put a where
> clause that has *no* criteria for Patients... then it runs forever...
Define "runs forever". AFAICT this query is going to give 750K rows of
output, if there's no WHERE clause. How long do you expect that to
take? How long does it actually take? Is SQL Server really any faster
given the same query with no WHERE?
regards, tom lane
^ permalink raw reply [nested|flat] 47+ messages in thread
* Re: Query Performance...
2002-07-17 20:31 Query Performance... Jeffrey Hood <jhood@hmcon.com>
@ 2002-07-18 15:43 ` Jeffrey Hood <jhood@hmcon.com>
2002-07-20 20:54 ` Re: Query Performance... Tom Lane <tgl@sss.pgh.pa.us>
2 siblings, 1 reply; 47+ messages in thread
From: jhood@hmcon.com @ 2002-07-18 15:43 UTC (permalink / raw)
To: pgsql-general
Here is the output from an explain...
explain
select r.dateissued, r.medication, p.lastname, p.dob
from rx r
inner join patient p on r.patientid = p.patientid
where r.dateissued between '7/13/02' and '7/14/02'
and lower (p.lastname) = 'may';
Merge Join (cost=42343.82..42498.98 rows=248957 width=64)
-> Sort (cost=8590.77..8590.77 rows=2515 width=32)
-> Index Scan using idx_rx_date_issued on rx r
(cost=0.00..8448.70 rows=2515 width=32)
-> Sort (cost=33753.05..33753.05 rows=9897 width=32)
-> Index Scan using idx_patient_last_name on patient p
(cost=0.00..32959.90 rows=9897 width=32)
explain
select r.dateissued, r.medication, p.lastname, p.dob
from rx r
inner join patient p on r.patientid = p.patientid
where r.dateissued between '7/13/02' and '7/14/02';
Merge Join (cost=237899.24..250302.47 rows=24895698 width=64)
-> Sort (cost=8590.77..8590.77 rows=2515 width=32)
-> Index Scan using idx_rx_date_issued on rx r
(cost=0.00..8448.70 rows=2515 width=32)
-> Sort (cost=229308.47..229308.47 rows=989743 width=32)
-> Seq Scan on patient p (cost=0.00..35256.43 rows=989743
width=32)
The first would return around 4 rows, the second around 1000...
How does one get rid of the table scan on patient in the second...???
Thanks,
JH
^ permalink raw reply [nested|flat] 47+ messages in thread
* Re: Query Performance...
2002-07-17 20:31 Query Performance... Jeffrey Hood <jhood@hmcon.com>
2002-07-18 15:43 ` Re: Query Performance... Jeffrey Hood <jhood@hmcon.com>
@ 2002-07-20 20:54 ` Tom Lane <tgl@sss.pgh.pa.us>
2002-07-21 20:48 ` Re: Query Performance... Jeffrey Hood <jhood@hmcon.com>
0 siblings, 1 reply; 47+ messages in thread
From: Tom Lane @ 2002-07-20 20:54 UTC (permalink / raw)
To: jhood@hmcon.com; +Cc: pgsql-general
jhood@hmcon.com (Jeffrey Hood) writes:
> explain
> select r.dateissued, r.medication, p.lastname, p.dob
> from rx r
> inner join patient p on r.patientid = p.patientid
> where r.dateissued between '7/13/02' and '7/14/02';
> Merge Join (cost=237899.24..250302.47 rows=24895698 width=64)
> -> Sort (cost=8590.77..8590.77 rows=2515 width=32)
> -> Index Scan using idx_rx_date_issued on rx r
> (cost=0.00..8448.70 rows=2515 width=32)
> -> Sort (cost=229308.47..229308.47 rows=989743 width=32)
> -> Seq Scan on patient p (cost=0.00..35256.43 rows=989743
> width=32)
> How does one get rid of the table scan on patient in the second...???
The only *possible* alternative to a seqscan on patient would be to use
a nestloop with inner indexscan on patient.patientid, and I'm not at all
clear that that'd be faster than the seqscan --- it would depend on how
many rows are actually returned by the rx scan.
Have you got an index on patientid? If you set enable_seqscan = off,
does the plan change?
regards, tom lane
^ permalink raw reply [nested|flat] 47+ messages in thread
* Re: Query Performance...
2002-07-17 20:31 Query Performance... Jeffrey Hood <jhood@hmcon.com>
2002-07-18 15:43 ` Re: Query Performance... Jeffrey Hood <jhood@hmcon.com>
2002-07-20 20:54 ` Re: Query Performance... Tom Lane <tgl@sss.pgh.pa.us>
@ 2002-07-21 20:48 ` Jeffrey Hood <jhood@hmcon.com>
0 siblings, 0 replies; 47+ messages in thread
From: jhood@hmcon.com @ 2002-07-21 20:48 UTC (permalink / raw)
To: pgsql-general
> > explain
> > select r.dateissued, r.medication, p.lastname, p.dob
> > from rx r
> > inner join patient p on r.patientid = p.patientid
> > where r.dateissued between '7/13/02' and '7/14/02';
>
> > Merge Join (cost=237899.24..250302.47 rows=24895698 width=64)
> > -> Sort (cost=8590.77..8590.77 rows=2515 width=32)
> > -> Index Scan using idx_rx_date_issued on rx r
> > (cost=0.00..8448.70 rows=2515 width=32)
> > -> Sort (cost=229308.47..229308.47 rows=989743 width=32)
> > -> Seq Scan on patient p (cost=0.00..35256.43 rows=989743
> > width=32)
>
> > How does one get rid of the table scan on patient in the second...???
>
> The only *possible* alternative to a seqscan on patient would be to use
> a nestloop with inner indexscan on patient.patientid, and I'm not at all
> clear that that'd be faster than the seqscan --- it would depend on how
> many rows are actually returned by the rx scan.
>
> Have you got an index on patientid? If you set enable_seqscan = off,
> does the plan change?
There is an index on patientid... and setting enable_seqscan = off and
running explain shows that the index will be used, but running the
query, it takes the same time (45 sec) that it does with the scan...
and the same query on SQLServer and mysql runs in .5 sec... (they both
always use the index...)
Thanks,
JH
^ permalink raw reply [nested|flat] 47+ messages in thread
* Query performance
@ 2006-08-03 08:12 Christian Rengstl <Christian.Rengstl@klinik.uni-regensburg.de>
2006-08-03 08:37 ` Re: Query performance Nikolay Samokhvalov <samokhvalov@gmail.com>
2006-08-03 09:57 ` Re: Query performance Chris Mair <chrisnospam@1006.org>
0 siblings, 2 replies; 47+ messages in thread
From: Christian Rengstl @ 2006-08-03 08:12 UTC (permalink / raw)
To: pgsql-general
Hi everyone,
i have a table with around 57 million tuples, with the following columns: pid(varchar), crit(varchar), val1(varchar), val2(varchar). Example:
pid crit val1 val2
p1 c1 x y
p1 c2 x z
p1 c3 y x
...
What i am doing is to query all val1 and val2 for one pid and all crit values:
select val1, val2, crit from mytable where pid='somepid' and crit in(select crit from myCritTable);
where myCritTable is a table that contains all crit values (around 42.000) ordered by their insertion date.
QUERY PLAN
--------------------------------------------------------------------------------
----------------------------------------------------------
Hash IN Join (cost=1033.67..134959.41 rows=37120 width=23) (actual time=357.11
6..356984.535 rows=37539 loops=1)
Hash Cond: (("outer".snp_id)::text = ("inner".snp_id)::text)
-> Bitmap Heap Scan on test2 (cost=232.92..132766.66 rows=37120 width=23) (
actual time=291.600..356707.737 rows=37539 loops=1)
Recheck Cond: ((pid)::text = '1'::text)
-> Bitmap Index Scan on idx_test2_pid (cost=0.00..232.92 rows=37120 w
idth=0) (actual time=234.516..234.516 rows=37539 loops=1)
Index Cond: ((pid)::text = '1'::text)
-> Hash (cost=700.20..700.20 rows=40220 width=13) (actual time=65.055..65.0
55 rows=40220 loops=1)
-> Seq Scan on snps_test (cost=0.00..700.20 rows=40220 width=13) (act
ual time=0.020..30.131 rows=40220 loops=1)
Total runtime: 357017.259 ms
Unfortunately the query takes pretty long for the big table, so maybe one of you has a suggestion on how to make it faster.
--
Christian Rengstl M.A.
Klinik und Poliklinik für Innere Medizin II
Kardiologie - Forschung
Universitätsklinikum Regensburg
B3 1.388
Franz-Josef-Strauss-Allee 11
93053 Regensburg
Tel.: +49-941-944-7230
^ permalink raw reply [nested|flat] 47+ messages in thread
* Re: Query performance
2006-08-03 08:12 Query performance Christian Rengstl <Christian.Rengstl@klinik.uni-regensburg.de>
@ 2006-08-03 08:37 ` Nikolay Samokhvalov <samokhvalov@gmail.com>
1 sibling, 0 replies; 47+ messages in thread
From: Nikolay Samokhvalov @ 2006-08-03 08:37 UTC (permalink / raw)
To: Christian Rengstl <Christian.Rengstl@klinik.uni-regensburg.de>; +Cc: pgsql-general
On 8/3/06, Christian Rengstl <Christian.Rengstl@klinik.uni-regensburg.de> wrote:
> ...
> Unfortunately the query takes pretty long for the big table, so maybe one of you has a suggestion on how to make it faster.
>
try smth like this:
select val1, val2, crit from mytable as a where pid='somepid' and
exists(select 1 from myCritTable as b where a.crit = b.crit);
--
Best regards,
Nikolay
^ permalink raw reply [nested|flat] 47+ messages in thread
* Re: Query performance
2006-08-03 08:12 Query performance Christian Rengstl <Christian.Rengstl@klinik.uni-regensburg.de>
@ 2006-08-03 09:57 ` Chris Mair <chrisnospam@1006.org>
1 sibling, 0 replies; 47+ messages in thread
From: Chris Mair @ 2006-08-03 09:57 UTC (permalink / raw)
To: Christian Rengstl <Christian.Rengstl@klinik.uni-regensburg.de>; +Cc: pgsql-general
> i have a table with around 57 million tuples, with the following columns: pid(varchar), crit(varchar), val1(varchar), val2(varchar). Example:
> pid crit val1 val2
> p1 c1 x y
> p1 c2 x z
> p1 c3 y x
> ...
> What i am doing is to query all val1 and val2 for one pid and all crit values:
>
> select val1, val2, crit from mytable where pid='somepid' and crit in(select crit from myCritTable);
> where myCritTable is a table that contains all crit values (around 42.000) ordered by their insertion date.
In case myCritTable doesn't change a lot and this select by contrast is
executed a lot, have you considered precomputing whether a record from
your big table has a crit value from myCritTable?
Of course this info would be invalidated each time myCritTable is
updated, so you would trade fast selects on the big table vs. slow
updates on myCritTable. Don't know wether that makes sence for you...
Bye, Chris.
^ permalink raw reply [nested|flat] 47+ messages in thread
* Re: Query performance
@ 2006-08-03 08:34 Hakan Kocaman <Hakan.Kocaman@digame.de>
2006-08-03 08:47 ` Re: Query performance Christian Rengstl <Christian.Rengstl@klinik.uni-regensburg.de>
0 siblings, 1 reply; 47+ messages in thread
From: Hakan Kocaman @ 2006-08-03 08:34 UTC (permalink / raw)
To: Christian Rengstl <Christian.Rengstl@klinik.uni-regensburg.de>; pgsql-general
Hi,
can you post the complete query,schema- and table-definition,server-version etc. ?
This will help to identity the main problem.
So at the moment i'm just guessing:
Hash Cond: (("outer".snp_id)::text = ("inner".snp_id)::text)
-> Bitmap Heap Scan on test2 (cost=232.92..132766.66 rows=37120 width=23)
(actual time=291.600..356707.737 rows=37539 loops=1)
This part is very expensive, but i got no clue why.
Maybe the text-type is not so ideal.
Best regards
Hakan Kocaman
Software-Development
digame.de GmbH
Richard-Byrd-Str. 4-8
50829 Köln
Tel.: +49 (0) 221 59 68 88 31
Fax: +49 (0) 221 59 68 88 98
Email: hakan.kocaman@digame.de
> -----Original Message-----
> From: pgsql-general-owner@postgresql.org
> [mailto:pgsql-general-owner@postgresql.org] On Behalf Of
> Christian Rengstl
> Sent: Thursday, August 03, 2006 10:13 AM
> To: pgsql-general@postgresql.org
> Subject: [GENERAL] Query performance
>
>
> Hi everyone,
>
> i have a table with around 57 million tuples, with the
> following columns: pid(varchar), crit(varchar),
> val1(varchar), val2(varchar). Example:
> pid crit val1 val2
> p1 c1 x y
> p1 c2 x z
> p1 c3 y x
> ...
> What i am doing is to query all val1 and val2 for one pid and
> all crit values:
>
> select val1, val2, crit from mytable where pid='somepid' and
> crit in(select crit from myCritTable);
> where myCritTable is a table that contains all crit values
> (around 42.000) ordered by their insertion date.
>
>
> QUERY PLAN
>
> --------------------------------------------------------------
> ------------------
> ----------------------------------------------------------
> Hash IN Join (cost=1033.67..134959.41 rows=37120 width=23)
> (actual time=357.11
> 6..356984.535 rows=37539 loops=1)
> Hash Cond: (("outer".snp_id)::text = ("inner".snp_id)::text)
> -> Bitmap Heap Scan on test2 (cost=232.92..132766.66
> rows=37120 width=23) (
> actual time=291.600..356707.737 rows=37539 loops=1)
> Recheck Cond: ((pid)::text = '1'::text)
> -> Bitmap Index Scan on idx_test2_pid
> (cost=0.00..232.92 rows=37120 w
> idth=0) (actual time=234.516..234.516 rows=37539 loops=1)
> Index Cond: ((pid)::text = '1'::text)
> -> Hash (cost=700.20..700.20 rows=40220 width=13)
> (actual time=65.055..65.0
> 55 rows=40220 loops=1)
> -> Seq Scan on snps_test (cost=0.00..700.20
> rows=40220 width=13) (act
> ual time=0.020..30.131 rows=40220 loops=1)
> Total runtime: 357017.259 ms
>
> Unfortunately the query takes pretty long for the big table,
> so maybe one of you has a suggestion on how to make it faster.
>
> --
> Christian Rengstl M.A.
> Klinik und Poliklinik für Innere Medizin II
> Kardiologie - Forschung
> Universitätsklinikum Regensburg
> B3 1.388
> Franz-Josef-Strauss-Allee 11
> 93053 Regensburg
> Tel.: +49-941-944-7230
>
>
> ---------------------------(end of
> broadcast)---------------------------
> TIP 4: Have you searched our list archives?
>
> http://archives.postgresql.org
>
^ permalink raw reply [nested|flat] 47+ messages in thread
* Re: Query performance
2006-08-03 08:34 Re: Query performance Hakan Kocaman <Hakan.Kocaman@digame.de>
@ 2006-08-03 08:47 ` Christian Rengstl <Christian.Rengstl@klinik.uni-regensburg.de>
2006-08-03 09:00 ` Re: Query performance Richard Huxton <dev@archonet.com>
0 siblings, 1 reply; 47+ messages in thread
From: Christian Rengstl @ 2006-08-03 08:47 UTC (permalink / raw)
To: Hakan Kocaman <Hakan.Kocaman@digame.de>; pgsql-general
Hi,
the complete query is the one i posted, but here comes the schema for mytable:
entry_no int8 NOT NULL DEFAULT nextval('entry_no_seq''::regclass),
pid varchar(15) NOT NULL,
crit varchar(13) NOT NULL,
val1 varchar(1),
val2 varchar(1),
aendat text,
aennam varchar(8),
CONSTRAINT "PK_ENTRY" PRIMARY KEY (entry_no)
myCritTable:
crit varchar(13) NOT NULL,
chr int2,
aendat timestamp,
CONSTRAINT pk_crit_master PRIMARY KEY (crit)
My server is 8.1.4. As a matter of fact, i have no idea where the text type comes from, because as you can see from above there are only varchar with maximum 15 characters.
"Hakan Kocaman" <Hakan.Kocaman@digame.de> wrote on 08/03/06 10:34 am:
> Hi,
>
> can you post the complete query,schema- and
> table-definition,server-version etc. ?
> This will help to identity the main problem.
>
> So at the moment i'm just guessing:
>
> Hash Cond: (("outer".snp_id)::text = ("inner".snp_id)::text)
> -> Bitmap Heap Scan on test2 (cost=232.92..132766.66 rows=37120
> width=23)
> (actual time=291.600..356707.737 rows=37539 loops=1)
> This part is very expensive, but i got no clue why.
> Maybe the text-type is not so ideal.
>
> Best regards
>
> Hakan Kocaman
> Software-Development
>
> digame.de GmbH
> Richard-Byrd-Str. 4-8
> 50829 Köln
>
> Tel.: +49 (0) 221 59 68 88 31
> Fax: +49 (0) 221 59 68 88 98
> Email: hakan.kocaman@digame.de
>
>
>
>> -----Original Message-----
>> From: pgsql-general-owner@postgresql.org
>> [mailto:pgsql-general-owner@postgresql.org] On Behalf Of
>> Christian Rengstl
>> Sent: Thursday, August 03, 2006 10:13 AM
>> To: pgsql-general@postgresql.org
>> Subject: [GENERAL] Query performance
>>
>>
>> Hi everyone,
>>
>> i have a table with around 57 million tuples, with the
>> following columns: pid(varchar), crit(varchar),
>> val1(varchar), val2(varchar). Example:
>> pid crit val1 val2
>> p1 c1 x y
>> p1 c2 x z
>> p1 c3 y x
>> ...
>> What i am doing is to query all val1 and val2 for one pid and
>> all crit values:
>>
>> select val1, val2, crit from mytable where pid='somepid' and
>> crit in(select crit from myCritTable);
>> where myCritTable is a table that contains all crit values
>> (around 42.000) ordered by their insertion date.
>>
>>
>> QUERY PLAN
>>
>> --------------------------------------------------------------
>> ------------------
>> ----------------------------------------------------------
>> Hash IN Join (cost=1033.67..134959.41 rows=37120 width=23)
>> (actual time=357.11
>> 6..356984.535 rows=37539 loops=1)
>> Hash Cond: (("outer".snp_id)::text = ("inner".snp_id)::text)
>> -> Bitmap Heap Scan on test2 (cost=232.92..132766.66
>> rows=37120 width=23) (
>> actual time=291.600..356707.737 rows=37539 loops=1)
>> Recheck Cond: ((pid)::text = '1'::text)
>> -> Bitmap Index Scan on idx_test2_pid
>> (cost=0.00..232.92 rows=37120 w
>> idth=0) (actual time=234.516..234.516 rows=37539 loops=1)
>> Index Cond: ((pid)::text = '1'::text)
>> -> Hash (cost=700.20..700.20 rows=40220 width=13)
>> (actual time=65.055..65.0
>> 55 rows=40220 loops=1)
>> -> Seq Scan on snps_test (cost=0.00..700.20
>> rows=40220 width=13) (act
>> ual time=0.020..30.131 rows=40220 loops=1)
>> Total runtime: 357017.259 ms
>>
>> Unfortunately the query takes pretty long for the big table,
>> so maybe one of you has a suggestion on how to make it faster.
>>
>> --
>> Christian Rengstl M.A.
>> Klinik und Poliklinik für Innere Medizin II
>> Kardiologie - Forschung
>> Universitätsklinikum Regensburg
>> B3 1.388
>> Franz-Josef-Strauss-Allee 11
>> 93053 Regensburg
>> Tel.: +49-941-944-7230
>>
>>
>> ---------------------------(end of
>> broadcast)---------------------------
>> TIP 4: Have you searched our list archives?
>>
>> http://archives.postgresql.org
>>
--
Christian Rengstl M.A.
Klinik und Poliklinik für Innere Medizin II
Kardiologie - Forschung
Universitätsklinikum Regensburg
B3 1.388
Franz-Josef-Strauss-Allee 11
93053 Regensburg
Tel.: +49-941-944-7230
^ permalink raw reply [nested|flat] 47+ messages in thread
* Re: Query performance
2006-08-03 08:34 Re: Query performance Hakan Kocaman <Hakan.Kocaman@digame.de>
2006-08-03 08:47 ` Re: Query performance Christian Rengstl <Christian.Rengstl@klinik.uni-regensburg.de>
@ 2006-08-03 09:00 ` Richard Huxton <dev@archonet.com>
0 siblings, 0 replies; 47+ messages in thread
From: Richard Huxton @ 2006-08-03 09:00 UTC (permalink / raw)
To: Christian Rengstl <Christian.Rengstl@klinik.uni-regensburg.de>; +Cc: Hakan Kocaman <Hakan.Kocaman@digame.de>; pgsql-general
Christian Rengstl wrote:
> Hi,
>
> the complete query is the one i posted, but here comes the schema for mytable:
> entry_no int8 NOT NULL DEFAULT nextval('entry_no_seq''::regclass),
> pid varchar(15) NOT NULL,
> crit varchar(13) NOT NULL,
> val1 varchar(1),
> val2 varchar(1),
> aendat text,
> aennam varchar(8),
> CONSTRAINT "PK_ENTRY" PRIMARY KEY (entry_no)
>
> myCritTable:
> crit varchar(13) NOT NULL,
> chr int2,
> aendat timestamp,
> CONSTRAINT pk_crit_master PRIMARY KEY (crit)
Still doesn't match the EXPLAIN output - where's snp_id? Where's table
test2?
> My server is 8.1.4. As a matter of fact, i have no idea where the text
> type comes from, because as you can see from above there are only
> varchar with maximum 15 characters.
PG is casting it to text. There's no real difference between the types
(other than the size limit) and it's not expensive.
> "Hakan Kocaman" <Hakan.Kocaman@digame.de> wrote on 08/03/06 10:34 am:
>> Hi,
>>
>> can you post the complete query,schema- and
>> table-definition,server-version etc. ?
>> This will help to identity the main problem.
>>
>> So at the moment i'm just guessing:
>>
>> Hash Cond: (("outer".snp_id)::text = ("inner".snp_id)::text)
>> -> Bitmap Heap Scan on test2 (cost=232.92..132766.66 rows=37120
>> width=23)
>> (actual time=291.600..356707.737 rows=37539 loops=1)
>> This part is very expensive, but i got no clue why.
Yep, it looks like the "Bitmap Heap Scan" is at the heart of this. You
might want to increase work_mem, it could be that the bitmap is spilling
to disk (which is much slower than keeping it all in RAM)
http://www.postgresql.org/docs/8.1/static/runtime-config-resource.html#RUNTIME-CONFIG-RESOURCE-MEMOR...
If it's still at 1MB (1024), try 5MB, 10MB, 50MB and see what happens.
--
Richard Huxton
Archonet Ltd
^ permalink raw reply [nested|flat] 47+ messages in thread
* Re: Query performance
@ 2006-08-03 09:08 Hakan Kocaman <Hakan.Kocaman@digame.de>
2006-08-03 09:17 ` Re: Query performance Christian Rengstl <Christian.Rengstl@klinik.uni-regensburg.de>
0 siblings, 1 reply; 47+ messages in thread
From: Hakan Kocaman @ 2006-08-03 09:08 UTC (permalink / raw)
To: Richard Huxton <dev@archonet.com>; Christian Rengstl <Christian.Rengstl@klinik.uni-regensburg.de>; +Cc: pgsql-general
Hi,
> -----Original Message-----
> From: Richard Huxton [mailto:dev@archonet.com]
> Sent: Thursday, August 03, 2006 11:00 AM
> To: Christian Rengstl
> Cc: Hakan Kocaman; pgsql-general@postgresql.org
> Subject: Re: [GENERAL] Query performance
>
>
> Christian Rengstl wrote:
> > Hi,
> >
> > the complete query is the one i posted, but here comes the
> schema for mytable:
> > entry_no int8 NOT NULL DEFAULT nextval('entry_no_seq''::regclass),
> > pid varchar(15) NOT NULL,
> > crit varchar(13) NOT NULL,
> > val1 varchar(1),
> > val2 varchar(1),
> > aendat text,
> > aennam varchar(8),
> > CONSTRAINT "PK_ENTRY" PRIMARY KEY (entry_no)
> >
> > myCritTable:
> > crit varchar(13) NOT NULL,
> > chr int2,
> > aendat timestamp,
> > CONSTRAINT pk_crit_master PRIMARY KEY (crit)
>
> Still doesn't match the EXPLAIN output - where's snp_id?
> Where's table
> test2?
>
Yep, that bothered me too.
> > My server is 8.1.4. As a matter of fact, i have no idea
> where the text
> > type comes from, because as you can see from above there are only
> > varchar with maximum 15 characters.
>
> PG is casting it to text. There's no real difference between
> the types
> (other than the size limit) and it's not expensive.
But wouldn't a comparison between int4 be much cheaper.
If i see smth like "id" (here snp_id) in a fieldname it should be a int-type, i think.
>
> > "Hakan Kocaman" <Hakan.Kocaman@digame.de> wrote on 08/03/06
> 10:34 am:
> >> Hi,
> >>
> >> can you post the complete query,schema- and
> >> table-definition,server-version etc. ?
> >> This will help to identity the main problem.
> >>
> >> So at the moment i'm just guessing:
> >>
> >> Hash Cond: (("outer".snp_id)::text = ("inner".snp_id)::text)
> >> -> Bitmap Heap Scan on test2 (cost=232.92..132766.66
> rows=37120
> >> width=23)
> >> (actual time=291.600..356707.737 rows=37539 loops=1)
> >> This part is very expensive, but i got no clue why.
>
> Yep, it looks like the "Bitmap Heap Scan" is at the heart of
> this. You
> might want to increase work_mem, it could be that the bitmap
> is spilling
> to disk (which is much slower than keeping it all in RAM)
>
> http://www.postgresql.org/docs/8.1/static/runtime-config-resou
rce.html#RUNTIME-CONFIG-RESOURCE-MEMORY
If it's still at 1MB (1024), try 5MB, 10MB, 50MB and see what happens.
--
Richard Huxton
Archonet Ltd
Hakan Kocaman
Software-Development
digame.de GmbH
Richard-Byrd-Str. 4-8
50829 Köln
Tel.: +49 (0) 221 59 68 88 31
Fax: +49 (0) 221 59 68 88 98
Email: hakan.kocaman@digame.de
^ permalink raw reply [nested|flat] 47+ messages in thread
* Re: Query performance
2006-08-03 09:08 Re: Query performance Hakan Kocaman <Hakan.Kocaman@digame.de>
@ 2006-08-03 09:17 ` Christian Rengstl <Christian.Rengstl@klinik.uni-regensburg.de>
0 siblings, 0 replies; 47+ messages in thread
From: Christian Rengstl @ 2006-08-03 09:17 UTC (permalink / raw)
To: Richard Huxton <dev@archonet.com>; Hakan Kocaman <Hakan.Kocaman@digame.de>; +Cc: pgsql-general
Hi,
i would rather compare int4 too, but the snp_id can be something like "abc123" unfortunately.
"Hakan Kocaman" <Hakan.Kocaman@digame.de> wrote on 08/03/06 11:08 am:
> Hi,
>
>
>> -----Original Message-----
>> From: Richard Huxton [mailto:dev@archonet.com]
>> Sent: Thursday, August 03, 2006 11:00 AM
>> To: Christian Rengstl
>> Cc: Hakan Kocaman; pgsql-general@postgresql.org
>> Subject: Re: [GENERAL] Query performance
>>
>>
>> Christian Rengstl wrote:
>> > Hi,
>> >
>> > the complete query is the one i posted, but here comes the
>> schema for mytable:
>> > entry_no int8 NOT NULL DEFAULT nextval('entry_no_seq''::regclass),
>> > pid varchar(15) NOT NULL,
>> > crit varchar(13) NOT NULL,
>> > val1 varchar(1),
>> > val2 varchar(1),
>> > aendat text,
>> > aennam varchar(8),
>> > CONSTRAINT "PK_ENTRY" PRIMARY KEY (entry_no)
>> >
>> > myCritTable:
>> > crit varchar(13) NOT NULL,
>> > chr int2,
>> > aendat timestamp,
>> > CONSTRAINT pk_crit_master PRIMARY KEY (crit)
>>
>> Still doesn't match the EXPLAIN output - where's snp_id?
>> Where's table
>> test2?
>>
>
> Yep, that bothered me too.
>
>> > My server is 8.1.4. As a matter of fact, i have no idea
>> where the text
>> > type comes from, because as you can see from above there are only
>> > varchar with maximum 15 characters.
>>
>> PG is casting it to text. There's no real difference between
>> the types
>> (other than the size limit) and it's not expensive.
>
> But wouldn't a comparison between int4 be much cheaper.
> If i see smth like "id" (here snp_id) in a fieldname it should be a
> int-type, i think.
>
>>
>> > "Hakan Kocaman" <Hakan.Kocaman@digame.de> wrote on 08/03/06
>> 10:34 am:
>> >> Hi,
>> >>
>> >> can you post the complete query,schema- and
>> >> table-definition,server-version etc. ?
>> >> This will help to identity the main problem.
>> >>
>> >> So at the moment i'm just guessing:
>> >>
>> >> Hash Cond: (("outer".snp_id)::text = ("inner".snp_id)::text)
>> >> -> Bitmap Heap Scan on test2 (cost=232.92..132766.66
>> rows=37120
>> >> width=23)
>> >> (actual time=291.600..356707.737 rows=37539 loops=1)
>> >> This part is very expensive, but i got no clue why.
>>
>> Yep, it looks like the "Bitmap Heap Scan" is at the heart of
>> this. You
>> might want to increase work_mem, it could be that the bitmap
>> is spilling
>> to disk (which is much slower than keeping it all in RAM)
>>
>> http://www.postgresql.org/docs/8.1/static/runtime-config-resou
> rce.html#RUNTIME-CONFIG-RESOURCE-MEMORY
>
> If it's still at 1MB (1024), try 5MB, 10MB, 50MB and see what happens.
>
> --
> Richard Huxton
> Archonet Ltd
>
>
>
> Hakan Kocaman
> Software-Development
>
> digame.de GmbH
> Richard-Byrd-Str. 4-8
> 50829 Köln
>
> Tel.: +49 (0) 221 59 68 88 31
> Fax: +49 (0) 221 59 68 88 98
> Email: hakan.kocaman@digame.de
>
> ---------------------------(end of broadcast)---------------------------
> TIP 9: In versions below 8.0, the planner will ignore your desire to
> choose an index scan if your joining column's datatypes do not
> match
--
Christian Rengstl M.A.
Klinik und Poliklinik für Innere Medizin II
Kardiologie - Forschung
Universitätsklinikum Regensburg
B3 1.388
Franz-Josef-Strauss-Allee 11
93053 Regensburg
Tel.: +49-941-944-7230
^ permalink raw reply [nested|flat] 47+ messages in thread
* Re: Query performance
@ 2006-08-03 09:36 Hakan Kocaman <Hakan.Kocaman@digame.de>
2006-08-03 10:27 ` Re: Query performance Christian Rengstl <Christian.Rengstl@klinik.uni-regensburg.de>
0 siblings, 1 reply; 47+ messages in thread
From: Hakan Kocaman @ 2006-08-03 09:36 UTC (permalink / raw)
To: Christian Rengstl <Christian.Rengstl@klinik.uni-regensburg.de>; Richard Huxton <dev@archonet.com>; +Cc: pgsql-general
Hi,
maybe you should overthink your db-design, but thats a bit premature whithout your complete
table-definitions(including table-names, datatypes, indexes, foreign-key constraints,etc.)
If your are using pgadmin3 just cut'n paste the content of the window on the bottom left for
the corresponding tables.
If you're using psql try \d yur-table-name.
Best regards
Hakan Kocaman
Software-Development
digame.de GmbH
Richard-Byrd-Str. 4-8
50829 Köln
Tel.: +49 (0) 221 59 68 88 31
Fax: +49 (0) 221 59 68 88 98
Email: hakan.kocaman@digame.de
> -----Original Message-----
> From: Christian Rengstl
> [mailto:Christian.Rengstl@klinik.uni-regensburg.de]
> Sent: Thursday, August 03, 2006 11:18 AM
> To: Richard Huxton; Hakan Kocaman
> Cc: pgsql-general@postgresql.org
> Subject: Re: [GENERAL] Query performance
>
>
> Hi,
>
> i would rather compare int4 too, but the snp_id can be
> something like "abc123" unfortunately.
>
> "Hakan Kocaman" <Hakan.Kocaman@digame.de> wrote on 08/03/06 11:08 am:
> > Hi,
> >
> >
> >> -----Original Message-----
> >> From: Richard Huxton [mailto:dev@archonet.com]
> >> Sent: Thursday, August 03, 2006 11:00 AM
> >> To: Christian Rengstl
> >> Cc: Hakan Kocaman; pgsql-general@postgresql.org
> >> Subject: Re: [GENERAL] Query performance
> >>
> >>
> >> Christian Rengstl wrote:
> >> > Hi,
> >> >
> >> > the complete query is the one i posted, but here comes the
> >> schema for mytable:
> >> > entry_no int8 NOT NULL DEFAULT
> nextval('entry_no_seq''::regclass),
> >> > pid varchar(15) NOT NULL,
> >> > crit varchar(13) NOT NULL,
> >> > val1 varchar(1),
> >> > val2 varchar(1),
> >> > aendat text,
> >> > aennam varchar(8),
> >> > CONSTRAINT "PK_ENTRY" PRIMARY KEY (entry_no)
> >> >
> >> > myCritTable:
> >> > crit varchar(13) NOT NULL,
> >> > chr int2,
> >> > aendat timestamp,
> >> > CONSTRAINT pk_crit_master PRIMARY KEY (crit)
> >>
> >> Still doesn't match the EXPLAIN output - where's snp_id?
> >> Where's table
> >> test2?
> >>
> >
> > Yep, that bothered me too.
> >
> >> > My server is 8.1.4. As a matter of fact, i have no idea
> >> where the text
> >> > type comes from, because as you can see from above
> there are only
> >> > varchar with maximum 15 characters.
> >>
> >> PG is casting it to text. There's no real difference between
> >> the types
> >> (other than the size limit) and it's not expensive.
> >
> > But wouldn't a comparison between int4 be much cheaper.
> > If i see smth like "id" (here snp_id) in a fieldname it should be a
> > int-type, i think.
> >
> >>
> >> > "Hakan Kocaman" <Hakan.Kocaman@digame.de> wrote on 08/03/06
> >> 10:34 am:
> >> >> Hi,
> >> >>
> >> >> can you post the complete query,schema- and
> >> >> table-definition,server-version etc. ?
> >> >> This will help to identity the main problem.
> >> >>
> >> >> So at the moment i'm just guessing:
> >> >>
> >> >> Hash Cond: (("outer".snp_id)::text = ("inner".snp_id)::text)
> >> >> -> Bitmap Heap Scan on test2 (cost=232.92..132766.66
> >> rows=37120
> >> >> width=23)
> >> >> (actual time=291.600..356707.737 rows=37539 loops=1)
> >> >> This part is very expensive, but i got no clue why.
> >>
> >> Yep, it looks like the "Bitmap Heap Scan" is at the heart of
> >> this. You
> >> might want to increase work_mem, it could be that the bitmap
> >> is spilling
> >> to disk (which is much slower than keeping it all in RAM)
> >>
> >> http://www.postgresql.org/docs/8.1/static/runtime-config-resou
> > rce.html#RUNTIME-CONFIG-RESOURCE-MEMORY
> >
> > If it's still at 1MB (1024), try 5MB, 10MB, 50MB and see
> what happens.
> >
> > --
> > Richard Huxton
> > Archonet Ltd
> >
> >
> >
> > Hakan Kocaman
> > Software-Development
> >
> > digame.de GmbH
> > Richard-Byrd-Str. 4-8
> > 50829 Köln
> >
> > Tel.: +49 (0) 221 59 68 88 31
> > Fax: +49 (0) 221 59 68 88 98
> > Email: hakan.kocaman@digame.de
> >
> > ---------------------------(end of
> broadcast)---------------------------
> > TIP 9: In versions below 8.0, the planner will ignore your desire to
> > choose an index scan if your joining column's
> datatypes do not
> > match
>
>
> --
> Christian Rengstl M.A.
> Klinik und Poliklinik für Innere Medizin II
> Kardiologie - Forschung
> Universitätsklinikum Regensburg
> B3 1.388
> Franz-Josef-Strauss-Allee 11
> 93053 Regensburg
> Tel.: +49-941-944-7230
>
>
^ permalink raw reply [nested|flat] 47+ messages in thread
* Re: Query performance
2006-08-03 09:36 Re: Query performance Hakan Kocaman <Hakan.Kocaman@digame.de>
@ 2006-08-03 10:27 ` Christian Rengstl <Christian.Rengstl@klinik.uni-regensburg.de>
0 siblings, 0 replies; 47+ messages in thread
From: Christian Rengstl @ 2006-08-03 10:27 UTC (permalink / raw)
To: Hakan Kocaman <Hakan.Kocaman@digame.de>; pgsql-general
Hi,
here is the definition of the master table which is inherited by around 30 tables based on the value of chr:
CREATE TABLE snp_master
(
entry_no int8 NOT NULL DEFAULT nextval('entry_no_seq'::regclass),
pid varchar(15) NOT NULL,
snp_id varchar(13) NOT NULL,
val1 varchar(1),
val2 varchar(1),
chr int2 NOT NULL,
aendat text,
aennam varchar(8),
CONSTRAINT "PK_ENTRY" PRIMARY KEY (entry_no),
CONSTRAINT "UNIQUE_SNP_ALLEL_MASTER" UNIQUE (pid, entry_no, snp_id)
)
WITHOUT OIDS;
The thing is that i load the data from txt files which themselves have more or less the same structure. So for every pid of 1500 there are up to 42000 different snp_id values and for each of this combinations there are different val1 and val2 entries (all together this accounts for up to around 58 million tuples in the biggest table). MyCritTable then just contains the distinct snp_ids so that at least this query does not take very long any more.
CREATE TABLE snps_master
(
snp_id varchar(13) NOT NULL,
chr int2,
aendat timestamp,
CONSTRAINT pk_snp_master PRIMARY KEY (snp_id)
)
WITHOUT OIDS;
Up to now there are no foreign key constraints, as once the data is loaded into the db there will be no update or delete operations at all. I only have to export the data to different file formats for which i need the query posted originally.
"Hakan Kocaman" <Hakan.Kocaman@digame.de> wrote on 08/03/06 11:36 am:
> Hi,
>
> maybe you should overthink your db-design, but thats a bit premature
> whithout your complete
> table-definitions(including table-names, datatypes, indexes,
> foreign-key constraints,etc.)
>
> If your are using pgadmin3 just cut'n paste the content of the window
> on the bottom left for
> the corresponding tables.
>
> If you're using psql try \d yur-table-name.
>
> Best regards
>
> Hakan Kocaman
> Software-Development
>
> digame.de GmbH
> Richard-Byrd-Str. 4-8
> 50829 Köln
>
> Tel.: +49 (0) 221 59 68 88 31
> Fax: +49 (0) 221 59 68 88 98
> Email: hakan.kocaman@digame.de
>
>
>
>> -----Original Message-----
>> From: Christian Rengstl
>> [mailto:Christian.Rengstl@klinik.uni-regensburg.de]
>> Sent: Thursday, August 03, 2006 11:18 AM
>> To: Richard Huxton; Hakan Kocaman
>> Cc: pgsql-general@postgresql.org
>> Subject: Re: [GENERAL] Query performance
>>
>>
>> Hi,
>>
>> i would rather compare int4 too, but the snp_id can be
>> something like "abc123" unfortunately.
>>
>> "Hakan Kocaman" <Hakan.Kocaman@digame.de> wrote on 08/03/06 11:08 am:
>> > Hi,
>> >
>> >
>> >> -----Original Message-----
>> >> From: Richard Huxton [mailto:dev@archonet.com]
>> >> Sent: Thursday, August 03, 2006 11:00 AM
>> >> To: Christian Rengstl
>> >> Cc: Hakan Kocaman; pgsql-general@postgresql.org
>> >> Subject: Re: [GENERAL] Query performance
>> >>
>> >>
>> >> Christian Rengstl wrote:
>> >> > Hi,
>> >> >
>> >> > the complete query is the one i posted, but here comes the
>> >> schema for mytable:
>> >> > entry_no int8 NOT NULL DEFAULT
>> nextval('entry_no_seq''::regclass),
>> >> > pid varchar(15) NOT NULL,
>> >> > crit varchar(13) NOT NULL,
>> >> > val1 varchar(1),
>> >> > val2 varchar(1),
>> >> > aendat text,
>> >> > aennam varchar(8),
>> >> > CONSTRAINT "PK_ENTRY" PRIMARY KEY (entry_no)
>> >> >
>> >> > myCritTable:
>> >> > crit varchar(13) NOT NULL,
>> >> > chr int2,
>> >> > aendat timestamp,
>> >> > CONSTRAINT pk_crit_master PRIMARY KEY (crit)
>> >>
>> >> Still doesn't match the EXPLAIN output - where's snp_id?
>> >> Where's table
>> >> test2?
>> >>
>> >
>> > Yep, that bothered me too.
>> >
>> >> > My server is 8.1.4. As a matter of fact, i have no idea
>> >> where the text
>> >> > type comes from, because as you can see from above
>> there are only
>> >> > varchar with maximum 15 characters.
>> >>
>> >> PG is casting it to text. There's no real difference between
>> >> the types
>> >> (other than the size limit) and it's not expensive.
>> >
>> > But wouldn't a comparison between int4 be much cheaper.
>> > If i see smth like "id" (here snp_id) in a fieldname it should be a
>> > int-type, i think.
>> >
>> >>
>> >> > "Hakan Kocaman" <Hakan.Kocaman@digame.de> wrote on 08/03/06
>> >> 10:34 am:
>> >> >> Hi,
>> >> >>
>> >> >> can you post the complete query,schema- and
>> >> >> table-definition,server-version etc. ?
>> >> >> This will help to identity the main problem.
>> >> >>
>> >> >> So at the moment i'm just guessing:
>> >> >>
>> >> >> Hash Cond: (("outer".snp_id)::text = ("inner".snp_id)::text)
>> >> >> -> Bitmap Heap Scan on test2 (cost=232.92..132766.66
>> >> rows=37120
>> >> >> width=23)
>> >> >> (actual time=291.600..356707.737 rows=37539 loops=1)
>> >> >> This part is very expensive, but i got no clue why.
>> >>
>> >> Yep, it looks like the "Bitmap Heap Scan" is at the heart of
>> >> this. You
>> >> might want to increase work_mem, it could be that the bitmap
>> >> is spilling
>> >> to disk (which is much slower than keeping it all in RAM)
>> >>
>> >> http://www.postgresql.org/docs/8.1/static/runtime-config-resou
>> > rce.html#RUNTIME-CONFIG-RESOURCE-MEMORY
>> >
>> > If it's still at 1MB (1024), try 5MB, 10MB, 50MB and see
>> what happens.
>> >
>> > --
>> > Richard Huxton
>> > Archonet Ltd
>> >
>> >
>> >
>> > Hakan Kocaman
>> > Software-Development
>> >
>> > digame.de GmbH
>> > Richard-Byrd-Str. 4-8
>> > 50829 Köln
>> >
>> > Tel.: +49 (0) 221 59 68 88 31
>> > Fax: +49 (0) 221 59 68 88 98
>> > Email: hakan.kocaman@digame.de
>> >
>> > ---------------------------(end of
>> broadcast)---------------------------
>> > TIP 9: In versions below 8.0, the planner will ignore your desire to
>> > choose an index scan if your joining column's
>> datatypes do not
>> > match
>>
>>
>> --
>> Christian Rengstl M.A.
>> Klinik und Poliklinik für Innere Medizin II
>> Kardiologie - Forschung
>> Universitätsklinikum Regensburg
>> B3 1.388
>> Franz-Josef-Strauss-Allee 11
>> 93053 Regensburg
>> Tel.: +49-941-944-7230
>>
>>
--
Christian Rengstl M.A.
Klinik und Poliklinik für Innere Medizin II
Kardiologie - Forschung
Universitätsklinikum Regensburg
B3 1.388
Franz-Josef-Strauss-Allee 11
93053 Regensburg
Tel.: +49-941-944-7230
^ permalink raw reply [nested|flat] 47+ messages in thread
* query performance
@ 2008-01-14 02:58 pepone.onrez <pepone.onrez@gmail.com>
2008-01-14 03:30 ` Re: query performance Alex Turner <armtuk@gmail.com>
2008-01-14 03:39 ` Re: query performance Scott Marlowe <scott.marlowe@gmail.com>
2008-01-17 09:40 ` Re: query performance Alban Hertroys <dalroi@solfertje.student.utwente.nl>
0 siblings, 3 replies; 47+ messages in thread
From: pepone.onrez @ 2008-01-14 02:58 UTC (permalink / raw)
To: pgsql-general
I have this query in a table with 150 thowsand tuples and it takes to long
t_documentcontent._id AS _id
FROM t_documentcontent LIMIT 50 OFFSET 80000
here is the explain output
"Limit (cost=100058762.30..100058799.02 rows=50 width=58) (actual time=
19433.474..19433.680 rows=50 loops=1)"
" -> Seq Scan on t_documentcontent
(cost=100000000.00..100110772.07rows=150807 width=58) (actual time=
53.934..19402.030 rows=80050 loops=1)"
"Total runtime: 19433.748 ms"
here the table structure
CREATE TABLE t_documentcontent(
_id varchar(60) NOT NULL,
_filesystem varchar(60) NOT NULL,
_parent varchar(60) NOT NULL,
_document varchar(60) NOT NULL,
_title varchar NOT NULL,
_resume varchar,
_content varchar,
_lang integer NOT NULL,
_creationdate timestamp NOT NULL DEFAULT now(),
_updatedate timestamp NOT NULL DEFAULT now(),
_indexeddate timestamp NOT NULL DEFAULT now(),
CONSTRAINT documentcontent_pkey PRIMARY KEY (_id),
CONSTRAINT documentcontent_filesystem_fkey
FOREIGN KEY (_filesystem) REFERENCES t_filesystem(_id) ON UPDATE
CASCADE ON DELETE NO ACTION,
CONSTRAINT documentcontent_parent_fkey FOREIGN KEY (_parent) REFERENCES
t_node(_id) ON DELETE NO ACTION,
CONSTRAINT documentcontent_document_fkey
FOREIGN KEY (_document) REFERENCES t_node(_id) ON UPDATE CASCADE ON
DELETE NO ACTION,
CONSTRAINT documentcontent_lang_lang_fkey FOREIGN KEY (_lang) REFERENCES
t_lang(_id)) WITHOUT OIDS;
Any ideas for improve this query performance.
^ permalink raw reply [nested|flat] 47+ messages in thread
* Re: query performance
2008-01-14 02:58 query performance pepone.onrez <pepone.onrez@gmail.com>
@ 2008-01-14 03:30 ` Alex Turner <armtuk@gmail.com>
2 siblings, 0 replies; 47+ messages in thread
From: Alex Turner @ 2008-01-14 03:30 UTC (permalink / raw)
To: pepone. onrez <pepone.onrez@gmail.com>; +Cc: pgsql-general
If you haven't already, make sure you've done a vacuum full recently. When
in doubt, pg_dump the db, and reload it, and see if that helps, but this
works for me:
create table overview as select _id from t_documentcontent;
alter table overview add constraint overview_pkey primary key (_id);
select * from t_documentcontent where _id in (select _id
FROM overview LIMIT 50 OFFSET 80000);
create a trigger on insert/delete/update to keep this table in sync, or if
you don't need to then just re-run the create every so often depending on
your needs (I'll be happy to demonstrate the required triggers if you need
it).
make sure that you have adequate RAM available for file cache, hitting the
disk everytime you query will suck no matter what you do.
Alex
On Jan 13, 2008 9:58 PM, pepone. onrez <pepone.onrez@gmail.com> wrote:
> I have this query in a table with 150 thowsand tuples and it takes to long
>
> t_documentcontent._id AS _id
> FROM t_documentcontent LIMIT 50 OFFSET 80000
>
> here is the explain output
>
> "Limit (cost= 100058762.30..100058799.02 rows=50 width=58) (actual time=
> 19433.474..19433.680 rows=50 loops=1)"
> " -> Seq Scan on t_documentcontent (cost=100000000.00..100110772.07rows=150807 width=58) (actual time=
> 53.934..19402.030 rows=80050 loops=1)"
> "Total runtime: 19433.748 ms"
>
> here the table structure
>
> CREATE TABLE t_documentcontent(
> _id varchar(60) NOT NULL,
> _filesystem varchar(60) NOT NULL,
> _parent varchar(60) NOT NULL,
> _document varchar(60) NOT NULL,
> _title varchar NOT NULL,
> _resume varchar,
> _content varchar,
> _lang integer NOT NULL,
> _creationdate timestamp NOT NULL DEFAULT now(),
> _updatedate timestamp NOT NULL DEFAULT now(),
> _indexeddate timestamp NOT NULL DEFAULT now(),
> CONSTRAINT documentcontent_pkey PRIMARY KEY (_id),
> CONSTRAINT documentcontent_filesystem_fkey
> FOREIGN KEY (_filesystem) REFERENCES t_filesystem(_id) ON UPDATE
> CASCADE ON DELETE NO ACTION,
> CONSTRAINT documentcontent_parent_fkey FOREIGN KEY (_parent)
> REFERENCES t_node(_id) ON DELETE NO ACTION,
> CONSTRAINT documentcontent_document_fkey
> FOREIGN KEY (_document) REFERENCES t_node(_id) ON UPDATE CASCADE
> ON DELETE NO ACTION,
> CONSTRAINT documentcontent_lang_lang_fkey FOREIGN KEY (_lang)
> REFERENCES t_lang(_id)) WITHOUT OIDS;
>
> Any ideas for improve this query performance.
>
^ permalink raw reply [nested|flat] 47+ messages in thread
* Re: query performance
2008-01-14 02:58 query performance pepone.onrez <pepone.onrez@gmail.com>
@ 2008-01-14 03:39 ` Scott Marlowe <scott.marlowe@gmail.com>
2008-01-14 03:52 ` Re: query performance Tom Lane <tgl@sss.pgh.pa.us>
2 siblings, 1 reply; 47+ messages in thread
From: Scott Marlowe @ 2008-01-14 03:39 UTC (permalink / raw)
To: pepone. onrez <pepone.onrez@gmail.com>; +Cc: pgsql-general
On Jan 13, 2008 8:58 PM, pepone. onrez <pepone.onrez@gmail.com> wrote:
> I have this query in a table with 150 thowsand tuples and it takes to long
>
> t_documentcontent._id AS _id
> FROM t_documentcontent LIMIT 50 OFFSET 80000
>
> here is the explain output
>
> "Limit (cost= 100058762.30..100058799.02 rows=50 width=58) (actual
> time=19433.474..19433.680 rows=50 loops=1)"
> " -> Seq Scan on t_documentcontent (cost=100000000.00..100110772.07
> rows=150807 width=58) (actual time=53.934..19402.030 rows=80050 loops=1)"
> "Total runtime: 19433.748 ms"
looks like you've set enable_seqscan=off. When looking for help on
queries it's a good idea to mention such things...
with no order by, and possibly no index on t_documentcontent._id,
there's no choice but a seq scan.
try adding both.
^ permalink raw reply [nested|flat] 47+ messages in thread
* Re: query performance
2008-01-14 02:58 query performance pepone.onrez <pepone.onrez@gmail.com>
2008-01-14 03:39 ` Re: query performance Scott Marlowe <scott.marlowe@gmail.com>
@ 2008-01-14 03:52 ` Tom Lane <tgl@sss.pgh.pa.us>
2008-01-14 04:27 ` Re: query performance pepone.onrez <pepone.onrez@gmail.com>
0 siblings, 1 reply; 47+ messages in thread
From: Tom Lane @ 2008-01-14 03:52 UTC (permalink / raw)
To: Scott Marlowe <scott.marlowe@gmail.com>; +Cc: pepone. onrez <pepone.onrez@gmail.com>; pgsql-general
"Scott Marlowe" <scott.marlowe@gmail.com> writes:
> On Jan 13, 2008 8:58 PM, pepone. onrez <pepone.onrez@gmail.com> wrote:
>> t_documentcontent._id AS _id
>> FROM t_documentcontent LIMIT 50 OFFSET 80000
> with no order by, and possibly no index on t_documentcontent._id,
> there's no choice but a seq scan.
More to the point: a query with a huge OFFSET is *always* going to suck,
because there is no choice but to read through all those records before
getting to the ones you want. You need to fundamentally rethink how you
are going about this. I'm assuming that this is actually just one query
in a series that are intended to eventually fetch the whole table.
One solution is to set up a cursor and FETCH 50 rows at a time from it.
However that requires holding a transaction open, which might not work
well in your environment.
Another possibility, if you have a primary key on the table, is to do
something like
SELECT ... FROM ... WHERE id > ? ORDER BY id LIMIT 50
where you leave out the WHERE clause on the first call, and on
subsequent calls '?' is the last id value seen in the prior call.
regards, tom lane
^ permalink raw reply [nested|flat] 47+ messages in thread
* Re: query performance
2008-01-14 02:58 query performance pepone.onrez <pepone.onrez@gmail.com>
2008-01-14 03:39 ` Re: query performance Scott Marlowe <scott.marlowe@gmail.com>
2008-01-14 03:52 ` Re: query performance Tom Lane <tgl@sss.pgh.pa.us>
@ 2008-01-14 04:27 ` pepone.onrez <pepone.onrez@gmail.com>
2008-01-14 04:43 ` Re: query performance Alex Turner <armtuk@gmail.com>
0 siblings, 1 reply; 47+ messages in thread
From: pepone.onrez @ 2008-01-14 04:27 UTC (permalink / raw)
To: pgsql-general
Sorry Alex i forget mention that i have setscan of in my last test.
now I have set seqscan on and indexscan on and added order by _id
The table has an index in the _id field
CREATE INDEX i_documentcontent_document
ON t_documentcontent
USING btree
(_document);
The database was rencently vacum analyze , but not vacun full
here is the explain of 2 diferent queries , when i put a large OFFSET
EXPLAIN ANALYZE SELECT
t_documentcontent._id AS _id
FROM t_documentcontent ORDER BY _id LIMIT 50 OFFSET 50000
"Limit (cost=137068.24..137068.36 rows=50 width=58) (actual time=
41119.702..41119.792 rows=50 loops=1)"
" -> Sort (cost=136943.24..137320.26 rows=150807 width=58) (actual time=
41064.802..41100.424 rows=50050 loops=1)"
" Sort Key: _id"
" -> Seq Scan on t_documentcontent (cost=
0.00..110772.07rows=150807 width=58) (actual time=
106.679..33267.194 rows=150807 loops=1)"
"Total runtime: 41120.015 ms"
EXPLAIN ANALYZE SELECT
t_documentcontent._id AS _id
FROM t_documentcontent ORDER BY _id LIMIT 50 OFFSET 10000
"Limit (cost=39839.37..40038.56 rows=50 width=58) (actual time=
1172.969..1194.228 rows=50 loops=1)"
" -> Index Scan using i_documentcontent_id on t_documentcontent (cost=
0.00..600805.54 rows=150807 width=58) (actual time=
0.077..1189.688rows=10050 loops=1)"
"Total runtime: 1194.316 ms"
Tom
i using uuid for the _id field that is the primary key add a WHERE id > ?
don 't apply
the cursor aproach is also not suitable for same of my queries
I use this query for paginate contents of a filesysstem with lots of
documents avoid offset is not posible always
^ permalink raw reply [nested|flat] 47+ messages in thread
* Re: query performance
2008-01-14 02:58 query performance pepone.onrez <pepone.onrez@gmail.com>
2008-01-14 03:39 ` Re: query performance Scott Marlowe <scott.marlowe@gmail.com>
2008-01-14 03:52 ` Re: query performance Tom Lane <tgl@sss.pgh.pa.us>
2008-01-14 04:27 ` Re: query performance pepone.onrez <pepone.onrez@gmail.com>
@ 2008-01-14 04:43 ` Alex Turner <armtuk@gmail.com>
2008-01-14 04:54 ` Re: query performance Alex Turner <armtuk@gmail.com>
0 siblings, 1 reply; 47+ messages in thread
From: Alex Turner @ 2008-01-14 04:43 UTC (permalink / raw)
To: pepone. onrez <pepone.onrez@gmail.com>; +Cc: pgsql-general
If you have to access the data this way (with no where clause at all - which
sometimes you do) then I have already provided a solution that will work
reasonably well. If you create what is essentially a materialized view of
just the id field, the sequence scan will return much fewer pages than when
you do it on the main table. Then you join it to the indexed main table,
and page in just the rows you need. Voila - much faster result. Of course
we haven't really talked about how that will affect insert speed and delete
speed if you trigger then up, but you haven't really talked about any
requirements there.
Alex
On Jan 13, 2008 11:27 PM, pepone. onrez <pepone.onrez@gmail.com> wrote:
> Sorry Alex i forget mention that i have setscan of in my last test.
>
> now I have set seqscan on and indexscan on and added order by _id
>
> The table has an index in the _id field
>
> CREATE INDEX i_documentcontent_document
> ON t_documentcontent
> USING btree
> (_document);
>
> The database was rencently vacum analyze , but not vacun full
>
> here is the explain of 2 diferent queries , when i put a large OFFSET
>
> EXPLAIN ANALYZE SELECT
> t_documentcontent._id AS _id
> FROM t_documentcontent ORDER BY _id LIMIT 50 OFFSET 50000
>
> "Limit (cost=137068.24..137068.36 rows=50 width=58) (actual time=
> 41119.702..41119.792 rows=50 loops=1)"
> " -> Sort (cost=136943.24..137320.26 rows=150807 width=58) (actual
> time=41064.802..41100.424 rows=50050 loops=1)"
> " Sort Key: _id"
> " -> Seq Scan on t_documentcontent (cost= 0.00..110772.07rows=150807 width=58) (actual time=
> 106.679..33267.194 rows=150807 loops=1)"
> "Total runtime: 41120.015 ms"
>
> EXPLAIN ANALYZE SELECT
> t_documentcontent._id AS _id
> FROM t_documentcontent ORDER BY _id LIMIT 50 OFFSET 10000
>
> "Limit (cost=39839.37..40038.56 rows=50 width=58) (actual time=
> 1172.969..1194.228 rows=50 loops=1)"
> " -> Index Scan using i_documentcontent_id on t_documentcontent (cost=
> 0.00..600805.54 rows=150807 width=58) (actual time= 0.077..1189.688rows=10050 loops=1)"
> "Total runtime: 1194.316 ms"
>
> Tom
> i using uuid for the _id field that is the primary key add a WHERE id >
> ? don 't apply
> the cursor aproach is also not suitable for same of my queries
>
> I use this query for paginate contents of a filesysstem with lots of
> documents avoid offset is not posible always
^ permalink raw reply [nested|flat] 47+ messages in thread
* Re: query performance
2008-01-14 02:58 query performance pepone.onrez <pepone.onrez@gmail.com>
2008-01-14 03:39 ` Re: query performance Scott Marlowe <scott.marlowe@gmail.com>
2008-01-14 03:52 ` Re: query performance Tom Lane <tgl@sss.pgh.pa.us>
2008-01-14 04:27 ` Re: query performance pepone.onrez <pepone.onrez@gmail.com>
2008-01-14 04:43 ` Re: query performance Alex Turner <armtuk@gmail.com>
@ 2008-01-14 04:54 ` Alex Turner <armtuk@gmail.com>
2008-01-14 04:59 ` Re: query performance pepone.onrez <pepone.onrez@gmail.com>
0 siblings, 1 reply; 47+ messages in thread
From: Alex Turner @ 2008-01-14 04:54 UTC (permalink / raw)
To: pepone. onrez <pepone.onrez@gmail.com>; +Cc: pgsql-general
Here is a table I threw together to demonstrate the approximate speed of a
materialized view in this case:
trend=# explain analyze select property_id from overview order by
property_id limit 50 offset 50000;
QUERY
PLAN
----------------------------------------------------------------------------------------------------------------------------
Limit (cost=19112.75..19112.88 rows=50 width=8) (actual time=
446.048..446.125 rows=50 loops=1)
-> Sort (cost=18987.75..19400.49 rows=165094 width=8) (actual time=
384.788..424.433 rows=50050 loops=1)
Sort Key: property_id
-> Seq Scan on overview (cost=0.00..2501.94 rows=165094 width=8)
(actual time=0.012..88.691 rows=173409 loops=1)
Total runtime: 447.578 ms
(5 rows)
trend=# select count(*) from overview;
count
--------
173409
(1 row)
trend=#
It's not great - but it's better than 47 seconds (The machine I'm running it
on is far from big iron, so these results should be fairly typical for any
modern x86 box - also this materialized view is almost certainly in RAM, and
therefore IO speed is irrelevant).
Tom lane has already suggested another approach, whereby you order your
results, then select the next 10 from the set where the id is greater than
the greatest of the last one:
select id from overview order by id limit 50;
x = get row['id'] // for row 1
do something
x=get row['id'] // for row 2
do something
...
x=get row['id'] // for row 50
select id from overview where id>x order by id limit 50.
The order by is relevant and infact imperative, because you must order your
results somehow, otherwise your pagination will produce different results
each time you try it as database updates will affect the order the rows come
back by default without an order by clause.
Let me say that again to be clear: The order rows come back if you don't
specify an order by can change! so pulling rows without an order by is a
REALLY bad idea. This will break your pagination if a database update
happens between someone viewing a page and hitting next to view the next
page.
Alex
On Jan 13, 2008 11:43 PM, Alex Turner <armtuk@gmail.com> wrote:
> If you have to access the data this way (with no where clause at all -
> which sometimes you do) then I have already provided a solution that will
> work reasonably well. If you create what is essentially a materialized view
> of just the id field, the sequence scan will return much fewer pages than
> when you do it on the main table. Then you join it to the indexed main
> table, and page in just the rows you need. Voila - much faster result. Of
> course we haven't really talked about how that will affect insert speed and
> delete speed if you trigger then up, but you haven't really talked about any
> requirements there.
>
> Alex
>
>
> On Jan 13, 2008 11:27 PM, pepone. onrez <pepone.onrez@gmail.com> wrote:
>
> > Sorry Alex i forget mention that i have setscan of in my last test.
> >
> > now I have set seqscan on and indexscan on and added order by _id
> >
> > The table has an index in the _id field
> >
> > CREATE INDEX i_documentcontent_document
> > ON t_documentcontent
> > USING btree
> > (_document);
> >
> > The database was rencently vacum analyze , but not vacun full
> >
> > here is the explain of 2 diferent queries , when i put a large OFFSET
> >
> > EXPLAIN ANALYZE SELECT
> > t_documentcontent._id AS _id
> > FROM t_documentcontent ORDER BY _id LIMIT 50 OFFSET 50000
> >
> > "Limit (cost=137068.24..137068.36 rows=50 width=58) (actual time=
> > 41119.702..41119.792 rows=50 loops=1)"
> > " -> Sort (cost=136943.24..137320.26 rows=150807 width=58) (actual
> > time=41064.802..41100.424 rows=50050 loops=1)"
> > " Sort Key: _id"
> > " -> Seq Scan on t_documentcontent (cost= 0.00..110772.07rows=150807 width=58) (actual time=
> > 106.679..33267.194 rows=150807 loops=1)"
> > "Total runtime: 41120.015 ms"
> >
> > EXPLAIN ANALYZE SELECT
> > t_documentcontent._id AS _id
> > FROM t_documentcontent ORDER BY _id LIMIT 50 OFFSET 10000
> >
> > "Limit (cost=39839.37..40038.56 rows=50 width=58) (actual time=
> > 1172.969..1194.228 rows=50 loops=1)"
> > " -> Index Scan using i_documentcontent_id on t_documentcontent
> > (cost=0.00..600805.54 rows=150807 width=58) (actual time=
> > 0.077..1189.688 rows=10050 loops=1)"
> > "Total runtime: 1194.316 ms"
> >
> > Tom
> > i using uuid for the _id field that is the primary key add a WHERE id
> > > ? don 't apply
> > the cursor aproach is also not suitable for same of my queries
> >
> > I use this query for paginate contents of a filesysstem with lots of
> > documents avoid offset is not posible always
>
>
>
^ permalink raw reply [nested|flat] 47+ messages in thread
* Re: query performance
2008-01-14 02:58 query performance pepone.onrez <pepone.onrez@gmail.com>
2008-01-14 03:39 ` Re: query performance Scott Marlowe <scott.marlowe@gmail.com>
2008-01-14 03:52 ` Re: query performance Tom Lane <tgl@sss.pgh.pa.us>
2008-01-14 04:27 ` Re: query performance pepone.onrez <pepone.onrez@gmail.com>
2008-01-14 04:43 ` Re: query performance Alex Turner <armtuk@gmail.com>
2008-01-14 04:54 ` Re: query performance Alex Turner <armtuk@gmail.com>
@ 2008-01-14 04:59 ` pepone.onrez <pepone.onrez@gmail.com>
2008-01-14 05:53 ` Re: query performance Alex Turner <armtuk@gmail.com>
0 siblings, 1 reply; 47+ messages in thread
From: pepone.onrez @ 2008-01-14 04:59 UTC (permalink / raw)
To: pgsql-general
Thanks Alex
I test your solution and is realy more faster.
Nested Loop (cost=1743.31..2044.58 rows=50 width=908) (actual time=
101.695..106.178 rows=50 loops=1)
-> HashAggregate (cost=1743.31..1743.31 rows=50 width=108) (actual time=
101.509..101.567 rows=50 loops=1)
-> Subquery Scan "IN_subquery" (cost=1741.60..1743.19 rows=50
width=108) (actual time=101.327..101.456 rows=50 loops=1)
-> Limit (cost=1741.60..1742.69 rows=50 width=108) (actual
time=101.313..101.383 rows=50 loops=1)
-> Seq Scan on overview
(cost=0.00..3283.07rows=150807 width=108) (actual time=
0.036..72.249 rows=80050 loops=1)
-> Index Scan using i_documentcontent_id on t_documentcontent (cost=
0.00..6.01 rows=1 width=908) (actual time=0.083..0.085 rows=1 loops=50)
Index Cond: ((t_documentcontent._id)::text = ("outer"._id)::text)
Total runtime: 106.323 ms
I now need to see what trigers i need to add, and test the insertions.
Thanks again
On Jan 14, 2008 5:54 AM, Alex Turner <armtuk@gmail.com> wrote:
> Here is a table I threw together to demonstrate the approximate speed of a
> materialized view in this case:
>
> trend=# explain analyze select property_id from overview order by
> property_id limit 50 offset 50000;
> QUERY
> PLAN
>
> ----------------------------------------------------------------------------------------------------------------------------
> Limit (cost=19112.75..19112.88 rows=50 width=8) (actual time=
> 446.048..446.125 rows=50 loops=1)
> -> Sort (cost=18987.75..19400.49 rows=165094 width=8) (actual time=
> 384.788..424.433 rows=50050 loops=1)
> Sort Key: property_id
> -> Seq Scan on overview (cost=0.00..2501.94 rows=165094
> width=8) (actual time= 0.012..88.691 rows=173409 loops=1)
> Total runtime: 447.578 ms
> (5 rows)
>
> trend=# select count(*) from overview;
> count
> --------
> 173409
> (1 row)
>
> trend=#
>
> It's not great - but it's better than 47 seconds (The machine I'm running
> it on is far from big iron, so these results should be fairly typical for
> any modern x86 box - also this materialized view is almost certainly in RAM,
> and therefore IO speed is irrelevant).
>
> Tom lane has already suggested another approach, whereby you order your
> results, then select the next 10 from the set where the id is greater than
> the greatest of the last one:
>
> select id from overview order by id limit 50;
>
> x = get row['id'] // for row 1
> do something
> x=get row['id'] // for row 2
> do something
> ...
> x=get row['id'] // for row 50
>
> select id from overview where id>x order by id limit 50.
>
> The order by is relevant and infact imperative, because you must order
> your results somehow, otherwise your pagination will produce different
> results each time you try it as database updates will affect the order the
> rows come back by default without an order by clause.
>
> Let me say that again to be clear: The order rows come back if you don't
> specify an order by can change! so pulling rows without an order by is a
> REALLY bad idea. This will break your pagination if a database update
> happens between someone viewing a page and hitting next to view the next
> page.
>
> Alex
>
>
> On Jan 13, 2008 11:43 PM, Alex Turner <armtuk@gmail.com> wrote:
>
> > If you have to access the data this way (with no where clause at all -
> > which sometimes you do) then I have already provided a solution that will
> > work reasonably well. If you create what is essentially a materialized view
> > of just the id field, the sequence scan will return much fewer pages than
> > when you do it on the main table. Then you join it to the indexed main
> > table, and page in just the rows you need. Voila - much faster result. Of
> > course we haven't really talked about how that will affect insert speed and
> > delete speed if you trigger then up, but you haven't really talked about any
> > requirements there.
> >
> > Alex
> >
> >
> > On Jan 13, 2008 11:27 PM, pepone. onrez <pepone.onrez@gmail.com > wrote:
> >
> > > Sorry Alex i forget mention that i have setscan of in my last test.
> > >
> > > now I have set seqscan on and indexscan on and added order by _id
> > >
> > > The table has an index in the _id field
> > >
> > > CREATE INDEX i_documentcontent_document
> > > ON t_documentcontent
> > > USING btree
> > > (_document);
> > >
> > > The database was rencently vacum analyze , but not vacun full
> > >
> > > here is the explain of 2 diferent queries , when i put a large OFFSET
> > >
> > > EXPLAIN ANALYZE SELECT
> > > t_documentcontent._id AS _id
> > > FROM t_documentcontent ORDER BY _id LIMIT 50 OFFSET 50000
> > >
> > > "Limit (cost=137068.24..137068.36 rows=50 width=58) (actual time=
> > > 41119.702..41119.792 rows=50 loops=1)"
> > > " -> Sort (cost=136943.24..137320.26 rows=150807 width=58) (actual
> > > time=41064.802..41100.424 rows=50050 loops=1)"
> > > " Sort Key: _id"
> > > " -> Seq Scan on t_documentcontent (cost= 0.00..110772.07rows=150807 width=58) (actual time=
> > > 106.679..33267.194 rows=150807 loops=1)"
> > > "Total runtime: 41120.015 ms"
> > >
> > > EXPLAIN ANALYZE SELECT
> > > t_documentcontent._id AS _id
> > > FROM t_documentcontent ORDER BY _id LIMIT 50 OFFSET 10000
> > >
> > > "Limit (cost=39839.37..40038.56 rows=50 width=58) (actual time=
> > > 1172.969..1194.228 rows=50 loops=1)"
> > > " -> Index Scan using i_documentcontent_id on t_documentcontent
> > > (cost=0.00..600805.54 rows=150807 width=58) (actual time=
> > > 0.077..1189.688 rows=10050 loops=1)"
> > > "Total runtime: 1194.316 ms"
> > >
> > > Tom
> > > i using uuid for the _id field that is the primary key add a WHERE
> > > id > ? don 't apply
> > > the cursor aproach is also not suitable for same of my queries
> > >
> > > I use this query for paginate contents of a filesysstem with lots of
> > > documents avoid offset is not posible always
> >
> >
> >
>
^ permalink raw reply [nested|flat] 47+ messages in thread
* Re: query performance
2008-01-14 02:58 query performance pepone.onrez <pepone.onrez@gmail.com>
2008-01-14 03:39 ` Re: query performance Scott Marlowe <scott.marlowe@gmail.com>
2008-01-14 03:52 ` Re: query performance Tom Lane <tgl@sss.pgh.pa.us>
2008-01-14 04:27 ` Re: query performance pepone.onrez <pepone.onrez@gmail.com>
2008-01-14 04:43 ` Re: query performance Alex Turner <armtuk@gmail.com>
2008-01-14 04:54 ` Re: query performance Alex Turner <armtuk@gmail.com>
2008-01-14 04:59 ` Re: query performance pepone.onrez <pepone.onrez@gmail.com>
@ 2008-01-14 05:53 ` Alex Turner <armtuk@gmail.com>
2008-01-14 05:54 ` Re: query performance Alex Turner <armtuk@gmail.com>
0 siblings, 1 reply; 47+ messages in thread
From: Alex Turner @ 2008-01-14 05:53 UTC (permalink / raw)
To: pepone. onrez <pepone.onrez@gmail.com>; +Cc: pgsql-general
If you combine it with Tom Lane's suggestion - it will go even better,
something like:
select * from t_documentcontent where _id in (select _id from overview where
_id>x order by _id limit 50);
Alex
On Jan 13, 2008 11:59 PM, pepone. onrez <pepone.onrez@gmail.com> wrote:
>
> Thanks Alex
>
> I test your solution and is realy more faster.
>
> Nested Loop (cost=1743.31..2044.58 rows=50 width=908) (actual time=
> 101.695..106.178 rows=50 loops=1)
> -> HashAggregate (cost=1743.31..1743.31 rows=50 width=108) (actual
> time=101.509..101.567 rows=50 loops=1)
> -> Subquery Scan "IN_subquery" (cost=1741.60..1743.19 rows=50
> width=108) (actual time=101.327..101.456 rows=50 loops=1)
> -> Limit (cost=1741.60..1742.69 rows=50 width=108) (actual
> time=101.313..101.383 rows=50 loops=1)
> -> Seq Scan on overview (cost=0.00..3283.07rows=150807 width=108) (actual time=
> 0.036..72.249 rows=80050 loops=1)
> -> Index Scan using i_documentcontent_id on t_documentcontent (cost=
> 0.00..6.01 rows=1 width=908) (actual time=0.083..0.085 rows=1 loops=50)
> Index Cond: ((t_documentcontent._id)::text = ("outer"._id)::text)
> Total runtime: 106.323 ms
>
> I now need to see what trigers i need to add, and test the insertions.
> Thanks again
>
>
> On Jan 14, 2008 5:54 AM, Alex Turner <armtuk@gmail.com> wrote:
>
> > Here is a table I threw together to demonstrate the approximate speed of
> > a materialized view in this case:
> >
> > trend=# explain analyze select property_id from overview order by
> > property_id limit 50 offset 50000;
> > QUERY
> > PLAN
> >
> > ----------------------------------------------------------------------------------------------------------------------------
> > Limit (cost=19112.75..19112.88 rows=50 width=8) (actual time=
> > 446.048..446.125 rows=50 loops=1)
> > -> Sort (cost=18987.75..19400.49 rows=165094 width=8) (actual time=
> > 384.788..424.433 rows=50050 loops=1)
> > Sort Key: property_id
> > -> Seq Scan on overview (cost=0.00..2501.94 rows=165094
> > width=8) (actual time= 0.012..88.691 rows=173409 loops=1)
> > Total runtime: 447.578 ms
> > (5 rows)
> >
> > trend=# select count(*) from overview;
> > count
> > --------
> > 173409
> > (1 row)
> >
> > trend=#
> >
> > It's not great - but it's better than 47 seconds (The machine I'm
> > running it on is far from big iron, so these results should be fairly
> > typical for any modern x86 box - also this materialized view is almost
> > certainly in RAM, and therefore IO speed is irrelevant).
> >
> > Tom lane has already suggested another approach, whereby you order your
> > results, then select the next 10 from the set where the id is greater than
> > the greatest of the last one:
> >
> > select id from overview order by id limit 50;
> >
> > x = get row['id'] // for row 1
> > do something
> > x=get row['id'] // for row 2
> > do something
> > ...
> > x=get row['id'] // for row 50
> >
> > select id from overview where id>x order by id limit 50.
> >
> > The order by is relevant and infact imperative, because you must order
> > your results somehow, otherwise your pagination will produce different
> > results each time you try it as database updates will affect the order the
> > rows come back by default without an order by clause.
> >
> > Let me say that again to be clear: The order rows come back if you
> > don't specify an order by can change! so pulling rows without an order by
> > is a REALLY bad idea. This will break your pagination if a database update
> > happens between someone viewing a page and hitting next to view the next
> > page.
> >
> > Alex
> >
> >
> > On Jan 13, 2008 11:43 PM, Alex Turner <armtuk@gmail.com > wrote:
> >
> > > If you have to access the data this way (with no where clause at all -
> > > which sometimes you do) then I have already provided a solution that will
> > > work reasonably well. If you create what is essentially a materialized view
> > > of just the id field, the sequence scan will return much fewer pages than
> > > when you do it on the main table. Then you join it to the indexed main
> > > table, and page in just the rows you need. Voila - much faster result. Of
> > > course we haven't really talked about how that will affect insert speed and
> > > delete speed if you trigger then up, but you haven't really talked about any
> > > requirements there.
> > >
> > > Alex
> > >
> > >
> > > On Jan 13, 2008 11:27 PM, pepone. onrez <pepone.onrez@gmail.com >
> > > wrote:
> > >
> > > > Sorry Alex i forget mention that i have setscan of in my last test.
> > > >
> > > > now I have set seqscan on and indexscan on and added order by _id
> > > >
> > > > The table has an index in the _id field
> > > >
> > > > CREATE INDEX i_documentcontent_document
> > > > ON t_documentcontent
> > > > USING btree
> > > > (_document);
> > > >
> > > > The database was rencently vacum analyze , but not vacun full
> > > >
> > > > here is the explain of 2 diferent queries , when i put a large
> > > > OFFSET
> > > >
> > > > EXPLAIN ANALYZE SELECT
> > > > t_documentcontent._id AS _id
> > > > FROM t_documentcontent ORDER BY _id LIMIT 50 OFFSET 50000
> > > >
> > > > "Limit (cost=137068.24..137068.36 rows=50 width=58) (actual time=
> > > > 41119.702..41119.792 rows=50 loops=1)"
> > > > " -> Sort (cost=136943.24..137320.26 rows=150807 width=58)
> > > > (actual time=41064.802..41100.424 rows=50050 loops=1)"
> > > > " Sort Key: _id"
> > > > " -> Seq Scan on t_documentcontent (cost= 0.00..110772.07rows=150807 width=58) (actual time=
> > > > 106.679..33267.194 rows=150807 loops=1)"
> > > > "Total runtime: 41120.015 ms"
> > > >
> > > > EXPLAIN ANALYZE SELECT
> > > > t_documentcontent._id AS _id
> > > > FROM t_documentcontent ORDER BY _id LIMIT 50 OFFSET 10000
> > > >
> > > > "Limit (cost=39839.37..40038.56 rows=50 width=58) (actual time=
> > > > 1172.969..1194.228 rows=50 loops=1)"
> > > > " -> Index Scan using i_documentcontent_id on t_documentcontent
> > > > (cost=0.00..600805.54 rows=150807 width=58) (actual time=
> > > > 0.077..1189.688 rows=10050 loops=1)"
> > > > "Total runtime: 1194.316 ms"
> > > >
> > > > Tom
> > > > i using uuid for the _id field that is the primary key add a WHERE
> > > > id > ? don 't apply
> > > > the cursor aproach is also not suitable for same of my queries
> > > >
> > > > I use this query for paginate contents of a filesysstem with lots of
> > > > documents avoid offset is not posible always
> > >
> > >
> > >
> >
>
^ permalink raw reply [nested|flat] 47+ messages in thread
* Re: query performance
2008-01-14 02:58 query performance pepone.onrez <pepone.onrez@gmail.com>
2008-01-14 03:39 ` Re: query performance Scott Marlowe <scott.marlowe@gmail.com>
2008-01-14 03:52 ` Re: query performance Tom Lane <tgl@sss.pgh.pa.us>
2008-01-14 04:27 ` Re: query performance pepone.onrez <pepone.onrez@gmail.com>
2008-01-14 04:43 ` Re: query performance Alex Turner <armtuk@gmail.com>
2008-01-14 04:54 ` Re: query performance Alex Turner <armtuk@gmail.com>
2008-01-14 04:59 ` Re: query performance pepone.onrez <pepone.onrez@gmail.com>
2008-01-14 05:53 ` Re: query performance Alex Turner <armtuk@gmail.com>
@ 2008-01-14 05:54 ` Alex Turner <armtuk@gmail.com>
0 siblings, 0 replies; 47+ messages in thread
From: Alex Turner @ 2008-01-14 05:54 UTC (permalink / raw)
To: pepone. onrez <pepone.onrez@gmail.com>; +Cc: pgsql-general
Oh - if you do this then make sure that you have the primary key index on
overview too.
Alex
On Jan 14, 2008 12:53 AM, Alex Turner <armtuk@gmail.com> wrote:
> If you combine it with Tom Lane's suggestion - it will go even better,
> something like:
>
> select * from t_documentcontent where _id in (select _id from overview
> where _id>x order by _id limit 50);
>
> Alex
>
>
> On Jan 13, 2008 11:59 PM, pepone. onrez <pepone.onrez@gmail.com> wrote:
>
> >
> > Thanks Alex
> >
> > I test your solution and is realy more faster.
> >
> > Nested Loop (cost=1743.31..2044.58 rows=50 width=908) (actual time=
> > 101.695..106.178 rows=50 loops=1)
> > -> HashAggregate (cost=1743.31..1743.31 rows=50 width=108) (actual
> > time=101.509..101.567 rows=50 loops=1)
> > -> Subquery Scan "IN_subquery" (cost=1741.60..1743.19 rows=50
> > width=108) (actual time=101.327..101.456 rows=50 loops=1)
> > -> Limit (cost=1741.60..1742.69 rows=50 width=108)
> > (actual time=101.313..101.383 rows=50 loops=1)
> > -> Seq Scan on overview (cost=0.00..3283.07rows=150807 width=108) (actual time=
> > 0.036..72.249 rows=80050 loops=1)
> > -> Index Scan using i_documentcontent_id on t_documentcontent (cost=
> > 0.00..6.01 rows=1 width=908) (actual time=0.083..0.085 rows=1 loops=50)
> > Index Cond: ((t_documentcontent._id)::text =
> > ("outer"._id)::text)
> > Total runtime: 106.323 ms
> >
> > I now need to see what trigers i need to add, and test the insertions.
> > Thanks again
> >
> >
> > On Jan 14, 2008 5:54 AM, Alex Turner < armtuk@gmail.com> wrote:
> >
> > > Here is a table I threw together to demonstrate the approximate speed
> > > of a materialized view in this case:
> > >
> > > trend=# explain analyze select property_id from overview order by
> > > property_id limit 50 offset 50000;
> > > QUERY
> > > PLAN
> > >
> > > ----------------------------------------------------------------------------------------------------------------------------
> > > Limit (cost=19112.75..19112.88 rows=50 width=8) (actual time=
> > > 446.048..446.125 rows=50 loops=1)
> > > -> Sort (cost=18987.75..19400.49 rows=165094 width=8) (actual
> > > time=384.788..424.433 rows=50050 loops=1)
> > > Sort Key: property_id
> > > -> Seq Scan on overview (cost=0.00..2501.94 rows=165094
> > > width=8) (actual time= 0.012..88.691 rows=173409 loops=1)
> > > Total runtime: 447.578 ms
> > > (5 rows)
> > >
> > > trend=# select count(*) from overview;
> > > count
> > > --------
> > > 173409
> > > (1 row)
> > >
> > > trend=#
> > >
> > > It's not great - but it's better than 47 seconds (The machine I'm
> > > running it on is far from big iron, so these results should be fairly
> > > typical for any modern x86 box - also this materialized view is almost
> > > certainly in RAM, and therefore IO speed is irrelevant).
> > >
> > > Tom lane has already suggested another approach, whereby you order
> > > your results, then select the next 10 from the set where the id is greater
> > > than the greatest of the last one:
> > >
> > > select id from overview order by id limit 50;
> > >
> > > x = get row['id'] // for row 1
> > > do something
> > > x=get row['id'] // for row 2
> > > do something
> > > ...
> > > x=get row['id'] // for row 50
> > >
> > > select id from overview where id>x order by id limit 50.
> > >
> > > The order by is relevant and infact imperative, because you must order
> > > your results somehow, otherwise your pagination will produce different
> > > results each time you try it as database updates will affect the order the
> > > rows come back by default without an order by clause.
> > >
> > > Let me say that again to be clear: The order rows come back if you
> > > don't specify an order by can change! so pulling rows without an order by
> > > is a REALLY bad idea. This will break your pagination if a database update
> > > happens between someone viewing a page and hitting next to view the next
> > > page.
> > >
> > > Alex
> > >
> > >
> > > On Jan 13, 2008 11:43 PM, Alex Turner <armtuk@gmail.com > wrote:
> > >
> > > > If you have to access the data this way (with no where clause at all
> > > > - which sometimes you do) then I have already provided a solution that will
> > > > work reasonably well. If you create what is essentially a materialized view
> > > > of just the id field, the sequence scan will return much fewer pages than
> > > > when you do it on the main table. Then you join it to the indexed main
> > > > table, and page in just the rows you need. Voila - much faster result. Of
> > > > course we haven't really talked about how that will affect insert speed and
> > > > delete speed if you trigger then up, but you haven't really talked about any
> > > > requirements there.
> > > >
> > > > Alex
> > > >
> > > >
> > > > On Jan 13, 2008 11:27 PM, pepone. onrez <pepone.onrez@gmail.com >
> > > > wrote:
> > > >
> > > > > Sorry Alex i forget mention that i have setscan of in my last
> > > > > test.
> > > > >
> > > > > now I have set seqscan on and indexscan on and added order by
> > > > > _id
> > > > >
> > > > > The table has an index in the _id field
> > > > >
> > > > > CREATE INDEX i_documentcontent_document
> > > > > ON t_documentcontent
> > > > > USING btree
> > > > > (_document);
> > > > >
> > > > > The database was rencently vacum analyze , but not vacun full
> > > > >
> > > > > here is the explain of 2 diferent queries , when i put a large
> > > > > OFFSET
> > > > >
> > > > > EXPLAIN ANALYZE SELECT
> > > > > t_documentcontent._id AS _id
> > > > > FROM t_documentcontent ORDER BY _id LIMIT 50 OFFSET 50000
> > > > >
> > > > > "Limit (cost=137068.24..137068.36 rows=50 width=58) (actual time=
> > > > > 41119.702..41119.792 rows=50 loops=1)"
> > > > > " -> Sort (cost=136943.24..137320.26 rows=150807 width=58)
> > > > > (actual time=41064.802..41100.424 rows=50050 loops=1)"
> > > > > " Sort Key: _id"
> > > > > " -> Seq Scan on t_documentcontent (cost= 0.00..110772.07rows=150807 width=58) (actual time=
> > > > > 106.679..33267.194 rows=150807 loops=1)"
> > > > > "Total runtime: 41120.015 ms"
> > > > >
> > > > > EXPLAIN ANALYZE SELECT
> > > > > t_documentcontent._id AS _id
> > > > > FROM t_documentcontent ORDER BY _id LIMIT 50 OFFSET 10000
> > > > >
> > > > > "Limit (cost=39839.37..40038.56 rows=50 width=58) (actual time=
> > > > > 1172.969..1194.228 rows=50 loops=1)"
> > > > > " -> Index Scan using i_documentcontent_id on t_documentcontent
> > > > > (cost=0.00..600805.54 rows=150807 width=58) (actual time=
> > > > > 0.077..1189.688 rows=10050 loops=1)"
> > > > > "Total runtime: 1194.316 ms"
> > > > >
> > > > > Tom
> > > > > i using uuid for the _id field that is the primary key add a
> > > > > WHERE id > ? don 't apply
> > > > > the cursor aproach is also not suitable for same of my queries
> > > > >
> > > > > I use this query for paginate contents of a filesysstem with lots
> > > > > of documents avoid offset is not posible always
> > > >
> > > >
> > > >
> > >
> >
>
^ permalink raw reply [nested|flat] 47+ messages in thread
* Re: query performance
2008-01-14 02:58 query performance pepone.onrez <pepone.onrez@gmail.com>
@ 2008-01-17 09:40 ` Alban Hertroys <dalroi@solfertje.student.utwente.nl>
2 siblings, 0 replies; 47+ messages in thread
From: Alban Hertroys @ 2008-01-17 09:40 UTC (permalink / raw)
To: pepone.onrez <pepone.onrez@gmail.com>; +Cc: pgsql-general
On Jan 14, 2008, at 3:58 AM, pepone.onrez wrote:
> I have this query in a table with 150 thowsand tuples and it takes
> to long
>
> t_documentcontent._id AS _id
> FROM t_documentcontent LIMIT 50 OFFSET 80000
You want an ORDER BY there. Not only will it probably speed things
up, without it there's no guaranteed order in the results returned.
As table records have no specific order and updates and inserts on
that table take the first free position, you risk ending up showing
some records twice in your set (same id on different 'pages') and
missing others because you've already skipped past them when there's
concurrent access.
There's still some risk for that if you order, but as you seem to
order on a sequence-generated column, updates aren't a problem for
you and inserts end up at the end anyway. But only if you order them.
Alban Hertroys
--
If you can't see the forest for the trees,
cut the trees and you'll see there is no forest.
!DSPAM:737,478f1e139491365710960!
^ permalink raw reply [nested|flat] 47+ messages in thread
* query performance
@ 2008-04-23 19:37 Brian Cox <brian.cox@ca.com>
2008-04-23 20:42 ` Re: query performance Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 47+ messages in thread
From: Brian Cox @ 2008-04-23 19:37 UTC (permalink / raw)
To: pgsql-general
I have a largish (pg_dump output is 4G) database. The query:
select count(*) from some-table
was taking 120 secs to report that there were 151,000+ rows.
This seemed very slow. This db gets vacuum'd regularly (at least once
per day). I also did a manual 'vacuum analyze', but after it completed,
the query ran no faster. However, after dumping the database and
recreating it
from the backup, the same query takes 2 secs.
Why the dramatic decrease? Would 'vacuum full' have achieved the
same performance improvements? Is there anything else that needs to be done
regularly to prevent this performance degradation?
postgresql 8.1.3 running on redhat es 4.
Thanks,
Brian
^ permalink raw reply [nested|flat] 47+ messages in thread
* Re: query performance
2008-04-23 19:37 query performance Brian Cox <brian.cox@ca.com>
@ 2008-04-23 20:42 ` Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 0 replies; 47+ messages in thread
From: Tom Lane @ 2008-04-23 20:42 UTC (permalink / raw)
To: Brian Cox <brian.cox@ca.com>; +Cc: pgsql-general
Brian Cox <brian.cox@ca.com> writes:
> I have a largish (pg_dump output is 4G) database. The query:
> select count(*) from some-table
> was taking 120 secs to report that there were 151,000+ rows.
> This seemed very slow. This db gets vacuum'd regularly (at least once
> per day). I also did a manual 'vacuum analyze', but after it completed,
> the query ran no faster. However, after dumping the database and
> recreating it from the backup, the same query takes 2 secs.
> Why the dramatic decrease?
Presumably, the table was really bloated (lots of unused space).
> Would 'vacuum full' have achieved the
> same performance improvements?
It would've compacted the table all right, but probably left the indexes
worse off.
> Is there anything else that needs to be done
> regularly to prevent this performance degradation?
I suspect that your FSM settings are too low, causing free space found
by VACUUM to be forgotten about. You might also need to consider
vacuuming more than once a day (there's a tradeoff between how often
you vacuum and how much FSM space you need).
regards, tom lane
^ permalink raw reply [nested|flat] 47+ messages in thread
* Re: query performance
@ 2008-04-24 23:59 Brian Cox <brian.cox@ca.com>
2008-04-25 00:12 ` Re: query performance Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 47+ messages in thread
From: Brian Cox @ 2008-04-24 23:59 UTC (permalink / raw)
To: pgsql-general
Tom Lane [tgl@sss.pgh.pa.us] wrote:
> I suspect that your FSM settings are too low, causing free space found
> by VACUUM to be forgotten about.
I read in another thread that vacuum verbose would tell me how much FSM
is needed, but I ran it and didn't see any output about this. What is
the way to determine how much FSM is needed (other than wait for hints
in the log).
You might also need to consider
> vacuuming more than once a day (there's a tradeoff between how often
> you vacuum and how much FSM space you need).
What is the trade-off? Anyway to predict how much more (presumably) FSM
is needed if you vacuum more often?
Thanks,
Brian
^ permalink raw reply [nested|flat] 47+ messages in thread
* Re: query performance
2008-04-24 23:59 Re: query performance Brian Cox <brian.cox@ca.com>
@ 2008-04-25 00:12 ` Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 0 replies; 47+ messages in thread
From: Tom Lane @ 2008-04-25 00:12 UTC (permalink / raw)
To: Brian Cox <brian.cox@ca.com>; +Cc: pgsql-general
Brian Cox <brian.cox@ca.com> writes:
> I read in another thread that vacuum verbose would tell me how much FSM
> is needed, but I ran it and didn't see any output about this.
You need a database-wide vacuum verbose (not just 1 table) to get that
output ...
regards, tom lane
^ permalink raw reply [nested|flat] 47+ messages in thread
* Re: query performance
@ 2008-04-25 00:23 Brian Cox <brian.cox@ca.com>
2008-04-25 00:29 ` Re: query performance Scott Marlowe <scott.marlowe@gmail.com>
2008-04-25 00:34 ` Re: query performance Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 2 replies; 47+ messages in thread
From: Brian Cox @ 2008-04-25 00:23 UTC (permalink / raw)
To: pgsql-general
Tom Lane [tgl@sss.pgh.pa.us] wrote:
> You need a database-wide vacuum verbose (not just 1 table) to get that
> output ...
I ran:
> pgsql -U admin -d cemdb -c 'vacuum verbose' > /tmp/pgvac.log 2>&1
the output file has 2593 lines and, while I haven't looked at all of
them, a:
> fgrep -i fsm /tmp/pgvac.log
returns no lines.
Any hints as to where the FSM info is in this file?
Thanks,
Brian
^ permalink raw reply [nested|flat] 47+ messages in thread
* Re: query performance
2008-04-25 00:23 Re: query performance Brian Cox <brian.cox@ca.com>
@ 2008-04-25 00:29 ` Scott Marlowe <scott.marlowe@gmail.com>
1 sibling, 0 replies; 47+ messages in thread
From: Scott Marlowe @ 2008-04-25 00:29 UTC (permalink / raw)
To: Brian Cox <brian.cox@ca.com>; +Cc: pgsql-general
On Thu, Apr 24, 2008 at 6:23 PM, Brian Cox <brian.cox@ca.com> wrote:
> Tom Lane [tgl@sss.pgh.pa.us] wrote:
>
> > You need a database-wide vacuum verbose (not just 1 table) to get that
> > output ...
> >
>
> I ran:
>
> > pgsql -U admin -d cemdb -c 'vacuum verbose' > /tmp/pgvac.log 2>&1
>
> the output file has 2593 lines and, while I haven't looked at all of them,
> a:
>
> > fgrep -i fsm /tmp/pgvac.log
>
> returns no lines.
>
> Any hints as to where the FSM info is in this file?
There's bits spread throughout the file, but the summary is at the bottom.
^ permalink raw reply [nested|flat] 47+ messages in thread
* Re: query performance
2008-04-25 00:23 Re: query performance Brian Cox <brian.cox@ca.com>
@ 2008-04-25 00:34 ` Tom Lane <tgl@sss.pgh.pa.us>
1 sibling, 0 replies; 47+ messages in thread
From: Tom Lane @ 2008-04-25 00:34 UTC (permalink / raw)
To: Brian Cox <brian.cox@ca.com>; +Cc: pgsql-general
Brian Cox <brian.cox@ca.com> writes:
> Any hints as to where the FSM info is in this file?
At the very end ... you're looking for these messages:
ereport(elevel,
(errmsg("free space map contains %d pages in %d relations",
storedPages, numRels),
errdetail("A total of %.0f page slots are in use (including overhead).\n"
"%.0f page slots are required to track all free space.\n"
"Current limits are: %d page slots, %d relations, using %.0f kB.",
Min(needed, MaxFSMPages),
needed,
MaxFSMPages, MaxFSMRelations,
(double) FreeSpaceShmemSize() / 1024.0)));
if (numRels == MaxFSMRelations)
ereport(elevel,
(errmsg("max_fsm_relations(%d) equals the number of relations checked",
MaxFSMRelations),
errhint("You have at least %d relations. "
"Consider increasing the configuration parameter \"max_fsm_relations\".",
numRels)));
else if (needed > MaxFSMPages)
ereport(elevel,
(errmsg("number of page slots needed (%.0f) exceeds max_fsm_pages (%d)",
needed, MaxFSMPages),
errhint("Consider increasing the configuration parameter \"max_fsm_pages\" "
"to a value over %.0f.", needed)));
regards, tom lane
^ permalink raw reply [nested|flat] 47+ messages in thread
* Re: query performance
@ 2008-04-25 00:33 Brian Cox <brian.cox@ca.com>
0 siblings, 0 replies; 47+ messages in thread
From: Brian Cox @ 2008-04-25 00:33 UTC (permalink / raw)
To: pgsql-general
Scott Marlowe [scott.marlowe@gmail.com] wrote:
> There's bits spread throughout the file, but the summary is at the bottom.
Here's a tail of the 'vacuum verbose' output:
INFO: vacuuming "pg_toast.pg_toast_797619965"
INFO: index "pg_toast_797619965_index" now contains 0 row versions in 1
pages
DETAIL: 0 index pages have been deleted, 0 are currently reusable.
CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: "pg_toast_797619965": found 0 removable, 0 nonremovable row
versions in 0 pages
DETAIL: 0 dead row versions cannot be removed yet.
There were 0 unused item pointers.
0 pages are entirely empty.
CPU 0.00s/0.00u sec elapsed 0.00 sec.
VACUUM
I don't see anything that looks like a "summary".
Thanks,
Brian
^ permalink raw reply [nested|flat] 47+ messages in thread
* Re: query performance
@ 2008-04-25 00:41 Brian Cox <brian.cox@ca.com>
2008-04-25 00:52 ` Re: query performance Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 47+ messages in thread
From: Brian Cox @ 2008-04-25 00:41 UTC (permalink / raw)
To: pgsql-general
Tom Lane [tgl@sss.pgh.pa.us] wrote:
> At the very end ... you're looking for these messages:
>
> ereport(elevel,
> (errmsg("free space map contains %d pages in %d relations",
> storedPages, numRels),
> errdetail("A total of %.0f page slots are in use (including
> overhead).\n"
> "%.0f page slots are required to track all free space.\n"
> "Current limits are: %d page slots, %d relations, using %.0f
> kB.",
> Min(needed, MaxFSMPages),
> needed,
> MaxFSMPages, MaxFSMRelations,
> (double) FreeSpaceShmemSize() / 1024.0)));
>
> if (numRels == MaxFSMRelations)
> ereport(elevel,
> (errmsg("max_fsm_relations(%d) equals the number of
> relations checked",
> MaxFSMRelations),
> errhint("You have at least %d relations. "
> "Consider increasing the configuration
> parameter \"max_fsm_relations\".",
> numRels)));
> else if (needed > MaxFSMPages)
> ereport(elevel,
> (errmsg("number of page slots needed (%.0f) exceeds
> max_fsm_pages (%d)",
> needed, MaxFSMPages),
> errhint("Consider increasing the configuration
> parameter \"max_fsm_pages\" "
> "to a value over %.0f.", needed)));
>
> regards, tom lane
>
The following greps of the vacuum verbose output return no lines:
fgrep -i fsm
fgrep 'free space'
fgrep 'page slots'
fgrep 'relations'
I've already posted the tail of this output previously.
I conclude that these lines are not in this file. Where
did they go?
Thanks,
Brian
^ permalink raw reply [nested|flat] 47+ messages in thread
* Re: query performance
2008-04-25 00:41 Re: query performance Brian Cox <brian.cox@ca.com>
@ 2008-04-25 00:52 ` Tom Lane <tgl@sss.pgh.pa.us>
2008-04-25 03:51 ` Re: query performance Cox, Brian <Brian.Cox@ca.com>
0 siblings, 1 reply; 47+ messages in thread
From: Tom Lane @ 2008-04-25 00:52 UTC (permalink / raw)
To: Brian Cox <brian.cox@ca.com>; +Cc: pgsql-general
Brian Cox <brian.cox@ca.com> writes:
> I've already posted the tail of this output previously.
> I conclude that these lines are not in this file. Where
> did they go?
[ scratches head... ] Your example command works as expected for me.
[ rereads thread... ] Oh, you're running 8.1. I think you have to
do the command as a superuser to get that output in 8.1. Later versions
are less picky.
regards, tom lane
^ permalink raw reply [nested|flat] 47+ messages in thread
* Re: query performance
2008-04-25 00:41 Re: query performance Brian Cox <brian.cox@ca.com>
2008-04-25 00:52 ` Re: query performance Tom Lane <tgl@sss.pgh.pa.us>
@ 2008-04-25 03:51 ` Cox, Brian <Brian.Cox@ca.com>
0 siblings, 0 replies; 47+ messages in thread
From: Cox, Brian @ 2008-04-25 03:51 UTC (permalink / raw)
To: pgsql-general
> [ scratches head... ] Your example command works as expected for me.
> [ rereads thread... ] Oh, you're running 8.1. I think you have to
> do the command as a superuser to get that output in 8.1. Later versions
> are less picky.
Yes, with the right incantations, the FSM information does appear. The perils
of being a bit behind the times, I guess.
Thanks for your help,
Brian
^ permalink raw reply [nested|flat] 47+ messages in thread
* query performance
@ 2018-02-17 23:35 hmidi slim <hmidi.slim2@gmail.com>
2018-02-18 05:37 ` Re: query performance David Rowley <david.rowley@2ndquadrant.com>
2018-02-20 21:36 ` Re: query performance PT <wmoran@potentialtech.com>
0 siblings, 2 replies; 47+ messages in thread
From: hmidi slim @ 2018-02-17 23:35 UTC (permalink / raw)
To: pgsql-general
Hi,
I have two tables: establishment which contains these columns: id, name,
longitude, latitude, geom (Geometric column)
Product contains: id, name, establishment_id
First of all I want to select the establishment within a radius.
I run this query:
select e.name, e1.name
from establishment as e, establishment as e1
where e.id <> e1.id
and e1.id = 1
and ST_DWithin(geom, ST_MakePoint(e1.longitude, e1.latitude)::geography,
1000)
The result of this query returns all the establishment within a radius 1KM
from from a given establishment which has an id = 1.
After that I want to get the product's name of each establishment from the
query's result.
Is there an other optimized solution to make a query such this:
select * from (
select e.name, e1.name, e.id
from establishment as e, establishment as e1
where e.id <> e1.id
and e1.id = 1
and ST_DWithin(geom, ST_MakePoint(e1.longitude, e1.latitude)::geography,
1000)) as tmp inner join product as p on p.establishment_id = tmp.id
^ permalink raw reply [nested|flat] 47+ messages in thread
* Re: query performance
2018-02-17 23:35 query performance hmidi slim <hmidi.slim2@gmail.com>
@ 2018-02-18 05:37 ` David Rowley <david.rowley@2ndquadrant.com>
2018-02-18 14:18 ` Re: query performance Tomas Vondra <tomas.vondra@2ndquadrant.com>
1 sibling, 1 reply; 47+ messages in thread
From: David Rowley @ 2018-02-18 05:37 UTC (permalink / raw)
To: hmidi slim <hmidi.slim2@gmail.com>; +Cc: pgsql-general
On 18 February 2018 at 12:35, hmidi slim <hmidi.slim2@gmail.com> wrote:
> Is there an other optimized solution to make a query such this:
> select * from (
> select e.name, e1.name, e.id
> from establishment as e, establishment as e1
> where e.id <> e1.id
> and e1.id = 1
> and ST_DWithin(geom, ST_MakePoint(e1.longitude, e1.latitude)::geography,
> 1000)) as tmp inner join product as p on p.establishment_id = tmp.id
You really should state what is wrong with the performance of the
above version and also include the EXPLAIN (ANALYZE, BUFFERS) of that
query.
The version of PostgreSQL that you're running it on is also a good
thing to share.
Details of the indexes which you've defined on the tables are also
useful information. It would especially be good to know if you've
added an index on product (establishment_id), for example.
--
David Rowley http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
^ permalink raw reply [nested|flat] 47+ messages in thread
* Re: query performance
2018-02-17 23:35 query performance hmidi slim <hmidi.slim2@gmail.com>
2018-02-18 05:37 ` Re: query performance David Rowley <david.rowley@2ndquadrant.com>
@ 2018-02-18 14:18 ` Tomas Vondra <tomas.vondra@2ndquadrant.com>
0 siblings, 0 replies; 47+ messages in thread
From: Tomas Vondra @ 2018-02-18 14:18 UTC (permalink / raw)
To: David Rowley <david.rowley@2ndquadrant.com>; hmidi slim <hmidi.slim2@gmail.com>; +Cc: pgsql-general
On 02/18/2018 06:37 AM, David Rowley wrote:
> On 18 February 2018 at 12:35, hmidi slim <hmidi.slim2@gmail.com> wrote:
>> Is there an other optimized solution to make a query such this:
>> select * from (
>> select e.name, e1.name, e.id
>> from establishment as e, establishment as e1
>> where e.id <> e1.id
>> and e1.id = 1
>> and ST_DWithin(geom, ST_MakePoint(e1.longitude, e1.latitude)::geography,
>> 1000)) as tmp inner join product as p on p.establishment_id = tmp.id
>
> You really should state what is wrong with the performance of the
> above version and also include the EXPLAIN (ANALYZE, BUFFERS) of that
> query.
>
> The version of PostgreSQL that you're running it on is also a good
> thing to share.
>
> Details of the indexes which you've defined on the tables are also
> useful information. It would especially be good to know if you've
> added an index on product (establishment_id), for example.
>
There's actually a wiki page with overview of what should be included in
"slow query" questions:
https://wiki.postgresql.org/wiki/Slow_Query_Questions
regards
--
Tomas Vondra http://www.2ndQuadrant.com
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
^ permalink raw reply [nested|flat] 47+ messages in thread
* Re: query performance
2018-02-17 23:35 query performance hmidi slim <hmidi.slim2@gmail.com>
@ 2018-02-20 21:36 ` PT <wmoran@potentialtech.com>
1 sibling, 0 replies; 47+ messages in thread
From: PT @ 2018-02-20 21:36 UTC (permalink / raw)
To: hmidi slim <hmidi.slim2@gmail.com>; +Cc: pgsql-general
On Sun, 18 Feb 2018 00:35:18 +0100
hmidi slim <hmidi.slim2@gmail.com> wrote:
> Hi,
> I have two tables: establishment which contains these columns: id, name,
> longitude, latitude, geom (Geometric column)
> Product contains: id, name, establishment_id
> First of all I want to select the establishment within a radius.
> I run this query:
> select e.name, e1.name
> from establishment as e, establishment as e1
> where e.id <> e1.id
> and e1.id = 1
> and ST_DWithin(geom, ST_MakePoint(e1.longitude, e1.latitude)::geography,
> 1000)
>
> The result of this query returns all the establishment within a radius 1KM
> from from a given establishment which has an id = 1.
>
> After that I want to get the product's name of each establishment from the
> query's result.
>
> Is there an other optimized solution to make a query such this:
> select * from (
> select e.name, e1.name, e.id
> from establishment as e, establishment as e1
> where e.id <> e1.id
> and e1.id = 1
> and ST_DWithin(geom, ST_MakePoint(e1.longitude, e1.latitude)::geography,
> 1000)) as tmp inner join product as p on p.establishment_id = tmp.id
Did you not see my earlier response?
--
Bill Moran
^ permalink raw reply [nested|flat] 47+ messages in thread
* Query performance
@ 2018-03-26 16:46 hmidi slim <hmidi.slim2@gmail.com>
0 siblings, 0 replies; 47+ messages in thread
From: hmidi slim @ 2018-03-26 16:46 UTC (permalink / raw)
To: pgsql-general
I want to compare the performance of some queries so I created those tables:
*CREATE TABLE reservation1 (room int, name varchar(255), during
daterange);CREATE TABLE reservation2 (room int, name varchar(255),
start_date date, end_date date);*
then:
*INSERT INTO public.reservation1(name, room, during) select
concat('room ', a), a, '[2010-01-01, 2010-04-02)' from
generate_series(1,10000000) as a;INSERT INTO public.reservation2(name,
room, start_date, end_date) select concat('room ', a), a, '2010-01-01',
'2010-04-02' from generate_series(1,10000000) as a;*
I create an index for during column:
*CREATE INDEX reservation1_idx ON reservation1 USING GIST (during);*
I'm using the operator contains (@>) and overlaps (&&):
*select * from reservation1 where during @>
'[2010-02-15,2010-03-02)';select * from reservation1 where during &&
'[2010-02-15,2010-03-02)'; *
And I got such result time:
* 25s*
However when I use this query:
*select * from reservation2 where ('2010-02-15' between start_date and
end_date) and ('2010-03-02' between start_date and end_date);*
The result time was:
* 9s.*
I understand that the index does not have any effect when the amount of
fetched data was huge and the query planner used the seq scan method. I
want to know if it is not recommended to use rang types and operator to get
good performance or should I add something to the queries to be faster?
^ permalink raw reply [nested|flat] 47+ messages in thread
* Re: Query Performance
@ 2022-09-17 05:28 sivapostgres@yahoo.com <sivapostgres@yahoo.com>
2022-09-17 08:48 ` Re: Query Performance Ron <ronljohnsonjr@gmail.com>
2022-09-17 10:03 ` Re: Query Performance Peter J. Holzer <hjp-pgsql@hjp.at>
0 siblings, 2 replies; 47+ messages in thread
From: sivapostgres@yahoo.com @ 2022-09-17 05:28 UTC (permalink / raw)
To: pgsql-general
Hello,
My query is like this
Select a.field1, a.field2, a.field3From (Select a.field1, b.field2, c.field3 From table1 a Join table2 b on b.something = a.something Join table3 c On c.something = a.something Where a.field7 = 'value'
UNION ALL
Select a.field4, a.field5, a.field6 From table11 a Join table21 b On b.something = a.something Where a.field8 = 'something' ) aJoin table10 bOn b.field11 = (Select c.field11 From table10 c Where c.field10 = a.field1 ) <- instead of a.field1, if I hardcode value (eg. '100') query runs fasterJoin table21 cOn c.something = a.something...
In the above query, If I substitute a value for a.field1, query runs faster. Any suggestion/guidance/links to improve the query performance without substituting the value ?
Happiness Always
BKR Sivaprakash
^ permalink raw reply [nested|flat] 47+ messages in thread
* Re: Query Performance
2022-09-17 05:28 Re: Query Performance sivapostgres@yahoo.com <sivapostgres@yahoo.com>
@ 2022-09-17 08:48 ` Ron <ronljohnsonjr@gmail.com>
1 sibling, 0 replies; 47+ messages in thread
From: Ron @ 2022-09-17 08:48 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
On 9/17/22 00:28, sivapostgres@yahoo.com wrote:
> Hello,
>
> My query is like this
>
> Select a.field1, a.field2, a.field3
> From (Select a.field1, b.field2, c.field3
> From table1 a
> Join table2 b
> on b.something = a.something
> Join table3 c
> On c.something = a.something
> Where a.field7 = 'value'
>
> UNION ALL
>
> Select a.field4, a.field5, a.field6
> From table11 a
> Join table21 b
> On b.something = a.something
> Where a.field8 = 'something' ) a
> Join table10 b
> On b.field11 = (Select c.field11
> From table10 c
> Where c.field10 = a.field1 ) <-
> instead of a.field1, if I hardcode value (eg. '100') query runs faster
> Join table21 c
> On c.something = a.something
> ...
>
>
> In the above query, If I substitute a value for a.field1, query runs faster.
> Any suggestion/guidance/links to improve the query performance without
> substituting the value ?
Is there an index on table1.field1?
Have you vacuumed or analyzed table1 lately?
Have you decomposed the query into the smallest possible query that
demonstrates the problem?
--
Angular momentum makes the world go 'round.
^ permalink raw reply [nested|flat] 47+ messages in thread
* Re: Query Performance
2022-09-17 05:28 Re: Query Performance sivapostgres@yahoo.com <sivapostgres@yahoo.com>
@ 2022-09-17 10:03 ` Peter J. Holzer <hjp-pgsql@hjp.at>
2022-09-17 11:32 ` Re: Query Performance sivapostgres@yahoo.com <sivapostgres@yahoo.com>
1 sibling, 1 reply; 47+ messages in thread
From: Peter J. Holzer @ 2022-09-17 10:03 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
On 2022-09-17 05:28:25 +0000, sivapostgres@yahoo.com wrote:
> My query is like this
>
> Select a.field1, a.field2, a.field3
> From (Select a.field1, b.field2, c.field3
> From table1 a
> Join table2 b
> on b.something = a.something
> Join table3 c
> On c.something = a.something
> Where a.field7 = 'value'
>
> UNION ALL
>
> Select a.field4, a.field5, a.field6
> From table11 a
> Join table21 b
> On b.something = a.something
> Where a.field8 = 'something' ) a
> Join table10 b
> On b.field11 = (Select c.field11
> From table10 c
> Where c.field10 = a.field1 ) <-
> instead of a.field1, if I hardcode value (eg. '100') query runs faster
> Join table21 c
> On c.something = a.something
> ...
Well, you are now searching table10 for a constant value (which can be
done once) instead of the output of the union (which has to be done for
each line of the union, so I'm not surprised that it's faster.
What is the output of `explain (analyze)` for the two queries? Is there
an obvious place where an index would help? Can you restructure the
query?
BTW, it is (at least for me) very hard to give advice on a query with
only completely abstract names like `table11` or `field4`: I have no
idea what this is supposed to do, so it's hard to tell if there is a
better way. Using `a` to refer to 3 different things doesn't help
either.
hp
--
_ | Peter J. Holzer | Story must make more sense than reality.
|_|_) | |
| | | hjp@hjp.at | -- Charles Stross, "Creative writing
__/ | http://www.hjp.at/ | challenge!"
Attachments:
signature.asc (application/pgp-signature, 832B) download
^ permalink raw reply [nested|flat] 47+ messages in thread
* Re: Query Performance
2022-09-17 05:28 Re: Query Performance sivapostgres@yahoo.com <sivapostgres@yahoo.com>
2022-09-17 10:03 ` Re: Query Performance Peter J. Holzer <hjp-pgsql@hjp.at>
@ 2022-09-17 11:32 ` sivapostgres@yahoo.com <sivapostgres@yahoo.com>
0 siblings, 0 replies; 47+ messages in thread
From: sivapostgres@yahoo.com @ 2022-09-17 11:32 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
I should have given you the full query. Here it is
Select a.locationname, a.usercode, a.itemname, a.uomname, a.batchnumber, a.expirydate, a.itemnamefk, a.itemuomfk, f.itemgroupname, a.locationpk, Sum(a.quantity) as quantity, Sum(a.freequantity) as freequantity, b.beforetaxrate as beforetaxrate, (b.rate / b.rateper) as purchaserate, b.netrate as netrate, b.netsalesrate as netsalesrate, b.effectiverate as effectiverate, b.rateper, a.reportformat, g.standardcost, g.defaultpurchaserate, g.salesrateone, g.salesratetwo, g.salesratethree, g.salesratefour, g.salesratefive, g.salesratesix, g.salesrateseven, g.salesrateeight, g.salesratenine, 'N' as negativerate From (Select k.locationname, a.usercode, a.itemname, c.uomname, j.batchnumber, j.expirydate, j.itemnamefk, j.itemuomfk, k.locationpk, j.receivedquantity as quantity, j.receivedfreequantity as freequantity, c.reportformat From in_item_name a Join in_item_uom b On b.itemnamefk = a.itemnamepk Join gl_uom c On c.uompk = b.uomfk Join view_item_receipts j On j.itemnamefk = a.itemnamepk And j.itemuomfk = b.itemuompk Join in_location k On k.locationpk = j.locationfk Where j.companycode = 'SDM' And j.branchcode = '001' And j.accountperiodid = 1 And j.voucherdate <= '2022/09/17' And j.billstatus <> 'C' And j.topparentcode <> 4 And (j.receivedquantity <> 0 Or j.receivedfreequantity <> 0) UNION ALL Select k.locationname, a.usercode, a.itemname, c.uomname, j.batchnumber, j.expirydate, j.itemnamefk, j.itemuomfk, k.locationpk, (j.issuedquantity * -1) as quantity, (j.issuedfreequantity * -1) as freequantity, c.reportformat From in_item_name a Join in_item_uom b On b.itemnamefk = a.itemnamepk Join gl_uom c On c.uompk = b.uomfk Join view_item_issues j On j.itemnamefk = a.itemnamepk And j.itemuomfk = b.itemuompk Join in_location k On k.locationpk = j.locationfk Where j.companycode = 'SDM' And j.branchcode = '001' And j.accountperiodid = 1 And j.voucherdate <= '2022/09/17' And j.billstatus <> 'C' And j.topparentcode <> 4 And (j.issuedquantity <> 0 Or j.issuedfreequantity <> 0)) a Left Outer Join view_item_receipts b <- It's actually a view of 4 tables which tries to arrive the last purchase rate On b.itemreceiptspk = (Select c.itemreceiptspk From view_item_receipts c Where c.companycode = 'SDM' And c.branchcode = '001' And c.accountperiodid = 1 And c.voucherdate <= '2022/09/17' And c.billstatus <> 'C' And c.itemnamefk = a.itemnamefk And c.itemuomfk = a.itemuomfk And c.batchnumber = a.batchnumber And c.expirydate = a.expirydate And (c.receivedquantity <> 0 Or c.receivedfreequantity <> 0) Order by c.voucherdate desc, c.vouchernumber desc, c.sequencenumber desc Limit 1 ) Join in_item_name c On c.itemnamepk = a.itemnamefk Join in_item_group f On f.itemgrouppk = c.itemgroupfk Left Outer Join in_item_rate g On g.itemuomfk = b.itemuomfk And g.itemnamefk = b.itemnamefk And '2022/09/17' between g.fromdate and g.todate Group By a.locationname, a.usercode, a.itemname, a.uomname, a.batchnumber, a.expirydate, a.itemnamefk, a.itemuomfk, f.itemgroupname, a.locationpk, b.beforetaxrate, b.rate, b.netrate, b.netsalesrate, b.effectiverate, b.rateper, a.reportformat, g.standardcost, g.defaultpurchaserate, g.salesrateone, g.salesratetwo, g.salesratethree, g.salesratefour, g.salesratefive, g.salesratesix, g.salesrateseven, g.salesrateeight, g.salesratenine Having (sum(a.quantity) + sum(a.freequantity)) <> 0 Order by 1, 3, 2, 5
Create a index for companycode, branchcode, c.accountperiodid, voucherdate, billstatus, itemnamefk, itemuomfk, batchnumber, expirydate, receivedquantity, receivedfreequantity
in all the 4 tables that this view got.
Happiness Always
BKR Sivaprakash
On Saturday, 17 September, 2022 at 03:33:48 pm IST, Peter J. Holzer <hjp-pgsql@hjp.at> wrote:
On 2022-09-17 05:28:25 +0000, sivapostgres@yahoo.com wrote:
> My query is like this
>
> Select a.field1, a.field2, a.field3
> From (Select a.field1, b.field2, c.field3
> From table1 a
> Join table2 b
> on b.something = a.something
> Join table3 c
> On c.something = a.something
> Where a.field7 = 'value'
>
> UNION ALL
>
> Select a.field4, a.field5, a.field6
> From table11 a
> Join table21 b
> On b.something = a.something
> Where a.field8 = 'something' ) a
> Join table10 b
> On b.field11 = (Select c.field11
> From table10 c
> Where c.field10 = a.field1 ) <-
> instead of a.field1, if I hardcode value (eg. '100') query runs faster
> Join table21 c
> On c.something = a.something
> ...
Well, you are now searching table10 for a constant value (which can be
done once) instead of the output of the union (which has to be done for
each line of the union, so I'm not surprised that it's faster.
What is the output of `explain (analyze)` for the two queries? Is there
an obvious place where an index would help? Can you restructure the
query?
BTW, it is (at least for me) very hard to give advice on a query with
only completely abstract names like `table11` or `field4`: I have no
idea what this is supposed to do, so it's hard to tell if there is a
better way. Using `a` to refer to 3 different things doesn't help
either.
hp
--
_ | Peter J. Holzer | Story must make more sense than reality.
|_|_) | |
| | | hjp@hjp.at | -- Charles Stross, "Creative writing
__/ | http://www.hjp.at/ | challenge!"
^ permalink raw reply [nested|flat] 47+ messages in thread
end of thread, other threads:[~2022-09-17 11:32 UTC | newest]
Thread overview: 47+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2002-07-17 20:31 Query Performance... Jeffrey Hood <jhood@hmcon.com>
2002-07-18 12:10 ` Martijn van Oosterhout <kleptog@svana.org>
2002-07-18 14:08 ` Tom Lane <tgl@sss.pgh.pa.us>
2002-07-18 15:43 ` Jeffrey Hood <jhood@hmcon.com>
2002-07-20 20:54 ` Tom Lane <tgl@sss.pgh.pa.us>
2002-07-21 20:48 ` Jeffrey Hood <jhood@hmcon.com>
2006-08-03 08:12 Query performance Christian Rengstl <Christian.Rengstl@klinik.uni-regensburg.de>
2006-08-03 08:37 ` Re: Query performance Nikolay Samokhvalov <samokhvalov@gmail.com>
2006-08-03 09:57 ` Re: Query performance Chris Mair <chrisnospam@1006.org>
2006-08-03 08:34 Re: Query performance Hakan Kocaman <Hakan.Kocaman@digame.de>
2006-08-03 08:47 ` Re: Query performance Christian Rengstl <Christian.Rengstl@klinik.uni-regensburg.de>
2006-08-03 09:00 ` Re: Query performance Richard Huxton <dev@archonet.com>
2006-08-03 09:08 Re: Query performance Hakan Kocaman <Hakan.Kocaman@digame.de>
2006-08-03 09:17 ` Re: Query performance Christian Rengstl <Christian.Rengstl@klinik.uni-regensburg.de>
2006-08-03 09:36 Re: Query performance Hakan Kocaman <Hakan.Kocaman@digame.de>
2006-08-03 10:27 ` Re: Query performance Christian Rengstl <Christian.Rengstl@klinik.uni-regensburg.de>
2008-01-14 02:58 query performance pepone.onrez <pepone.onrez@gmail.com>
2008-01-14 03:30 ` Re: query performance Alex Turner <armtuk@gmail.com>
2008-01-14 03:39 ` Re: query performance Scott Marlowe <scott.marlowe@gmail.com>
2008-01-14 03:52 ` Re: query performance Tom Lane <tgl@sss.pgh.pa.us>
2008-01-14 04:27 ` Re: query performance pepone.onrez <pepone.onrez@gmail.com>
2008-01-14 04:43 ` Re: query performance Alex Turner <armtuk@gmail.com>
2008-01-14 04:54 ` Re: query performance Alex Turner <armtuk@gmail.com>
2008-01-14 04:59 ` Re: query performance pepone.onrez <pepone.onrez@gmail.com>
2008-01-14 05:53 ` Re: query performance Alex Turner <armtuk@gmail.com>
2008-01-14 05:54 ` Re: query performance Alex Turner <armtuk@gmail.com>
2008-01-17 09:40 ` Re: query performance Alban Hertroys <dalroi@solfertje.student.utwente.nl>
2008-04-23 19:37 query performance Brian Cox <brian.cox@ca.com>
2008-04-23 20:42 ` Re: query performance Tom Lane <tgl@sss.pgh.pa.us>
2008-04-24 23:59 Re: query performance Brian Cox <brian.cox@ca.com>
2008-04-25 00:12 ` Re: query performance Tom Lane <tgl@sss.pgh.pa.us>
2008-04-25 00:23 Re: query performance Brian Cox <brian.cox@ca.com>
2008-04-25 00:29 ` Re: query performance Scott Marlowe <scott.marlowe@gmail.com>
2008-04-25 00:34 ` Re: query performance Tom Lane <tgl@sss.pgh.pa.us>
2008-04-25 00:33 Re: query performance Brian Cox <brian.cox@ca.com>
2008-04-25 00:41 Re: query performance Brian Cox <brian.cox@ca.com>
2008-04-25 00:52 ` Re: query performance Tom Lane <tgl@sss.pgh.pa.us>
2008-04-25 03:51 ` Re: query performance Cox, Brian <Brian.Cox@ca.com>
2018-02-17 23:35 query performance hmidi slim <hmidi.slim2@gmail.com>
2018-02-18 05:37 ` Re: query performance David Rowley <david.rowley@2ndquadrant.com>
2018-02-18 14:18 ` Re: query performance Tomas Vondra <tomas.vondra@2ndquadrant.com>
2018-02-20 21:36 ` Re: query performance PT <wmoran@potentialtech.com>
2018-03-26 16:46 Query performance hmidi slim <hmidi.slim2@gmail.com>
2022-09-17 05:28 Re: Query Performance sivapostgres@yahoo.com <sivapostgres@yahoo.com>
2022-09-17 08:48 ` Re: Query Performance Ron <ronljohnsonjr@gmail.com>
2022-09-17 10:03 ` Re: Query Performance Peter J. Holzer <hjp-pgsql@hjp.at>
2022-09-17 11:32 ` Re: Query Performance sivapostgres@yahoo.com <sivapostgres@yahoo.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox