pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedSlow SELECT
21+ messages / 10 participants
[nested] [flat]
* Slow SELECT
@ 2003-10-02 10:03 psql-mail@freeuk.com
2003-10-03 16:50 ` Re: Slow SELECT Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 21+ messages in thread
From: psql-mail@freeuk.com @ 2003-10-02 10:03 UTC (permalink / raw)
To: pgsql-general
I am running a SELECT to get all tuples within a given date range. This
query is much slwoer than i expected - am i missing something?
I have a table 'meta' with a column 'in_date' of type timestamp(0), i
am trying to select all
records within a given date range. I have an index on 'in_date' and I
also have an index on date(in_date). The queries I am doing are between
dates rather than timestamps.
Immeadiately prior to running the queries shown below a VACUUM ANALYZE
was run.
The query planner seems to be out by a factor of 10 for cost and number
of rows. Is this this because of the slow performance?
I have 6 million records. With dates spread fairly evenly between the
end of 2001 and now.
I was very suprised to see the query take over 20 minutes when using
the date(in_date) index. And more suprised to see the seq_scan over in_
date using timestamps take only 10 minutes.
Both are taking too long in my opinion! I was hoping for less than 10
seconds. Is this too optimistic?
Any suggestions much appreciated.
I am using RH_AS_3 on IBM x450 quad xeon ia64, 4GB mem (1GB shared
buffers for
postmaster)
When running queries the processor its running on sits down at 15-20%
usage and the iowait goes up to 80-99% (fiber attached raid(0) yes i
know its not resiliant).
testdb=# EXPLAIN ANALYZE SELECT item_id, in_date FROM meta WHERE date(
in_date) >= '2002-03-01' AND date(in_date) < '2002-04-01' order by in_
date DESC;
QUERY PLAN
------------------------------------------------------------------------
------------------------------------------------------------------------
------------------
Sort (cost=122755.65..122830.64 rows=29996 width=50) (actual time=
1248326.17..1248608.39 rows=261305 loops=1)
Sort Key: in_date
-> Index Scan using meta_in_date_date_index on meta (cost=0.00..
120525.09 rows=29996 width=50) (actual time=0.00..1244835.94 rows=
261305 loops=1)
Index Cond: ((date(in_date) >= '2002-03-01'::date) AND (date(in_date) <
'2002-04-01'::date))
Total runtime: 1248887.70 msec
(5 rows)
Here are the stats on the in_date column if they're any use...
testdb=# SELECT * FROM pg_stats WHERE tablename = 'meta' and attname = '
in_date';
schemaname | tablename | attname | null_frac | avg_width | n_distinct |
most_common_vals | most_common_freqs |
histogram_bounds |
correlation
------------+-----------+----------+-----------+-----------+------------
+------------------+-------------------+--------------------------------
------------------------------------------------------------------------
------------------------------------------------------------------------
---------------------------------------------------------------------+--
------------
public | meta | in_date | 0 | 8 | -1 |
| | {"2001-10-18 17:28:23","2001-12-28 19:31:06","
2002-03-14 19:59:08","2002-05-27 08:28:04","2002-07-31 14:06:06","2002-
10-09 19:09:49","2002-12-21 03:58:46","2003-03-02 21:41:37","2003-05-09
16:12:39","2003-07-22 05:13:18","2003-09-30 13:48:04"} | -0.000184019
(1 row)
Here is the same query as above but using timestamp(0)'s instead of
dates.
testdb=# EXPLAIN ANALYZE SELECT item_id, in_date FROM meta WHERE in_
date >= '2002-03-01' AND in_date < '2002-04-01' order by in_date DESC;
QUERY PLAN
------------------------------------------------------------------------
------------------------------------------------------------------------
--------
Sort (cost=797371.98..797995.09 rows=249246 width=50) (actual time=
616906.25..617183.58 rows=261305 loops=1)
Sort Key: in_date
-> Seq Scan on meta (cost=0.00..775030.55 rows=249246 width=50) (
actual time=19.53..611541.03 rows=261305 loops=1)
Filter: ((in_date >= '2002-03-01 00:00:00'::timestamp without time zone)
AND (in_date < '2002-04-01 00:00:00'::timestamp without time zone))
Total runtime: 617446.29 msec
(5 rows)
--
^ permalink raw reply [nested|flat] 21+ messages in thread
* Re: Slow SELECT
2003-10-02 10:03 Slow SELECT psql-mail@freeuk.com
@ 2003-10-03 16:50 ` Tom Lane <tgl@sss.pgh.pa.us>
2003-10-08 19:25 ` Re: Slow SELECT Mat <psql-mail@freeuk.com>
0 siblings, 1 reply; 21+ messages in thread
From: Tom Lane @ 2003-10-03 16:50 UTC (permalink / raw)
To: psql-mail@freeuk.com; +Cc: pgsql-general
psql-mail@freeuk.com writes:
> I am running a SELECT to get all tuples within a given date range. This
> query is much slwoer than i expected - am i missing something?
Well, it seems to be running at about 5 msec/row, which would be quite
respectable if each fetch required another disk seek. I'm wondering why
you are (apparently) not managing to get more than one row per page
fetched. What are your configuration settings --- particularly
shared_buffers? Could we see the output of VACUUM VERBOSE for this
table?
regards, tom lane
^ permalink raw reply [nested|flat] 21+ messages in thread
* Re: Slow SELECT
2003-10-02 10:03 Slow SELECT psql-mail@freeuk.com
2003-10-03 16:50 ` Re: Slow SELECT Tom Lane <tgl@sss.pgh.pa.us>
@ 2003-10-08 19:25 ` Mat <psql-mail@freeuk.com>
2003-10-09 07:36 ` Re: Slow SELECT Shridhar Daithankar <shridhar_daithankar@persistent.co.in>
2003-10-13 21:54 ` Re: Slow SELECT Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 2 replies; 21+ messages in thread
From: Mat @ 2003-10-08 19:25 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-general
On Fri, 2003-10-03 at 17:50, Tom Lane wrote:
> psql-mail@freeuk.com writes:
> > I am running a SELECT to get all tuples within a given date range. This
> > query is much slwoer than i expected - am i missing something?
>
> Well, it seems to be running at about 5 msec/row, which would be quite
> respectable if each fetch required another disk seek. I'm wondering why
> you are (apparently) not managing to get more than one row per page
> fetched. What are your configuration settings --- particularly
> shared_buffers? Could we see the output of VACUUM VERBOSE for this
> table?
>
> regards, tom lane
>
> ---------------------------(end of broadcast)---------------------------
> TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org
Tom,
Thanks for your comments.
Here is the information you requested:
The date column is in the public.meta table.
public.data contains bodies of text to be searched.
DATA:
Lines from postgresql.conf that don't start with a '#':
tcpip_socket = true
shared_buffers = 126976 #992 MB
sort_mem = 36864 #36 MB
vacuum_mem = 73696 #72 MB
stats_start_collector = true
stats_command_string = true
stats_row_level = true
stats_block_level = true
LC_MESSAGES = 'en_US.UTF-8'
LC_MONETARY = 'en_US.UTF-8'
LC_NUMERIC = 'en_US.UTF-8'
LC_TIME = 'en_US.UTF-8'
testdb=# VACUUM VERBOSE;
INFO: --Relation pg_catalog.pg_conversion--
INFO: Pages 2: Changed 0, Empty 0; Tup 114: Vac 0, Keep 0, UnUsed 0.
Total CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: --Relation pg_catalog.pg_depend--
INFO: Pages 22: Changed 0, Empty 0; Tup 2947: Vac 0, Keep 0, UnUsed 1.
Total CPU 0.00s/0.00u sec elapsed 0.03 sec.
INFO: --Relation pg_catalog.pg_attrdef--
INFO: Pages 1: Changed 0, Empty 0; Tup 6: Vac 0, Keep 0, UnUsed 0.
Total CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: --Relation pg_toast.pg_toast_16384--
INFO: Pages 0: Changed 0, Empty 0; Tup 0: Vac 0, Keep 0, UnUsed 0.
Total CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: --Relation pg_catalog.pg_constraint--
INFO: Pages 1: Changed 0, Empty 0; Tup 20: Vac 0, Keep 0, UnUsed 0.
Total CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: --Relation pg_toast.pg_toast_16386--
INFO: Pages 0: Changed 0, Empty 0; Tup 0: Vac 0, Keep 0, UnUsed 0.
Total CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: --Relation pg_catalog.pg_database--
INFO: Pages 1: Changed 0, Empty 0; Tup 4: Vac 0, Keep 0, UnUsed 3.
Total CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: --Relation pg_toast.pg_toast_1262--
INFO: Pages 0: Changed 0, Empty 0; Tup 0: Vac 0, Keep 0, UnUsed 0.
Total CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: --Relation pg_catalog.pg_description--
INFO: Pages 12: Changed 0, Empty 0; Tup 1390: Vac 0, Keep 0, UnUsed 0.
Total CPU 0.00s/0.00u sec elapsed 0.01 sec.
INFO: --Relation pg_toast.pg_toast_16416--
INFO: Pages 0: Changed 0, Empty 0; Tup 0: Vac 0, Keep 0, UnUsed 0.
Total CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: --Relation pg_catalog.pg_group--
INFO: Pages 0: Changed 0, Empty 0; Tup 0: Vac 0, Keep 0, UnUsed 0.
Total CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: --Relation pg_toast.pg_toast_1261--
INFO: Pages 0: Changed 0, Empty 0; Tup 0: Vac 0, Keep 0, UnUsed 0.
Total CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: --Relation pg_catalog.pg_proc--
INFO: Pages 59: Changed 0, Empty 0; Tup 1492: Vac 0, Keep 0, UnUsed
179.
Total CPU 0.00s/0.00u sec elapsed 0.02 sec.
INFO: --Relation pg_toast.pg_toast_1255--
INFO: Pages 0: Changed 0, Empty 0; Tup 0: Vac 0, Keep 0, UnUsed 0.
Total CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: --Relation pg_catalog.pg_rewrite--
INFO: Pages 4: Changed 0, Empty 0; Tup 27: Vac 0, Keep 0, UnUsed 0.
Total CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: --Relation pg_toast.pg_toast_16410--
INFO: Pages 4: Changed 0, Empty 0; Tup 16: Vac 0, Keep 0, UnUsed 1.
Total CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: --Relation pg_catalog.pg_type--
INFO: Pages 4: Changed 0, Empty 0; Tup 190: Vac 0, Keep 0, UnUsed 0.
Total CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: --Relation pg_catalog.pg_attribute--
INFO: Pages 18: Changed 0, Empty 0; Tup 1062: Vac 0, Keep 0, UnUsed 7.
Total CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: --Relation pg_catalog.pg_class--
INFO: Pages 5: Changed 0, Empty 0; Tup 175: Vac 0, Keep 0, UnUsed 71.
Total CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: --Relation pg_catalog.pg_inherits--
INFO: Pages 0: Changed 0, Empty 0; Tup 0: Vac 0, Keep 0, UnUsed 0.
Total CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: --Relation pg_catalog.pg_index--
INFO: Pages 3: Changed 0, Empty 0; Tup 94: Vac 0, Keep 0, UnUsed 1.
Total CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: --Relation pg_catalog.pg_operator--
INFO: Pages 14: Changed 0, Empty 0; Tup 643: Vac 0, Keep 0, UnUsed 0.
Total CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: --Relation pg_catalog.pg_opclass--
INFO: Pages 1: Changed 0, Empty 0; Tup 51: Vac 0, Keep 0, UnUsed 0.
Total CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: --Relation pg_catalog.pg_am--
INFO: Pages 1: Changed 0, Empty 0; Tup 4: Vac 0, Keep 0, UnUsed 0.
Total CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: --Relation pg_catalog.pg_amop--
INFO: Pages 1: Changed 0, Empty 0; Tup 180: Vac 0, Keep 0, UnUsed 0.
Total CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: --Relation pg_catalog.pg_amproc--
INFO: Pages 1: Changed 0, Empty 0; Tup 57: Vac 0, Keep 0, UnUsed 0.
Total CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: --Relation pg_catalog.pg_language--
INFO: Pages 1: Changed 0, Empty 0; Tup 3: Vac 0, Keep 0, UnUsed 3.
Total CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: --Relation pg_catalog.pg_largeobject--
INFO: Pages 0: Changed 0, Empty 0; Tup 0: Vac 0, Keep 0, UnUsed 0.
Total CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: --Relation pg_catalog.pg_shadow--
INFO: Pages 1: Changed 0, Empty 0; Tup 2: Vac 0, Keep 0, UnUsed 0.
Total CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: --Relation pg_toast.pg_toast_1260--
INFO: Pages 0: Changed 0, Empty 0; Tup 0: Vac 0, Keep 0, UnUsed 0.
Total CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: --Relation pg_catalog.pg_aggregate--
INFO: Pages 1: Changed 0, Empty 0; Tup 60: Vac 0, Keep 0, UnUsed 0.
Total CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: --Relation pg_catalog.pg_trigger--
INFO: Pages 1: Changed 0, Empty 0; Tup 26: Vac 0, Keep 0, UnUsed 0.
Total CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: --Relation pg_catalog.pg_listener--
INFO: Pages 0: Changed 0, Empty 0; Tup 0: Vac 0, Keep 0, UnUsed 0.
Total CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: --Relation pg_catalog.pg_cast--
INFO: Pages 2: Changed 0, Empty 0; Tup 174: Vac 0, Keep 0, UnUsed 0.
Total CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: --Relation pg_catalog.pg_namespace--
INFO: Pages 1: Changed 0, Empty 0; Tup 4: Vac 0, Keep 0, UnUsed 0.
Total CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: --Relation pg_catalog.pg_statistic--
INFO: Pages 17: Changed 0, Empty 0; Tup 164: Vac 0, Keep 0, UnUsed 320.
Total CPU 0.00s/0.00u sec elapsed 0.04 sec.
INFO: --Relation pg_toast.pg_toast_16408--
INFO: Pages 5: Changed 0, Empty 0; Tup 6: Vac 0, Keep 0, UnUsed 15.
Total CPU 0.00s/0.00u sec elapsed 0.02 sec.
INFO: --Relation public.meta--
INFO: Pages 685043: Changed 0, Empty 8; Tup 5999170: Vac 0, Keep 0,
UnUsed 5999170.
Total CPU 18.06s/3.61u sec elapsed 612.91 sec.
INFO: --Relation pg_toast.pg_toast_16989--
INFO: Pages 1: Changed 0, Empty 0; Tup 4: Vac 0, Keep 0, UnUsed 0.
Total CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: --Relation public.data--
INFO: Pages 1359266: Changed 0, Empty 24; Tup 5999174: Vac 0, Keep 0,
UnUsed 1595705.
Total CPU 33.74s/6.64u sec elapsed 1436.04 sec.
INFO: --Relation pg_toast.pg_toast_16979--
INFO: Pages 1154272: Changed 0, Empty 9; Tup 4454208: Vac 0, Keep 0,
UnUsed 1074682.
Total CPU 28.98s/5.65u sec elapsed 1159.64 sec.
INFO: --Relation public.sys_id_ng_id_link--
INFO: Pages 76254: Changed 0, Empty 1; Tup 11960406: Vac 0, Keep 0,
UnUsed 10064.
Total CPU 1.96s/1.10u sec elapsed 97.64 sec.
INFO: --Relation public.src_grp--
INFO: Pages 202: Changed 0, Empty 0; Tup 23501: Vac 0, Keep 0, UnUsed
0.
Total CPU 0.00s/0.00u sec elapsed 0.24 sec.
INFO: --Relation pg_toast.pg_toast_17009--
INFO: Pages 0: Changed 0, Empty 0; Tup 0: Vac 0, Keep 0, UnUsed 0.
Total CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: --Relation public.meta2--
INFO: Pages 0: Changed 0, Empty 0; Tup 0: Vac 0, Keep 0, UnUsed 0.
Total CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: --Relation pg_toast.pg_toast_17041--
INFO: Pages 0: Changed 0, Empty 0; Tup 0: Vac 0, Keep 0, UnUsed 0.
Total CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: --Relation public.data2--
INFO: Pages 0: Changed 0, Empty 0; Tup 0: Vac 0, Keep 0, UnUsed 0.
Total CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: --Relation pg_toast.pg_toast_17031--
INFO: Pages 0: Changed 0, Empty 0; Tup 0: Vac 0, Keep 0, UnUsed 0.
Total CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: --Relation public.src_grp2--
INFO: Pages 0: Changed 0, Empty 0; Tup 0: Vac 0, Keep 0, UnUsed 0.
Total CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: --Relation pg_toast.pg_toast_17061--
INFO: Pages 0: Changed 0, Empty 0; Tup 0: Vac 0, Keep 0, UnUsed 0.
Total CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: --Relation public.data2_group_link--
INFO: Pages 0: Changed 0, Empty 0; Tup 0: Vac 0, Keep 0, UnUsed 0.
Total CPU 0.00s/0.00u sec elapsed 0.00 sec.
^ permalink raw reply [nested|flat] 21+ messages in thread
* Re: Slow SELECT
2003-10-02 10:03 Slow SELECT psql-mail@freeuk.com
2003-10-03 16:50 ` Re: Slow SELECT Tom Lane <tgl@sss.pgh.pa.us>
2003-10-08 19:25 ` Re: Slow SELECT Mat <psql-mail@freeuk.com>
@ 2003-10-09 07:36 ` Shridhar Daithankar <shridhar_daithankar@persistent.co.in>
1 sibling, 0 replies; 21+ messages in thread
From: Shridhar Daithankar @ 2003-10-09 07:36 UTC (permalink / raw)
To: ; +Cc: pgsql-general
Mat wrote:
> Lines from postgresql.conf that don't start with a '#':
>
> tcpip_socket = true
> shared_buffers = 126976 #992 MB
> sort_mem = 36864 #36 MB
> vacuum_mem = 73696 #72 MB
I would suggest scale down shared buffers to 128 or 64MB and set effective cache
size correct. That should help.
HTH
Shridhar
^ permalink raw reply [nested|flat] 21+ messages in thread
* Re: Slow SELECT
2003-10-02 10:03 Slow SELECT psql-mail@freeuk.com
2003-10-03 16:50 ` Re: Slow SELECT Tom Lane <tgl@sss.pgh.pa.us>
2003-10-08 19:25 ` Re: Slow SELECT Mat <psql-mail@freeuk.com>
@ 2003-10-13 21:54 ` Tom Lane <tgl@sss.pgh.pa.us>
1 sibling, 0 replies; 21+ messages in thread
From: Tom Lane @ 2003-10-13 21:54 UTC (permalink / raw)
To: Mat <psql-mail@freeuk.com>; +Cc: pgsql-general
Mat <psql-mail@freeuk.com> writes:
> On Fri, 2003-10-03 at 17:50, Tom Lane wrote:
>> Well, it seems to be running at about 5 msec/row, which would be quite
>> respectable if each fetch required another disk seek. I'm wondering why
>> you are (apparently) not managing to get more than one row per page
>> fetched. What are your configuration settings --- particularly
>> shared_buffers? Could we see the output of VACUUM VERBOSE for this
>> table?
> Lines from postgresql.conf that don't start with a '#':
> shared_buffers = 126976 #992 MB
As someone else pointed out, that is way too large (unless maybe you
have 4Gb of RAM, and even then I'd not counsel making shared_buffers
that large).
> INFO: --Relation public.meta--
> INFO: Pages 685043: Changed 0, Empty 8; Tup 5999170: Vac 0, Keep 0,
> UnUsed 5999170.
> Total CPU 18.06s/3.61u sec elapsed 612.91 sec.
This shows you've got less than 9 tuples per 8k disk page on average.
Is the table very wide? If it doesn't seem to you that the tuples
should occupy 1K apiece, it might be that you need to run a VACUUM FULL
to get rid of some excess free space. (If so, it's a sign that you need
to increase the FSM settings in postgresql.conf and/or run plain VACUUM
more frequently, so that the free space doesn't get away from you
again.)
regards, tom lane
^ permalink raw reply [nested|flat] 21+ messages in thread
* Slow SELECT
@ 2020-05-26 07:22 Frank Millman <frank@chagford.com>
2020-05-26 07:32 ` Re: Slow SELECT Olivier Gautherot <ogautherot@gautherot.net>
2020-05-26 10:02 ` Re: Slow SELECT Christian Ramseyer <rc@networkz.ch>
2020-05-26 10:04 ` Re: Slow SELECT David Rowley <dgrowleyml@gmail.com>
0 siblings, 3 replies; 21+ messages in thread
From: Frank Millman @ 2020-05-26 07:22 UTC (permalink / raw)
To: pgsql-general
Hi all
I have a SELECT that runs over 5 times slower on PostgreSQL compared
with Sql Server and sqlite3. I am trying to understand why.
I have a table that looks like this (simplified) -
CREATE TABLE my_table (
row_id SERIAL PRIMARY KEY,
deleted_id INT DEFAULT 0,
fld_1 INT REFERENCES table_1(row_id),
fld_2 INT REFERENCES table_2(row_id),
fld_3 INT REFERENCES table_3(row_id),
fld_4 INT REFERENCES table_4(row_id),
tran_date DATE,
tran_total DEC(21,2)
);
CREATE UNIQUE INDEX my_table_ndx ON my_table (fld_1, fld_2, fld_3,
fld_4, tran_date) WHERE deleted_id = 0;
The table sizes are -
my_table : 167 rows
table_1 : 21 rows
table_2 : 11 rows
table_3 : 3 rows
table_4 : 16 rows
Therefore for each tran_date in my_table there are potentially
21x11x3x16 = 11088 rows. Most will be null.
I want to select the row_id for the last tran_date for each of those
potential groups. This is my select -
SELECT (
SELECT a.row_id FROM my_table a
WHERE a.fld_1 = b.row_id
AND a.fld_2 = c.row_id
AND a.fld_3 = d.row_id
AND a.fld_4 = e.row_id
AND a.deleted_id = 0
ORDER BY a.tran_date DESC LIMIT 1
)
FROM table_1 b, table_2 c, table_3 d, table_4 e
Out of 11088 rows selected, 103 are not null.
On identical data, this takes 0.06 sec on SQL Server, 0.04 sec on
sqlite3, and 0.31 sec on PostgreSQL.
I have looked at the EXPLAIN, but I don't really know what to look for.
I can supply it if that would help.
Thanks for any advice.
Frank Millman
^ permalink raw reply [nested|flat] 21+ messages in thread
* Re: Slow SELECT
2020-05-26 07:22 Slow SELECT Frank Millman <frank@chagford.com>
@ 2020-05-26 07:32 ` Olivier Gautherot <ogautherot@gautherot.net>
2020-05-26 08:38 ` Re: Slow SELECT Frank Millman <frank@chagford.com>
2 siblings, 1 reply; 21+ messages in thread
From: Olivier Gautherot @ 2020-05-26 07:32 UTC (permalink / raw)
To: Frank Millman <frank@chagford.com>; +Cc: pgsql-general
Hi Frank,
On Tue, May 26, 2020 at 9:23 AM Frank Millman <frank@chagford.com> wrote:
> Hi all
>
> I have a SELECT that runs over 5 times slower on PostgreSQL compared
> with Sql Server and sqlite3. I am trying to understand why.
>
> I have a table that looks like this (simplified) -
>
> CREATE TABLE my_table (
> row_id SERIAL PRIMARY KEY,
> deleted_id INT DEFAULT 0,
> fld_1 INT REFERENCES table_1(row_id),
> fld_2 INT REFERENCES table_2(row_id),
> fld_3 INT REFERENCES table_3(row_id),
> fld_4 INT REFERENCES table_4(row_id),
> tran_date DATE,
> tran_total DEC(21,2)
> );
>
> CREATE UNIQUE INDEX my_table_ndx ON my_table (fld_1, fld_2, fld_3,
> fld_4, tran_date) WHERE deleted_id = 0;
>
> The table sizes are -
> my_table : 167 rows
> table_1 : 21 rows
> table_2 : 11 rows
> table_3 : 3 rows
> table_4 : 16 rows
>
> Therefore for each tran_date in my_table there are potentially
> 21x11x3x16 = 11088 rows. Most will be null.
>
> I want to select the row_id for the last tran_date for each of those
> potential groups. This is my select -
>
> SELECT (
> SELECT a.row_id FROM my_table a
> WHERE a.fld_1 = b.row_id
> AND a.fld_2 = c.row_id
> AND a.fld_3 = d.row_id
> AND a.fld_4 = e.row_id
> AND a.deleted_id = 0
> ORDER BY a.tran_date DESC LIMIT 1
> )
> FROM table_1 b, table_2 c, table_3 d, table_4 e
>
> Out of 11088 rows selected, 103 are not null.
>
> On identical data, this takes 0.06 sec on SQL Server, 0.04 sec on
> sqlite3, and 0.31 sec on PostgreSQL.
>
SQL Server does a good job at caching data in memory. PostgreSQL does too
on consecutive calls to the same table. What execution time do you get if
you issue the query a second time?
My first guess would be to add an index on my_table.tran_date and check in
EXPLAIN that you don't have a SEQUENTIAL SCAN on that table.
> I have looked at the EXPLAIN, but I don't really know what to look for.
> I can supply it if that would help.
>
> Thanks for any advice.
>
> Frank Millman
>
>
--
Olivier Gautherot
^ permalink raw reply [nested|flat] 21+ messages in thread
* Re: Slow SELECT
2020-05-26 07:22 Slow SELECT Frank Millman <frank@chagford.com>
2020-05-26 07:32 ` Re: Slow SELECT Olivier Gautherot <ogautherot@gautherot.net>
@ 2020-05-26 08:38 ` Frank Millman <frank@chagford.com>
2020-05-26 09:10 ` Re: Slow SELECT Charles Clavadetscher <clavadetscher@swisspug.org>
0 siblings, 1 reply; 21+ messages in thread
From: Frank Millman @ 2020-05-26 08:38 UTC (permalink / raw)
To: Olivier Gautherot <ogautherot@gautherot.net>; +Cc: pgsql-general
On 2020-05-26 9:32 AM, Olivier Gautherot wrote:
> Hi Frank,
>
> On Tue, May 26, 2020 at 9:23 AM Frank Millman <frank@chagford.com
> <mailto:frank@chagford.com>> wrote:
>
> Hi all
>
> I have a SELECT that runs over 5 times slower on PostgreSQL compared
> with Sql Server and sqlite3. I am trying to understand why.
>
> I have a table that looks like this (simplified) -
>
> CREATE TABLE my_table (
> Â Â Â row_id SERIAL PRIMARY KEY,
> Â Â Â deleted_id INT DEFAULT 0,
> Â Â Â fld_1 INT REFERENCES table_1(row_id),
> Â Â Â fld_2 INT REFERENCES table_2(row_id),
> Â Â Â fld_3 INT REFERENCES table_3(row_id),
> Â Â Â fld_4 INT REFERENCES table_4(row_id),
> Â Â Â tran_date DATE,
> Â Â Â tran_total DEC(21,2)
> Â Â Â );
>
> CREATE UNIQUE INDEX my_table_ndx ON my_table (fld_1, fld_2, fld_3,
> fld_4, tran_date) WHERE deleted_id = 0;
>
> The table sizes are -
> Â Â Â my_table : 167 rows
> Â Â Â table_1 : 21 rows
> Â Â Â table_2 : 11 rows
> Â Â Â table_3 : 3 rows
> Â Â Â table_4 : 16 rows
>
> Therefore for each tran_date in my_table there are potentially
> 21x11x3x16 = 11088 rows. Most will be null.
>
> I want to select the row_id for the last tran_date for each of those
> potential groups. This is my select -
>
> Â Â Â SELECT (
> Â Â Â Â Â SELECT a.row_id FROM my_table a
> Â Â Â Â Â WHERE a.fld_1 = b.row_id
> Â Â Â Â Â AND a.fld_2 = c.row_id
> Â Â Â Â Â AND a.fld_3 = d.row_id
> Â Â Â Â Â AND a.fld_4 = e.row_id
> Â Â Â Â Â AND a.deleted_id = 0
> Â Â Â Â Â ORDER BY a.tran_date DESC LIMIT 1
> Â Â Â )
> Â Â Â FROM table_1 b, table_2 c, table_3 d, table_4 e
>
> Out of 11088 rows selected, 103 are not null.
>
> On identical data, this takes 0.06 sec on SQL Server, 0.04 sec on
> sqlite3, and 0.31 sec on PostgreSQL.
>
>
> SQL Server does a good job at caching data in memory. PostgreSQL does
> too on consecutive calls to the same table. What execution time do you
> get if you issue the query a second time?
>
> My first guess would be to add an index on my_table.tran_date and check
> in EXPLAIN that you don't have a SEQUENTIAL SCAN on that table.
>
> I have looked at the EXPLAIN, but I don't really know what to look for.
> I can supply it if that would help.
>
> Thanks for any advice.
>
Thanks Olivier. Unfortunately that did not help.
I was already running the query twice and only timing the second one.
I added the index on tran_date. The timing is the same, and EXPLAIN
shows that it is using a SEQUENTIAL SCAN.
Here is the EXPLAIN -
Nested Loop (cost=0.00..64155.70 rows=11088 width=4)
-> Nested Loop (cost=0.00..10.36 rows=528 width=12)
-> Nested Loop (cost=0.00..2.56 rows=33 width=8)
-> Seq Scan on table_2 c (cost=0.00..1.11 rows=11 width=4)
-> Materialize (cost=0.00..1.04 rows=3 width=4)
-> Seq Scan on table_3 d (cost=0.00..1.03 rows=3
width=4)
-> Materialize (cost=0.00..1.24 rows=16 width=4)
-> Seq Scan on table_4 e (cost=0.00..1.16 rows=16 width=4)
-> Materialize (cost=0.00..1.31 rows=21 width=4)
-> Seq Scan on table_1 b (cost=0.00..1.21 rows=21 width=4)
SubPlan 1
-> Limit (cost=5.77..5.77 rows=1 width=8)
-> Sort (cost=5.77..5.77 rows=1 width=8)
Sort Key: a.tran_date DESC
-> Seq Scan on my_table a (cost=0.00..5.76 rows=1
width=8)
Filter: ((fld_1 = b.row_id) AND (fld_2 =
c.row_id) AND (fld_3 = d.row_id) AND (fld_4 = e.row_id) AND (deleted_id
= 0))
Frank
^ permalink raw reply [nested|flat] 21+ messages in thread
* Re: Slow SELECT
2020-05-26 07:22 Slow SELECT Frank Millman <frank@chagford.com>
2020-05-26 07:32 ` Re: Slow SELECT Olivier Gautherot <ogautherot@gautherot.net>
2020-05-26 08:38 ` Re: Slow SELECT Frank Millman <frank@chagford.com>
@ 2020-05-26 09:10 ` Charles Clavadetscher <clavadetscher@swisspug.org>
2020-05-26 09:27 ` Re: Slow SELECT Charles Clavadetscher <clavadetscher@swisspug.org>
2020-05-26 09:32 ` Re: Slow SELECT Frank Millman <frank@chagford.com>
0 siblings, 2 replies; 21+ messages in thread
From: Charles Clavadetscher @ 2020-05-26 09:10 UTC (permalink / raw)
To: Frank Millman <frank@chagford.com>; +Cc: Olivier Gautherot <ogautherot@gautherot.net>; pgsql-general
Hello
On 2020-05-26 10:38, Frank Millman wrote:
> On 2020-05-26 9:32 AM, Olivier Gautherot wrote:
>> Hi Frank,
>>
>> On Tue, May 26, 2020 at 9:23 AM Frank Millman <frank@chagford.com
>> <mailto:frank@chagford.com>> wrote:
>>
>> Hi all
>>
>> I have a SELECT that runs over 5 times slower on PostgreSQL
>> compared
>> with Sql Server and sqlite3. I am trying to understand why.
>>
>> I have a table that looks like this (simplified) -
>>
>> CREATE TABLE my_table (
>> Â Â Â row_id SERIAL PRIMARY KEY,
>> Â Â Â deleted_id INT DEFAULT 0,
>> Â Â Â fld_1 INT REFERENCES table_1(row_id),
>> Â Â Â fld_2 INT REFERENCES table_2(row_id),
>> Â Â Â fld_3 INT REFERENCES table_3(row_id),
>> Â Â Â fld_4 INT REFERENCES table_4(row_id),
>> Â Â Â tran_date DATE,
>> Â Â Â tran_total DEC(21,2)
>> Â Â Â );
>>
>> CREATE UNIQUE INDEX my_table_ndx ON my_table (fld_1, fld_2, fld_3,
>> fld_4, tran_date) WHERE deleted_id = 0;
>>
>> The table sizes are -
>> Â Â Â my_table : 167 rows
>> Â Â Â table_1 : 21 rows
>> Â Â Â table_2 : 11 rows
>> Â Â Â table_3 : 3 rows
>> Â Â Â table_4 : 16 rows
>>
>> Therefore for each tran_date in my_table there are potentially
>> 21x11x3x16 = 11088 rows. Most will be null.
>>
>> I want to select the row_id for the last tran_date for each of
>> those
>> potential groups. This is my select -
>>
>> Â Â Â SELECT (
>> Â Â Â Â Â SELECT a.row_id FROM my_table a
>> Â Â Â Â Â WHERE a.fld_1 = b.row_id
>> Â Â Â Â Â AND a.fld_2 = c.row_id
>> Â Â Â Â Â AND a.fld_3 = d.row_id
>> Â Â Â Â Â AND a.fld_4 = e.row_id
>> Â Â Â Â Â AND a.deleted_id = 0
>> Â Â Â Â Â ORDER BY a.tran_date DESC LIMIT 1
>> Â Â Â )
>> Â Â Â FROM table_1 b, table_2 c, table_3 d, table_4 e
>>
>> Out of 11088 rows selected, 103 are not null.
>>
>> On identical data, this takes 0.06 sec on SQL Server, 0.04 sec on
>> sqlite3, and 0.31 sec on PostgreSQL.
>>
>>
>> SQL Server does a good job at caching data in memory. PostgreSQL does
>> too on consecutive calls to the same table. What execution time do you
>> get if you issue the query a second time?
>>
>> My first guess would be to add an index on my_table.tran_date and
>> check in EXPLAIN that you don't have a SEQUENTIAL SCAN on that table.
>>
>> I have looked at the EXPLAIN, but I don't really know what to look
>> for.
>> I can supply it if that would help.
>>
>> Thanks for any advice.
>>
>
> Thanks Olivier. Unfortunately that did not help.
>
> I was already running the query twice and only timing the second one.
>
> I added the index on tran_date. The timing is the same, and EXPLAIN
> shows that it is using a SEQUENTIAL SCAN.
>
> Here is the EXPLAIN -
>
>
> Nested Loop (cost=0.00..64155.70 rows=11088 width=4)
> -> Nested Loop (cost=0.00..10.36 rows=528 width=12)
> -> Nested Loop (cost=0.00..2.56 rows=33 width=8)
> -> Seq Scan on table_2 c (cost=0.00..1.11 rows=11
> width=4)
> -> Materialize (cost=0.00..1.04 rows=3 width=4)
> -> Seq Scan on table_3 d (cost=0.00..1.03 rows=3
> width=4)
> -> Materialize (cost=0.00..1.24 rows=16 width=4)
> -> Seq Scan on table_4 e (cost=0.00..1.16 rows=16
> width=4)
> -> Materialize (cost=0.00..1.31 rows=21 width=4)
> -> Seq Scan on table_1 b (cost=0.00..1.21 rows=21 width=4)
> SubPlan 1
> -> Limit (cost=5.77..5.77 rows=1 width=8)
> -> Sort (cost=5.77..5.77 rows=1 width=8)
> Sort Key: a.tran_date DESC
> -> Seq Scan on my_table a (cost=0.00..5.76 rows=1
> width=8)
> Filter: ((fld_1 = b.row_id) AND (fld_2 =
> c.row_id) AND (fld_3 = d.row_id) AND (fld_4 = e.row_id) AND
> (deleted_id = 0))
>
>
> Frank
If I see it correct, the query runs sequential scans on all tables, i.e.
table_1 to table_4.
Do you have an index on the referenced keys (row_id) in table_1 to
table_4?
It happens often that referenced keys are not indexed, leading to poor
execution plans.
Bye
Charles
^ permalink raw reply [nested|flat] 21+ messages in thread
* Re: Slow SELECT
2020-05-26 07:22 Slow SELECT Frank Millman <frank@chagford.com>
2020-05-26 07:32 ` Re: Slow SELECT Olivier Gautherot <ogautherot@gautherot.net>
2020-05-26 08:38 ` Re: Slow SELECT Frank Millman <frank@chagford.com>
2020-05-26 09:10 ` Re: Slow SELECT Charles Clavadetscher <clavadetscher@swisspug.org>
@ 2020-05-26 09:27 ` Charles Clavadetscher <clavadetscher@swisspug.org>
2020-05-26 10:25 ` Re: Slow SELECT Frank Millman <frank@chagford.com>
1 sibling, 1 reply; 21+ messages in thread
From: Charles Clavadetscher @ 2020-05-26 09:27 UTC (permalink / raw)
To: Frank Millman <frank@chagford.com>; +Cc: Olivier Gautherot <ogautherot@gautherot.net>; pgsql-general
On 2020-05-26 11:10, Charles Clavadetscher wrote:
> Hello
>
> On 2020-05-26 10:38, Frank Millman wrote:
>> On 2020-05-26 9:32 AM, Olivier Gautherot wrote:
>>> Hi Frank,
>>>
>>> On Tue, May 26, 2020 at 9:23 AM Frank Millman <frank@chagford.com
>>> <mailto:frank@chagford.com>> wrote:
>>>
>>> Hi all
>>>
>>> I have a SELECT that runs over 5 times slower on PostgreSQL
>>> compared
>>> with Sql Server and sqlite3. I am trying to understand why.
>>>
>>> I have a table that looks like this (simplified) -
>>>
>>> CREATE TABLE my_table (
>>> Â Â Â row_id SERIAL PRIMARY KEY,
>>> Â Â Â deleted_id INT DEFAULT 0,
>>> Â Â Â fld_1 INT REFERENCES table_1(row_id),
>>> Â Â Â fld_2 INT REFERENCES table_2(row_id),
>>> Â Â Â fld_3 INT REFERENCES table_3(row_id),
>>> Â Â Â fld_4 INT REFERENCES table_4(row_id),
>>> Â Â Â tran_date DATE,
>>> Â Â Â tran_total DEC(21,2)
>>> Â Â Â );
>>>
>>> CREATE UNIQUE INDEX my_table_ndx ON my_table (fld_1, fld_2,
>>> fld_3,
>>> fld_4, tran_date) WHERE deleted_id = 0;
>>>
>>> The table sizes are -
>>> Â Â Â my_table : 167 rows
>>> Â Â Â table_1 : 21 rows
>>> Â Â Â table_2 : 11 rows
>>> Â Â Â table_3 : 3 rows
>>> Â Â Â table_4 : 16 rows
>>>
>>> Therefore for each tran_date in my_table there are potentially
>>> 21x11x3x16 = 11088 rows. Most will be null.
>>>
>>> I want to select the row_id for the last tran_date for each of
>>> those
>>> potential groups. This is my select -
>>>
>>> Â Â Â SELECT (
>>> Â Â Â Â Â SELECT a.row_id FROM my_table a
>>> Â Â Â Â Â WHERE a.fld_1 = b.row_id
>>> Â Â Â Â Â AND a.fld_2 = c.row_id
>>> Â Â Â Â Â AND a.fld_3 = d.row_id
>>> Â Â Â Â Â AND a.fld_4 = e.row_id
>>> Â Â Â Â Â AND a.deleted_id = 0
>>> Â Â Â Â Â ORDER BY a.tran_date DESC LIMIT 1
>>> Â Â Â )
>>> Â Â Â FROM table_1 b, table_2 c, table_3 d, table_4 e
>>>
>>> Out of 11088 rows selected, 103 are not null.
>>>
>>> On identical data, this takes 0.06 sec on SQL Server, 0.04 sec on
>>> sqlite3, and 0.31 sec on PostgreSQL.
>>>
>>>
>>> SQL Server does a good job at caching data in memory. PostgreSQL does
>>> too on consecutive calls to the same table. What execution time do
>>> you get if you issue the query a second time?
>>>
>>> My first guess would be to add an index on my_table.tran_date and
>>> check in EXPLAIN that you don't have a SEQUENTIAL SCAN on that table.
>>>
>>> I have looked at the EXPLAIN, but I don't really know what to
>>> look for.
>>> I can supply it if that would help.
>>>
>>> Thanks for any advice.
>>>
>>
>> Thanks Olivier. Unfortunately that did not help.
>>
>> I was already running the query twice and only timing the second one.
>>
>> I added the index on tran_date. The timing is the same, and EXPLAIN
>> shows that it is using a SEQUENTIAL SCAN.
>>
>> Here is the EXPLAIN -
>>
>>
>> Nested Loop (cost=0.00..64155.70 rows=11088 width=4)
>> -> Nested Loop (cost=0.00..10.36 rows=528 width=12)
>> -> Nested Loop (cost=0.00..2.56 rows=33 width=8)
>> -> Seq Scan on table_2 c (cost=0.00..1.11 rows=11
>> width=4)
>> -> Materialize (cost=0.00..1.04 rows=3 width=4)
>> -> Seq Scan on table_3 d (cost=0.00..1.03
>> rows=3 width=4)
>> -> Materialize (cost=0.00..1.24 rows=16 width=4)
>> -> Seq Scan on table_4 e (cost=0.00..1.16 rows=16
>> width=4)
>> -> Materialize (cost=0.00..1.31 rows=21 width=4)
>> -> Seq Scan on table_1 b (cost=0.00..1.21 rows=21 width=4)
>> SubPlan 1
>> -> Limit (cost=5.77..5.77 rows=1 width=8)
>> -> Sort (cost=5.77..5.77 rows=1 width=8)
>> Sort Key: a.tran_date DESC
>> -> Seq Scan on my_table a (cost=0.00..5.76 rows=1
>> width=8)
>> Filter: ((fld_1 = b.row_id) AND (fld_2 =
>> c.row_id) AND (fld_3 = d.row_id) AND (fld_4 = e.row_id) AND
>> (deleted_id = 0))
>>
>>
>> Frank
>
> If I see it correct, the query runs sequential scans on all tables,
> i.e. table_1 to table_4.
> Do you have an index on the referenced keys (row_id) in table_1 to
> table_4?
>
> It happens often that referenced keys are not indexed, leading to poor
> execution plans.
>
> Bye
> Charles
I noticed later that you have very small tables. This will probably lead
to a sequential scan althought there is an index in place.
I am not sure if it makes a difference, but what about using explicit
joins?
SELECT a.row_id FROM my_table a
JOIN b table_1 ON (b.row_id = a.fld_1)
JOIN c table_2 ON (c.row_id = a.fld_2)
JOIN d table_3 ON (d.row_id = a.fld_3)
JOIN e table_4 ON (e.row_id = a.fld_4)
WHERE a.deleted_id = 0
ORDER BY a.tran_date DESC LIMIT 1;
Regards
Charles
^ permalink raw reply [nested|flat] 21+ messages in thread
* Re: Slow SELECT
2020-05-26 07:22 Slow SELECT Frank Millman <frank@chagford.com>
2020-05-26 07:32 ` Re: Slow SELECT Olivier Gautherot <ogautherot@gautherot.net>
2020-05-26 08:38 ` Re: Slow SELECT Frank Millman <frank@chagford.com>
2020-05-26 09:10 ` Re: Slow SELECT Charles Clavadetscher <clavadetscher@swisspug.org>
2020-05-26 09:27 ` Re: Slow SELECT Charles Clavadetscher <clavadetscher@swisspug.org>
@ 2020-05-26 10:25 ` Frank Millman <frank@chagford.com>
0 siblings, 0 replies; 21+ messages in thread
From: Frank Millman @ 2020-05-26 10:25 UTC (permalink / raw)
To: pgsql-general
On 2020-05-26 11:27 AM, Charles Clavadetscher wrote:
> On 2020-05-26 11:10, Charles Clavadetscher wrote:
>> Hello
>>
>> On 2020-05-26 10:38, Frank Millman wrote:
>>> On 2020-05-26 9:32 AM, Olivier Gautherot wrote:
>>>> Hi Frank,
>>>>
>>>> On Tue, May 26, 2020 at 9:23 AM Frank Millman <frank@chagford.com
>>>> <mailto:frank@chagford.com>> wrote:
>>>>
>>>> Â Â Â Hi all
>>>>
>>>> Â Â Â I have a SELECT that runs over 5 times slower on PostgreSQL
>>>> compared
>>>> Â Â Â with Sql Server and sqlite3. I am trying to understand why.
>>>>
>>>> Â Â Â I have a table that looks like this (simplified) -
>>>>
>>>> Â Â Â CREATE TABLE my_table (
>>>> Â Â Â Â Â Â Â row_id SERIAL PRIMARY KEY,
>>>> Â Â Â Â Â Â Â deleted_id INT DEFAULT 0,
>>>> Â Â Â Â Â Â Â fld_1 INT REFERENCES table_1(row_id),
>>>> Â Â Â Â Â Â Â fld_2 INT REFERENCES table_2(row_id),
>>>> Â Â Â Â Â Â Â fld_3 INT REFERENCES table_3(row_id),
>>>> Â Â Â Â Â Â Â fld_4 INT REFERENCES table_4(row_id),
>>>> Â Â Â Â Â Â Â tran_date DATE,
>>>> Â Â Â Â Â Â Â tran_total DEC(21,2)
>>>> Â Â Â Â Â Â Â );
>>>>
>>>> Â Â Â CREATE UNIQUE INDEX my_table_ndx ON my_table (fld_1, fld_2, fld_3,
>>>> Â Â Â fld_4, tran_date) WHERE deleted_id = 0;
>>>>
>>>> Â Â Â The table sizes are -
>>>> Â Â Â Â Â Â Â my_table : 167 rows
>>>> Â Â Â Â Â Â Â table_1 : 21 rows
>>>> Â Â Â Â Â Â Â table_2 : 11 rows
>>>> Â Â Â Â Â Â Â table_3 : 3 rows
>>>> Â Â Â Â Â Â Â table_4 : 16 rows
>>>>
>>>> Â Â Â Therefore for each tran_date in my_table there are potentially
>>>> Â Â Â 21x11x3x16 = 11088 rows. Most will be null.
>>>>
>>>> Â Â Â I want to select the row_id for the last tran_date for each of
>>>> those
>>>> Â Â Â potential groups. This is my select -
>>>>
>>>> Â Â Â Â Â Â Â SELECT (
>>>> Â Â Â Â Â Â Â Â Â SELECT a.row_id FROM my_table a
>>>> Â Â Â Â Â Â Â Â Â WHERE a.fld_1 = b.row_id
>>>> Â Â Â Â Â Â Â Â Â AND a.fld_2 = c.row_id
>>>> Â Â Â Â Â Â Â Â Â AND a.fld_3 = d.row_id
>>>> Â Â Â Â Â Â Â Â Â AND a.fld_4 = e.row_id
>>>> Â Â Â Â Â Â Â Â Â AND a.deleted_id = 0
>>>> Â Â Â Â Â Â Â Â Â ORDER BY a.tran_date DESC LIMIT 1
>>>> Â Â Â Â Â Â Â )
>>>> Â Â Â Â Â Â Â FROM table_1 b, table_2 c, table_3 d, table_4 e
>>>>
>>>> Â Â Â Out of 11088 rows selected, 103 are not null.
>>>>
>>>> Â Â Â On identical data, this takes 0.06 sec on SQL Server, 0.04 sec on
>>>> Â Â Â sqlite3, and 0.31 sec on PostgreSQL.
>>>>
>>>>
>>>> SQL Server does a good job at caching data in memory. PostgreSQL
>>>> does too on consecutive calls to the same table. What execution time
>>>> do you get if you issue the query a second time?
>>>>
>>>> My first guess would be to add an index on my_table.tran_date and
>>>> check in EXPLAIN that you don't have a SEQUENTIAL SCAN on that table.
>>>>
>>>> Â Â Â I have looked at the EXPLAIN, but I don't really know what to
>>>> look for.
>>>> Â Â Â I can supply it if that would help.
>>>>
>>>> Â Â Â Thanks for any advice.
>>>>
>>>
>>> Thanks Olivier. Unfortunately that did not help.
>>>
>>> I was already running the query twice and only timing the second one.
>>>
>>> I added the index on tran_date. The timing is the same, and EXPLAIN
>>> shows that it is using a SEQUENTIAL SCAN.
>>>
>>> Here is the EXPLAIN -
>>>
>>>
>>>  Nested Loop (cost=0.00..64155.70 rows=11088 width=4)
>>>   -> Nested Loop (cost=0.00..10.36 rows=528 width=12)
>>>         -> Nested Loop (cost=0.00..2.56 rows=33 width=8)
>>>               -> Seq Scan on table_2 c (cost=0.00..1.11 rows=11
>>> width=4)
>>>               -> Materialize (cost=0.00..1.04 rows=3 width=4)
>>>                     -> Seq Scan on table_3 d (cost=0.00..1.03
>>> rows=3 width=4)
>>>         -> Materialize (cost=0.00..1.24 rows=16 width=4)
>>>               -> Seq Scan on table_4 e (cost=0.00..1.16 rows=16
>>> width=4)
>>>   -> Materialize (cost=0.00..1.31 rows=21 width=4)
>>>         -> Seq Scan on table_1 b (cost=0.00..1.21 rows=21 width=4)
>>> Â Â SubPlan 1
>>>     -> Limit (cost=5.77..5.77 rows=1 width=8)
>>>           -> Sort (cost=5.77..5.77 rows=1 width=8)
>>> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Sort Key: a.tran_date DESC
>>>                 -> Seq Scan on my_table a (cost=0.00..5.76 rows=1
>>> width=8)
>>> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Filter: ((fld_1 = b.row_id) AND (fld_2 =
>>> c.row_id) AND (fld_3 = d.row_id) AND (fld_4 = e.row_id) AND
>>> (deleted_id = 0))
>>>
>>>
>>> Frank
>>
>> If I see it correct, the query runs sequential scans on all tables,
>> i.e. table_1 to table_4.
>> Do you have an index on the referenced keys (row_id) in table_1 to
>> table_4?
>>
>> It happens often that referenced keys are not indexed, leading to poor
>> execution plans.
>>
>> Bye
>> Charles
>
> I noticed later that you have very small tables. This will probably lead
> to a sequential scan althought there is an index in place.
>
> I am not sure if it makes a difference, but what about using explicit
> joins?
>
> SELECT a.row_id FROM my_table a
> JOIN b table_1 ON (b.row_id = a.fld_1)
> JOIN c table_2 ON (c.row_id = a.fld_2)
> JOIN d table_3 ON (d.row_id = a.fld_3)
> JOIN e table_4 ON (e.row_id = a.fld_4)
> WHERE a.deleted_id = 0
> ORDER BY a.tran_date DESC LIMIT 1;
>
Thanks, Charles. I tried that, but unfortunately it produces a different
result. I need to test for every possible combination of fld1-4, and get
the highest date for each one. Using joins only tests existing
combinations, and gets the highest date for all of them combined.
Seel my reply to David Rowley. I do not fully understand his solution
yet, but it seems to be what I am looking for.
Thanks again
Frank
^ permalink raw reply [nested|flat] 21+ messages in thread
* Re: Slow SELECT
2020-05-26 07:22 Slow SELECT Frank Millman <frank@chagford.com>
2020-05-26 07:32 ` Re: Slow SELECT Olivier Gautherot <ogautherot@gautherot.net>
2020-05-26 08:38 ` Re: Slow SELECT Frank Millman <frank@chagford.com>
2020-05-26 09:10 ` Re: Slow SELECT Charles Clavadetscher <clavadetscher@swisspug.org>
@ 2020-05-26 09:32 ` Frank Millman <frank@chagford.com>
1 sibling, 0 replies; 21+ messages in thread
From: Frank Millman @ 2020-05-26 09:32 UTC (permalink / raw)
To: Charles Clavadetscher <clavadetscher@swisspug.org>; +Cc: pgsql-general
On 2020-05-26 11:10 AM, Charles Clavadetscher wrote:
> Hello
>
> On 2020-05-26 10:38, Frank Millman wrote:
>> On 2020-05-26 9:32 AM, Olivier Gautherot wrote:
>>> Hi Frank,
>>>
>>> On Tue, May 26, 2020 at 9:23 AM Frank Millman <frank@chagford.com
>>> <mailto:frank@chagford.com>> wrote:
>>>
>>> Â Â Â Hi all
>>>
>>> Â Â Â I have a SELECT that runs over 5 times slower on PostgreSQL compared
>>> Â Â Â with Sql Server and sqlite3. I am trying to understand why.
>>>
>>> Â Â Â I have a table that looks like this (simplified) -
>>>
>>> Â Â Â CREATE TABLE my_table (
>>> Â Â Â Â Â Â Â row_id SERIAL PRIMARY KEY,
>>> Â Â Â Â Â Â Â deleted_id INT DEFAULT 0,
>>> Â Â Â Â Â Â Â fld_1 INT REFERENCES table_1(row_id),
>>> Â Â Â Â Â Â Â fld_2 INT REFERENCES table_2(row_id),
>>> Â Â Â Â Â Â Â fld_3 INT REFERENCES table_3(row_id),
>>> Â Â Â Â Â Â Â fld_4 INT REFERENCES table_4(row_id),
>>> Â Â Â Â Â Â Â tran_date DATE,
>>> Â Â Â Â Â Â Â tran_total DEC(21,2)
>>> Â Â Â Â Â Â Â );
>>>
>>> Â Â Â CREATE UNIQUE INDEX my_table_ndx ON my_table (fld_1, fld_2, fld_3,
>>> Â Â Â fld_4, tran_date) WHERE deleted_id = 0;
>>>
>>> Â Â Â The table sizes are -
>>> Â Â Â Â Â Â Â my_table : 167 rows
>>> Â Â Â Â Â Â Â table_1 : 21 rows
>>> Â Â Â Â Â Â Â table_2 : 11 rows
>>> Â Â Â Â Â Â Â table_3 : 3 rows
>>> Â Â Â Â Â Â Â table_4 : 16 rows
>>>
>>> Â Â Â Therefore for each tran_date in my_table there are potentially
>>> Â Â Â 21x11x3x16 = 11088 rows. Most will be null.
>>>
>>> Â Â Â I want to select the row_id for the last tran_date for each of those
>>> Â Â Â potential groups. This is my select -
>>>
>>> Â Â Â Â Â Â Â SELECT (
>>> Â Â Â Â Â Â Â Â Â SELECT a.row_id FROM my_table a
>>> Â Â Â Â Â Â Â Â Â WHERE a.fld_1 = b.row_id
>>> Â Â Â Â Â Â Â Â Â AND a.fld_2 = c.row_id
>>> Â Â Â Â Â Â Â Â Â AND a.fld_3 = d.row_id
>>> Â Â Â Â Â Â Â Â Â AND a.fld_4 = e.row_id
>>> Â Â Â Â Â Â Â Â Â AND a.deleted_id = 0
>>> Â Â Â Â Â Â Â Â Â ORDER BY a.tran_date DESC LIMIT 1
>>> Â Â Â Â Â Â Â )
>>> Â Â Â Â Â Â Â FROM table_1 b, table_2 c, table_3 d, table_4 e
>>>
>>> Â Â Â Out of 11088 rows selected, 103 are not null.
>>>
>>> Â Â Â On identical data, this takes 0.06 sec on SQL Server, 0.04 sec on
>>> Â Â Â sqlite3, and 0.31 sec on PostgreSQL.
>>>
>>>
>>> SQL Server does a good job at caching data in memory. PostgreSQL does
>>> too on consecutive calls to the same table. What execution time do
>>> you get if you issue the query a second time?
>>>
>>> My first guess would be to add an index on my_table.tran_date and
>>> check in EXPLAIN that you don't have a SEQUENTIAL SCAN on that table.
>>>
>>> Â Â Â I have looked at the EXPLAIN, but I don't really know what to
>>> look for.
>>> Â Â Â I can supply it if that would help.
>>>
>>> Â Â Â Thanks for any advice.
>>>
>>
>> Thanks Olivier. Unfortunately that did not help.
>>
>> I was already running the query twice and only timing the second one.
>>
>> I added the index on tran_date. The timing is the same, and EXPLAIN
>> shows that it is using a SEQUENTIAL SCAN.
>>
>> Here is the EXPLAIN -
>>
>>
>>  Nested Loop (cost=0.00..64155.70 rows=11088 width=4)
>>   -> Nested Loop (cost=0.00..10.36 rows=528 width=12)
>>         -> Nested Loop (cost=0.00..2.56 rows=33 width=8)
>>               -> Seq Scan on table_2 c (cost=0.00..1.11 rows=11
>> width=4)
>>               -> Materialize (cost=0.00..1.04 rows=3 width=4)
>>                     -> Seq Scan on table_3 d (cost=0.00..1.03
>> rows=3 width=4)
>>         -> Materialize (cost=0.00..1.24 rows=16 width=4)
>>               -> Seq Scan on table_4 e (cost=0.00..1.16 rows=16
>> width=4)
>>   -> Materialize (cost=0.00..1.31 rows=21 width=4)
>>         -> Seq Scan on table_1 b (cost=0.00..1.21 rows=21 width=4)
>> Â Â SubPlan 1
>>     -> Limit (cost=5.77..5.77 rows=1 width=8)
>>           -> Sort (cost=5.77..5.77 rows=1 width=8)
>> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Sort Key: a.tran_date DESC
>>                 -> Seq Scan on my_table a (cost=0.00..5.76 rows=1
>> width=8)
>> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Filter: ((fld_1 = b.row_id) AND (fld_2 =
>> c.row_id) AND (fld_3 = d.row_id) AND (fld_4 = e.row_id) AND
>> (deleted_id = 0))
>>
>>
>> Frank
>
> If I see it correct, the query runs sequential scans on all tables, i.e.
> table_1 to table_4.
> Do you have an index on the referenced keys (row_id) in table_1 to table_4?
>
> It happens often that referenced keys are not indexed, leading to poor
> execution plans.
>
The referenced keys are all defined as SERIAL PRIMARY KEY in their own
tables, so I presume that that are all indexed automatically.
On the other hand, there are not many rows in those tables, so the
planner may decide not to use the index in that case.
Frank
^ permalink raw reply [nested|flat] 21+ messages in thread
* Re: Slow SELECT
2020-05-26 07:22 Slow SELECT Frank Millman <frank@chagford.com>
@ 2020-05-26 10:02 ` Christian Ramseyer <rc@networkz.ch>
2020-05-26 10:26 ` Re: Slow SELECT Frank Millman <frank@chagford.com>
2 siblings, 1 reply; 21+ messages in thread
From: Christian Ramseyer @ 2020-05-26 10:02 UTC (permalink / raw)
To: Frank Millman <frank@chagford.com>; pgsql-general
Hi
On 26.05.20 09:22, Frank Millman wrote:
>
> I have looked at the EXPLAIN, but I don't really know what to look for.
> I can supply it if that would help.
>
My favorite approach to tuning Postgres queries is:
1. Run EXPLAIN ANALYZE <query>
2. Copy/Paste the output into the fantastic https://explain.depesz.com/
This will turn the somewhat hard-to-understand explain output into a
nice colored structure. If it's not obvious from the orange-reddish
boxes where the slowness comes from, please post the link here and
somebody will certainly have some advice.
Cheers
Christian
--
Christian Ramseyer, netnea ag
Network Management. Security. OpenSource.
https://www.netnea.com
^ permalink raw reply [nested|flat] 21+ messages in thread
* Re: Slow SELECT
2020-05-26 07:22 Slow SELECT Frank Millman <frank@chagford.com>
2020-05-26 10:02 ` Re: Slow SELECT Christian Ramseyer <rc@networkz.ch>
@ 2020-05-26 10:26 ` Frank Millman <frank@chagford.com>
0 siblings, 0 replies; 21+ messages in thread
From: Frank Millman @ 2020-05-26 10:26 UTC (permalink / raw)
To: pgsql-general
On 2020-05-26 12:02 PM, Christian Ramseyer wrote:
> Hi
>
> On 26.05.20 09:22, Frank Millman wrote:
>>
>> I have looked at the EXPLAIN, but I don't really know what to look for.
>> I can supply it if that would help.
>>
>
> My favorite approach to tuning Postgres queries is:
>
> 1. Run EXPLAIN ANALYZE <query>
> 2. Copy/Paste the output into the fantastic https://explain.depesz.com/
>
> This will turn the somewhat hard-to-understand explain output into a
> nice colored structure. If it's not obvious from the orange-reddish
> boxes where the slowness comes from, please post the link here and
> somebody will certainly have some advice.
>
Thanks, Christian. I will definitely look into that.
Frank
^ permalink raw reply [nested|flat] 21+ messages in thread
* Re: Slow SELECT
2020-05-26 07:22 Slow SELECT Frank Millman <frank@chagford.com>
@ 2020-05-26 10:04 ` David Rowley <dgrowleyml@gmail.com>
2020-05-26 10:30 ` Re: Slow SELECT Frank Millman <frank@chagford.com>
2020-05-26 11:41 ` Re: Slow SELECT Vik Fearing <vik@postgresfriends.org>
2020-05-29 12:14 ` Re: Slow SELECT Frank Millman <frank@chagford.com>
2 siblings, 3 replies; 21+ messages in thread
From: David Rowley @ 2020-05-26 10:04 UTC (permalink / raw)
To: Frank Millman <frank@chagford.com>; +Cc: pgsql-general
On Tue, 26 May 2020 at 19:23, Frank Millman <frank@chagford.com> wrote:
> The table sizes are -
> my_table : 167 rows
> table_1 : 21 rows
> table_2 : 11 rows
> table_3 : 3 rows
> table_4 : 16 rows
>
> Therefore for each tran_date in my_table there are potentially
> 21x11x3x16 = 11088 rows. Most will be null.
>
> I want to select the row_id for the last tran_date for each of those
> potential groups. This is my select -
>
> SELECT (
> SELECT a.row_id FROM my_table a
> WHERE a.fld_1 = b.row_id
> AND a.fld_2 = c.row_id
> AND a.fld_3 = d.row_id
> AND a.fld_4 = e.row_id
> AND a.deleted_id = 0
> ORDER BY a.tran_date DESC LIMIT 1
> )
> FROM table_1 b, table_2 c, table_3 d, table_4 e
>
> Out of 11088 rows selected, 103 are not null.
Perhaps SQL Server is doing something to rewrite the subquery in the
target list to a LEFT JOIN. PostgreSQL currently does not do that.
Since "my_table" is small, you'd likely be much better doing a manual
rewrite of the query to join a subquery containing the required
details from "my_table". It looks like you want the row_id from the
latest tran_date for each fld_N column. So something like:
SELECT a.row_id
FROM table_1 b
CROSS JOIN table_2 c
CROSS JOIN table_3 d
CROSS JOIN table_4 e
LEFT OUTER JOIN (
SELECT fld_1,fld_2,fld_3,fld_4,row_id,tran_date,
ROW_NUMBER() OVER (PARTITION BY fld_1,fld_2,fld_3,fld_4 ORDER BY
tran_date DESC) row_num
FROM my_table
WHERE deleted_id = 0
) a ON a.fld_1 = b.row_id AND a.fld_2 = c.row_id AND a.fld_3 =
d.row_id AND a.fld_4 = e.row_id AND a.row_num = 1;
Should do it. You could also perhaps do something with DISTINCT ON
instead of using ROW_NUMBER(). That might be a bit more efficient, but
it's unlikely to matter too much since there are only 167 rows in that
table.
David
^ permalink raw reply [nested|flat] 21+ messages in thread
* Re: Slow SELECT
2020-05-26 07:22 Slow SELECT Frank Millman <frank@chagford.com>
2020-05-26 10:04 ` Re: Slow SELECT David Rowley <dgrowleyml@gmail.com>
@ 2020-05-26 10:30 ` Frank Millman <frank@chagford.com>
2020-05-26 11:45 ` Re: Slow SELECT David Rowley <dgrowleyml@gmail.com>
2 siblings, 1 reply; 21+ messages in thread
From: Frank Millman @ 2020-05-26 10:30 UTC (permalink / raw)
To: pgsql-general
On 2020-05-26 12:04 PM, David Rowley wrote:
> On Tue, 26 May 2020 at 19:23, Frank Millman <frank@chagford.com> wrote:
>> The table sizes are -
>> my_table : 167 rows
>> table_1 : 21 rows
>> table_2 : 11 rows
>> table_3 : 3 rows
>> table_4 : 16 rows
>>
>> Therefore for each tran_date in my_table there are potentially
>> 21x11x3x16 = 11088 rows. Most will be null.
>>
>> I want to select the row_id for the last tran_date for each of those
>> potential groups. This is my select -
>>
>> SELECT (
>> SELECT a.row_id FROM my_table a
>> WHERE a.fld_1 = b.row_id
>> AND a.fld_2 = c.row_id
>> AND a.fld_3 = d.row_id
>> AND a.fld_4 = e.row_id
>> AND a.deleted_id = 0
>> ORDER BY a.tran_date DESC LIMIT 1
>> )
>> FROM table_1 b, table_2 c, table_3 d, table_4 e
>>
>> Out of 11088 rows selected, 103 are not null.
>
> Perhaps SQL Server is doing something to rewrite the subquery in the
> target list to a LEFT JOIN. PostgreSQL currently does not do that.
>
> Since "my_table" is small, you'd likely be much better doing a manual
> rewrite of the query to join a subquery containing the required
> details from "my_table". It looks like you want the row_id from the
> latest tran_date for each fld_N column. So something like:
>
> SELECT a.row_id
> FROM table_1 b
> CROSS JOIN table_2 c
> CROSS JOIN table_3 d
> CROSS JOIN table_4 e
> LEFT OUTER JOIN (
> SELECT fld_1,fld_2,fld_3,fld_4,row_id,tran_date,
> ROW_NUMBER() OVER (PARTITION BY fld_1,fld_2,fld_3,fld_4 ORDER BY
> tran_date DESC) row_num
> FROM my_table
> WHERE deleted_id = 0
> ) a ON a.fld_1 = b.row_id AND a.fld_2 = c.row_id AND a.fld_3 =
> d.row_id AND a.fld_4 = e.row_id AND a.row_num = 1;
>
> Should do it. You could also perhaps do something with DISTINCT ON
> instead of using ROW_NUMBER(). That might be a bit more efficient, but
> it's unlikely to matter too much since there are only 167 rows in that
> table.
>
Thank you David. I tried that and it produced the correct result in
53ms, which is what I am looking for.
It will take me some time to understand it fully, so I have some
homework to do!
Much appreciated.
Frank
^ permalink raw reply [nested|flat] 21+ messages in thread
* Re: Slow SELECT
2020-05-26 07:22 Slow SELECT Frank Millman <frank@chagford.com>
2020-05-26 10:04 ` Re: Slow SELECT David Rowley <dgrowleyml@gmail.com>
2020-05-26 10:30 ` Re: Slow SELECT Frank Millman <frank@chagford.com>
@ 2020-05-26 11:45 ` David Rowley <dgrowleyml@gmail.com>
2020-05-26 12:07 ` Re: Slow SELECT Frank Millman <frank@chagford.com>
0 siblings, 1 reply; 21+ messages in thread
From: David Rowley @ 2020-05-26 11:45 UTC (permalink / raw)
To: Frank Millman <frank@chagford.com>; +Cc: pgsql-general
On Tue, 26 May 2020 at 22:31, Frank Millman <frank@chagford.com> wrote:
> Thank you David. I tried that and it produced the correct result in
> 53ms, which is what I am looking for.
>
> It will take me some time to understand it fully, so I have some
> homework to do!
The main problem with your previous query was that the subquery was
being executed 11088 times and could only ever find anything 167
times. The remaining number of times nothing would be found.
I just changed the subquery which would be executed once per output
row and altered it so it became a subquery that's joined and only
executed once. The ROW_NUMBER() is a windowing function, which is
explained in [1]. I used this to get the row_id of the record with
the lowest tran_date, just like you were doing with the ORDER BY
tran_date DESC LIMIT 1, but the subquery with the windowing function
gets them all at once, rather than doing it in a way that requires it
to be executed once for each row in the top-level query. In this case,
the functionality that the LIMIT 1 does in your query is achieved with
"AND a.row_num = 1;" in my version. This is pretty fast to execute
once due to there only being 167 rows.
It's also important to know that there may be cases where the method I
proposed is slower. For example, if my_table was very large and
contained rows that were not in table_1 to table_4. Since the subquery
in my version calculates everything then it could be wasteful to do
that for values that would never be used. For you, you have foreign
keys that ensure my_table does not contain records that are not in the
other tables, but you could still see this issue if you were to add
some restrictive WHERE clause to the outer query. Perhaps this won't
be a problem for you, but it's likely good to know.
[1] https://www.postgresql.org/docs/current/tutorial-window.html
David
^ permalink raw reply [nested|flat] 21+ messages in thread
* Re: Slow SELECT
2020-05-26 07:22 Slow SELECT Frank Millman <frank@chagford.com>
2020-05-26 10:04 ` Re: Slow SELECT David Rowley <dgrowleyml@gmail.com>
2020-05-26 10:30 ` Re: Slow SELECT Frank Millman <frank@chagford.com>
2020-05-26 11:45 ` Re: Slow SELECT David Rowley <dgrowleyml@gmail.com>
@ 2020-05-26 12:07 ` Frank Millman <frank@chagford.com>
0 siblings, 0 replies; 21+ messages in thread
From: Frank Millman @ 2020-05-26 12:07 UTC (permalink / raw)
To: pgsql-general
On 2020-05-26 1:45 PM, David Rowley wrote:
> On Tue, 26 May 2020 at 22:31, Frank Millman <frank@chagford.com> wrote:
>
>> Thank you David. I tried that and it produced the correct result in
>> 53ms, which is what I am looking for.
>>
>> It will take me some time to understand it fully, so I have some
>> homework to do!
>
> The main problem with your previous query was that the subquery was
> being executed 11088 times and could only ever find anything 167
> times. The remaining number of times nothing would be found.
>
> I just changed the subquery which would be executed once per output
> row and altered it so it became a subquery that's joined and only
> executed once. The ROW_NUMBER() is a windowing function, which is
> explained in [1]. I used this to get the row_id of the record with
> the lowest tran_date, just like you were doing with the ORDER BY
> tran_date DESC LIMIT 1, but the subquery with the windowing function
> gets them all at once, rather than doing it in a way that requires it
> to be executed once for each row in the top-level query. In this case,
> the functionality that the LIMIT 1 does in your query is achieved with
> "AND a.row_num = 1;" in my version. This is pretty fast to execute
> once due to there only being 167 rows.
>
> It's also important to know that there may be cases where the method I
> proposed is slower. For example, if my_table was very large and
> contained rows that were not in table_1 to table_4. Since the subquery
> in my version calculates everything then it could be wasteful to do
> that for values that would never be used. For you, you have foreign
> keys that ensure my_table does not contain records that are not in the
> other tables, but you could still see this issue if you were to add
> some restrictive WHERE clause to the outer query. Perhaps this won't
> be a problem for you, but it's likely good to know.
>
> [1] https://www.postgresql.org/docs/current/tutorial-window.html
>
Thanks very much for the explanation. I will go through it carefully.
For the record, your query works without modification in both Sql Server
and sqlite3. It is also much faster in all three cases - all around
0.005 sec instead of 0.05 sec.
Frank
^ permalink raw reply [nested|flat] 21+ messages in thread
* Re: Slow SELECT
2020-05-26 07:22 Slow SELECT Frank Millman <frank@chagford.com>
2020-05-26 10:04 ` Re: Slow SELECT David Rowley <dgrowleyml@gmail.com>
@ 2020-05-26 11:41 ` Vik Fearing <vik@postgresfriends.org>
2020-05-26 11:49 ` Re: Slow SELECT David Rowley <dgrowleyml@gmail.com>
2 siblings, 1 reply; 21+ messages in thread
From: Vik Fearing @ 2020-05-26 11:41 UTC (permalink / raw)
To: David Rowley <dgrowleyml@gmail.com>; Frank Millman <frank@chagford.com>; +Cc: pgsql-general
On 5/26/20 12:04 PM, David Rowley wrote:
> Since "my_table" is small, you'd likely be much better doing a manual
> rewrite of the query to join a subquery containing the required
> details from "my_table". It looks like you want the row_id from the
> latest tran_date for each fld_N column. So something like:
>
> SELECT a.row_id
> FROM table_1 b
> CROSS JOIN table_2 c
> CROSS JOIN table_3 d
> CROSS JOIN table_4 e
> LEFT OUTER JOIN (
> SELECT fld_1,fld_2,fld_3,fld_4,row_id,tran_date,
> ROW_NUMBER() OVER (PARTITION BY fld_1,fld_2,fld_3,fld_4 ORDER BY
> tran_date DESC) row_num
> FROM my_table
> WHERE deleted_id = 0
> ) a ON a.fld_1 = b.row_id AND a.fld_2 = c.row_id AND a.fld_3 =
> d.row_id AND a.fld_4 = e.row_id AND a.row_num = 1;
>
> Should do it. You could also perhaps do something with DISTINCT ON
> instead of using ROW_NUMBER(). That might be a bit more efficient, but
> it's unlikely to matter too much since there are only 167 rows in that
> table.
I would expect a lateral query to be better here.
SELECT a.*
FROM table_1 AS b
CROSS JOIN table_2 AS c
CROSS JOIN table_3 AS d
CROSS JOIN table_4 AS e
CROSS JOIN LATERAL (
SELECT *
FROM my_table AS a
WHERE (a.fld_1, a.fld_2, a.fld_3, a.fld_4) = (b.row_id, c.row_id,
d.row_id, e.row_id)
AND a.deleted = 0
ORDER BY a.tran_date DESC
FETCH FIRST ROW ONLY
) AS a
WHERE a.row_id IS NOT NULL;
You will likely want an index on my_table (fld_1, fld_2, fld_3, fld_4,
tran_date) if your dataset gets bigger.
This query is 100% Standard SQL, so it *should* work on other engines.
That doesn't mean it will, though.
--
Vik Fearing
^ permalink raw reply [nested|flat] 21+ messages in thread
* Re: Slow SELECT
2020-05-26 07:22 Slow SELECT Frank Millman <frank@chagford.com>
2020-05-26 10:04 ` Re: Slow SELECT David Rowley <dgrowleyml@gmail.com>
2020-05-26 11:41 ` Re: Slow SELECT Vik Fearing <vik@postgresfriends.org>
@ 2020-05-26 11:49 ` David Rowley <dgrowleyml@gmail.com>
0 siblings, 0 replies; 21+ messages in thread
From: David Rowley @ 2020-05-26 11:49 UTC (permalink / raw)
To: Vik Fearing <vik@postgresfriends.org>; +Cc: Frank Millman <frank@chagford.com>; pgsql-general
On Tue, 26 May 2020 at 23:41, Vik Fearing <vik@postgresfriends.org> wrote:
>
> On 5/26/20 12:04 PM, David Rowley wrote:
> > Since "my_table" is small, you'd likely be much better doing a manual
> > rewrite of the query to join a subquery containing the required
> > details from "my_table". It looks like you want the row_id from the
> > latest tran_date for each fld_N column. So something like:
> >
> > SELECT a.row_id
> > FROM table_1 b
> > CROSS JOIN table_2 c
> > CROSS JOIN table_3 d
> > CROSS JOIN table_4 e
> > LEFT OUTER JOIN (
> > SELECT fld_1,fld_2,fld_3,fld_4,row_id,tran_date,
> > ROW_NUMBER() OVER (PARTITION BY fld_1,fld_2,fld_3,fld_4 ORDER BY
> > tran_date DESC) row_num
> > FROM my_table
> > WHERE deleted_id = 0
> > ) a ON a.fld_1 = b.row_id AND a.fld_2 = c.row_id AND a.fld_3 =
> > d.row_id AND a.fld_4 = e.row_id AND a.row_num = 1;
> >
> > Should do it. You could also perhaps do something with DISTINCT ON
> > instead of using ROW_NUMBER(). That might be a bit more efficient, but
> > it's unlikely to matter too much since there are only 167 rows in that
> > table.
>
> I would expect a lateral query to be better here.
But that would put it back to executing the subquery 11088 times. I
wrote it in a way to avoid that.
David
^ permalink raw reply [nested|flat] 21+ messages in thread
* Re: Slow SELECT
2020-05-26 07:22 Slow SELECT Frank Millman <frank@chagford.com>
2020-05-26 10:04 ` Re: Slow SELECT David Rowley <dgrowleyml@gmail.com>
@ 2020-05-29 12:14 ` Frank Millman <frank@chagford.com>
2 siblings, 0 replies; 21+ messages in thread
From: Frank Millman @ 2020-05-29 12:14 UTC (permalink / raw)
To: pgsql-general
On 2020-05-26 12:04 PM, David Rowley wrote:
>
> Since "my_table" is small, you'd likely be much better doing a manual
> rewrite of the query to join a subquery containing the required
> details from "my_table". It looks like you want the row_id from the
> latest tran_date for each fld_N column. So something like:
>
> SELECT a.row_id
> FROM table_1 b
> CROSS JOIN table_2 c
> CROSS JOIN table_3 d
> CROSS JOIN table_4 e
> LEFT OUTER JOIN (
> SELECT fld_1,fld_2,fld_3,fld_4,row_id,tran_date,
> ROW_NUMBER() OVER (PARTITION BY fld_1,fld_2,fld_3,fld_4 ORDER BY
> tran_date DESC) row_num
> FROM my_table
> WHERE deleted_id = 0
> ) a ON a.fld_1 = b.row_id AND a.fld_2 = c.row_id AND a.fld_3 =
> d.row_id AND a.fld_4 = e.row_id AND a.row_num = 1;
>
> Should do it. You could also perhaps do something with DISTINCT ON
> instead of using ROW_NUMBER(). That might be a bit more efficient, but
> it's unlikely to matter too much since there are only 167 rows in that
> table.
>
I have studied the above SELECT, and I now more or less understand it. I
would not have come up with that unaided, so many thanks.
I tried DISTINCT ON, and it was very efficient, but unfortunately that
is not supported by SQL Server or sqlite3.
Then I came up with this alternative, which works on all three platforms
and seems a bit faster than the above -
SELECT a.row_id FROM (
SELECT row_id,
ROW_NUMBER() OVER (PARTITION BY fld_1, fld_2, fld_3, fld_4
ORDER BY tran_date DESC) row_num
FROM my_table
WHERE deleted_id = 0
) as a
WHERE a.row_num = 1
Do you see any problem with this?
Thanks
Frank
^ permalink raw reply [nested|flat] 21+ messages in thread
end of thread, other threads:[~2020-05-29 12:14 UTC | newest]
Thread overview: 21+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2003-10-02 10:03 Slow SELECT psql-mail@freeuk.com
2003-10-03 16:50 ` Tom Lane <tgl@sss.pgh.pa.us>
2003-10-08 19:25 ` Mat <psql-mail@freeuk.com>
2003-10-09 07:36 ` Shridhar Daithankar <shridhar_daithankar@persistent.co.in>
2003-10-13 21:54 ` Tom Lane <tgl@sss.pgh.pa.us>
2020-05-26 07:22 Slow SELECT Frank Millman <frank@chagford.com>
2020-05-26 07:32 ` Olivier Gautherot <ogautherot@gautherot.net>
2020-05-26 08:38 ` Frank Millman <frank@chagford.com>
2020-05-26 09:10 ` Charles Clavadetscher <clavadetscher@swisspug.org>
2020-05-26 09:27 ` Charles Clavadetscher <clavadetscher@swisspug.org>
2020-05-26 10:25 ` Frank Millman <frank@chagford.com>
2020-05-26 09:32 ` Frank Millman <frank@chagford.com>
2020-05-26 10:02 ` Christian Ramseyer <rc@networkz.ch>
2020-05-26 10:26 ` Frank Millman <frank@chagford.com>
2020-05-26 10:04 ` David Rowley <dgrowleyml@gmail.com>
2020-05-26 10:30 ` Frank Millman <frank@chagford.com>
2020-05-26 11:45 ` David Rowley <dgrowleyml@gmail.com>
2020-05-26 12:07 ` Frank Millman <frank@chagford.com>
2020-05-26 11:41 ` Vik Fearing <vik@postgresfriends.org>
2020-05-26 11:49 ` David Rowley <dgrowleyml@gmail.com>
2020-05-29 12:14 ` Frank Millman <frank@chagford.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