pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedHelp with sql
7+ messages / 5 participants
[nested] [flat]
* Help with sql
@ 2012-07-06 21:34 Perry Smith <pedzsan@gmail.com>
0 siblings, 2 replies; 7+ messages in thread
From: Perry Smith @ 2012-07-06 21:34 UTC (permalink / raw)
To: pgsql-general
Hi Guys,
This isn't a PostgreSQL specific question but just a SQL question. If this is not an appropriate question for this list, please let me know.
It is also, perhaps, a really silly question.
This query (without the 'explain' keyword) , when executed takes forever and a day:
> condor_development=> explain select id from filesets where id not in ( select fileset_id from service_pack_fileset_maps );
> QUERY PLAN
> ----------------------------------------------------------------------------------------------
> Seq Scan on filesets (cost=0.00..71937742.00 rows=26088 width=4)
> Filter: (NOT (SubPlan 1))
> SubPlan 1
> -> Materialize (cost=0.00..2517.78 rows=95852 width=4)
> -> Seq Scan on service_pack_fileset_maps (cost=0.00..1663.52 rows=95852 width=4)
> (5 rows)
This query returns within a second:
> condor_development=> explain select id from filesets where id not in ( select distinct fileset_id from service_pack_fileset_maps );
> QUERY PLAN
> ----------------------------------------------------------------------------------------------
> Seq Scan on filesets (cost=2102.31..3153.53 rows=26088 width=4)
> Filter: (NOT (hashed SubPlan 1))
> SubPlan 1
> -> HashAggregate (cost=1903.15..2062.48 rows=15933 width=4)
> -> Seq Scan on service_pack_fileset_maps (cost=0.00..1663.52 rows=95852 width=4)
> (5 rows)
The difference is the "distinct" keyword in the inner select.
What I'm confused about is why isn't the "distinct" implicit? I thought the construct "blah in ( select ... )" was using "sets" and an item (I thought) can not be in a set more than once.
Perhaps my question is the opposite really? Why would you not always use "distinct" in the inner select when the operator is "in" or "not in" ?
And if I can throw in another question on top: is there a different method other than "not in" that would work better?
Thank you guys for the help and a really awesome database.
pedz
(this is *suppose* to be sent as plain text... I hope my mailer does what it is told)
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Help with sql
@ 2012-07-07 00:43 Rob Sargent <robjsargent@gmail.com>
parent: Perry Smith <pedzsan@gmail.com>
1 sibling, 0 replies; 7+ messages in thread
From: Rob Sargent @ 2012-07-07 00:43 UTC (permalink / raw)
To: pgsql-general
On 07/06/2012 03:34 PM, Perry Smith wrote:
> Hi Guys,
>
> This isn't a PostgreSQL specific question but just a SQL question. If this is not an appropriate question for this list, please let me know.
>
> It is also, perhaps, a really silly question.
>
> This query (without the 'explain' keyword) , when executed takes forever and a day:
>
>> condor_development=> explain select id from filesets where id not in ( select fileset_id from service_pack_fileset_maps );
>> QUERY PLAN
>> ----------------------------------------------------------------------------------------------
>> Seq Scan on filesets (cost=0.00..71937742.00 rows=26088 width=4)
>> Filter: (NOT (SubPlan 1))
>> SubPlan 1
>> -> Materialize (cost=0.00..2517.78 rows=95852 width=4)
>> -> Seq Scan on service_pack_fileset_maps (cost=0.00..1663.52 rows=95852 width=4)
>> (5 rows)
>
> This query returns within a second:
>
>> condor_development=> explain select id from filesets where id not in ( select distinct fileset_id from service_pack_fileset_maps );
>> QUERY PLAN
>> ----------------------------------------------------------------------------------------------
>> Seq Scan on filesets (cost=2102.31..3153.53 rows=26088 width=4)
>> Filter: (NOT (hashed SubPlan 1))
>> SubPlan 1
>> -> HashAggregate (cost=1903.15..2062.48 rows=15933 width=4)
>> -> Seq Scan on service_pack_fileset_maps (cost=0.00..1663.52 rows=95852 width=4)
>> (5 rows)
>
> The difference is the "distinct" keyword in the inner select.
>
> What I'm confused about is why isn't the "distinct" implicit? I thought the construct "blah in ( select ... )" was using "sets" and an item (I thought) can not be in a set more than once.
>
> Perhaps my question is the opposite really? Why would you not always use "distinct" in the inner select when the operator is "in" or "not in" ?
>
> And if I can throw in another question on top: is there a different method other than "not in" that would work better?
>
> Thank you guys for the help and a really awesome database.
> pedz
> (this is *suppose* to be sent as plain text... I hope my mailer does what it is told)
>
>
Well they are distinct records, they just may have the same values. And
I'm not trying to be flippant. We don't see the structure of those
table: are all the id fields involved primary keys or with unique index
coverage? Does "not exists ( select fileset.id = fileset_id from
service_pack_fileset_map) change the behaviour?
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Help with sql
@ 2012-07-07 00:56 Steve Crawford <scrawford@pinpointresearch.com>
parent: Perry Smith <pedzsan@gmail.com>
1 sibling, 1 reply; 7+ messages in thread
From: Steve Crawford @ 2012-07-07 00:56 UTC (permalink / raw)
To: Perry Smith <pedzsan@gmail.com>; +Cc: pgsql-general
On 07/06/2012 02:34 PM, Perry Smith wrote:
> Hi Guys,
>
> This isn't a PostgreSQL specific question but just a SQL question. If this is not an appropriate question for this list, please let me know.
>
> It is also, perhaps, a really silly question.
>
> This query (without the 'explain' keyword) , when executed takes forever and a day:
>
>> condor_development=> explain select id from filesets where id not in ( select fileset_id from service_pack_fileset_maps );
>> QUERY PLAN
>> ----------------------------------------------------------------------------------------------
>> Seq Scan on filesets (cost=0.00..71937742.00 rows=26088 width=4)
>> Filter: (NOT (SubPlan 1))
>> SubPlan 1
>> -> Materialize (cost=0.00..2517.78 rows=95852 width=4)
>> -> Seq Scan on service_pack_fileset_maps (cost=0.00..1663.52 rows=95852 width=4)
>> (5 rows)
> This query returns within a second:
>
>> condor_development=> explain select id from filesets where id not in ( select distinct fileset_id from service_pack_fileset_maps );
>> QUERY PLAN
>> ----------------------------------------------------------------------------------------------
>> Seq Scan on filesets (cost=2102.31..3153.53 rows=26088 width=4)
>> Filter: (NOT (hashed SubPlan 1))
>> SubPlan 1
>> -> HashAggregate (cost=1903.15..2062.48 rows=15933 width=4)
>> -> Seq Scan on service_pack_fileset_maps (cost=0.00..1663.52 rows=95852 width=4)
>> (5 rows)
> The difference is the "distinct" keyword in the inner select.
>
> What I'm confused about is why isn't the "distinct" implicit? I thought the construct "blah in ( select ... )" was using "sets" and an item (I thought) can not be in a set more than once.
>
> Perhaps my question is the opposite really? Why would you not always use "distinct" in the inner select when the operator is "in" or "not in" ?
>
> And if I can throw in another question on top: is there a different method other than "not in" that would work better?
Actually it is *very* PostgreSQL specific. In fact, it may even be
PostgreSQL *version* specific as you are delving into how the planner
decides how to handle a query.
It appears that the planner is assuming, based on collected stats and
available indexes, that there will be roughly 1/6 the records returned
by the "distinct" query and thus chose a different method to join the
records. One useful piece of information would be the indexes on the two
tables.
As to other methods, you can use:
... where not exists (select 1 from service_pack_fileset_maps where
fileset_id = filesets.id)...
(Note: as alluded to above, ...not in... works better in some releases
and ...not exists... better in others due to improvements over time.)
Still another method:
select id from filesets except select fileset_id from
service_pack_fileset_maps;
Cheers,
Steve
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Help with sql
@ 2012-07-07 13:48 Perry Smith <pedzsan@gmail.com>
parent: Steve Crawford <scrawford@pinpointresearch.com>
0 siblings, 1 reply; 7+ messages in thread
From: Perry Smith @ 2012-07-07 13:48 UTC (permalink / raw)
To: pgsql-general
On Jul 6, 2012, at 7:56 PM, Steve Crawford wrote:
> On 07/06/2012 02:34 PM, Perry Smith wrote:
>> Hi Guys,
>>
>> This isn't a PostgreSQL specific question but just a SQL question. If this is not an appropriate question for this list, please let me know.
>>
>> It is also, perhaps, a really silly question.
>>
>> This query (without the 'explain' keyword) , when executed takes forever and a day:
>>
>>> condor_development=> explain select id from filesets where id not in ( select fileset_id from service_pack_fileset_maps );
>>> QUERY PLAN
>>> ----------------------------------------------------------------------------------------------
>>> Seq Scan on filesets (cost=0.00..71937742.00 rows=26088 width=4)
>>> Filter: (NOT (SubPlan 1))
>>> SubPlan 1
>>> -> Materialize (cost=0.00..2517.78 rows=95852 width=4)
>>> -> Seq Scan on service_pack_fileset_maps (cost=0.00..1663.52 rows=95852 width=4)
>>> (5 rows)
>> This query returns within a second:
>>
>>> condor_development=> explain select id from filesets where id not in ( select distinct fileset_id from service_pack_fileset_maps );
>>> QUERY PLAN
>>> ----------------------------------------------------------------------------------------------
>>> Seq Scan on filesets (cost=2102.31..3153.53 rows=26088 width=4)
>>> Filter: (NOT (hashed SubPlan 1))
>>> SubPlan 1
>>> -> HashAggregate (cost=1903.15..2062.48 rows=15933 width=4)
>>> -> Seq Scan on service_pack_fileset_maps (cost=0.00..1663.52 rows=95852 width=4)
>>> (5 rows)
>> The difference is the "distinct" keyword in the inner select.
>>
>> What I'm confused about is why isn't the "distinct" implicit? I thought the construct "blah in ( select ... )" was using "sets" and an item (I thought) can not be in a set more than once.
>>
>> Perhaps my question is the opposite really? Why would you not always use "distinct" in the inner select when the operator is "in" or "not in" ?
>>
>> And if I can throw in another question on top: is there a different method other than "not in" that would work better?
> Actually it is *very* PostgreSQL specific. In fact, it may even be PostgreSQL *version* specific as you are delving into how the planner decides how to handle a query.
>
> It appears that the planner is assuming, based on collected stats and available indexes, that there will be roughly 1/6 the records returned by the "distinct" query and thus chose a different method to join the records. One useful piece of information would be the indexes on the two tables.
>
> As to other methods, you can use:
> ... where not exists (select 1 from service_pack_fileset_maps where fileset_id = filesets.id)...
> (Note: as alluded to above, ...not in... works better in some releases and ...not exists... better in others due to improvements over time.)
>
> Still another method:
> select id from filesets except select fileset_id from service_pack_fileset_maps;
Thanks guys.
Small side note: I thought I saw "set difference" in the documentation but I couldn't find it. It appears "EXCEPT" is set difference. Thank you for that tidbit.
The database is mostly static. I run through a very lengthy process to populate the database maybe once a month and then it is 99% read-only. By far, most of the accesses are via a view that I have that is rather long and ugly so I won't paste it in. I've tried to make this particular view as fast as possible so the indexes, etc are what I think will help that out.
The version is psql (PostgreSQL) 9.0.4
Each table has a key of "id" -- this database was created by / used by Ruby on Rails and that is how it likes to do things.
> condor_development=> \d service_pack_fileset_maps
> Table "public.service_pack_fileset_maps"
> Column | Type | Modifiers
> -----------------+-----------------------------+------------------------------------------------------------------------
> id | integer | not null default nextval('service_pack_fileset_maps_id_seq'::regclass)
> service_pack_id | integer | not null
> fileset_id | integer | not null
> created_at | timestamp without time zone |
> updated_at | timestamp without time zone |
> Indexes:
> "service_pack_fileset_maps_pkey" PRIMARY KEY, btree (id)
> "service_pack_fileset_maps_service_pack_id_key" UNIQUE, btree (service_pack_id, fileset_id)
> "index_service_pack_fileset_maps_on_fileset_id" btree (fileset_id)
> Foreign-key constraints:
> "service_pack_fileset_maps_fileset_id_fkey" FOREIGN KEY (fileset_id) REFERENCES filesets(id) ON DELETE CASCADE DEFERRABLE
> "service_pack_fileset_maps_service_pack_id_fkey" FOREIGN KEY (service_pack_id) REFERENCES service_packs(id) ON DELETE CASCADE DEFERRABLE
> condor_development=> \d filesets
> Table "public.filesets"
> Column | Type | Modifiers
> ------------+-----------------------------+-------------------------------------------------------
> id | integer | not null default nextval('filesets_id_seq'::regclass)
> lpp_id | integer | not null
> vrmf | character varying(255) | not null
> created_at | timestamp without time zone |
> updated_at | timestamp without time zone |
> Indexes:
> "filesets_pkey" PRIMARY KEY, btree (id)
> "filesets_lpp_id_key" UNIQUE, btree (lpp_id, vrmf)
> Foreign-key constraints:
> "filesets_lpp_id_fkey" FOREIGN KEY (lpp_id) REFERENCES lpps(id) ON DELETE CASCADE DEFERRABLE
> Referenced by:
> TABLE "fileset_aix_file_maps" CONSTRAINT "fileset_aix_file_maps_fileset_id_fkey" FOREIGN KEY (fileset_id) REFERENCES filesets(id) ON DELETE CASCADE DEFERRABLE
> TABLE "fileset_ptf_maps" CONSTRAINT "fileset_ptf_maps_fileset_id_fkey" FOREIGN KEY (fileset_id) REFERENCES filesets(id) ON DELETE CASCADE DEFERRABLE
> TABLE "package_fileset_maps" CONSTRAINT "package_fileset_maps_fileset_id_fkey" FOREIGN KEY (fileset_id) REFERENCES filesets(id) ON DELETE CASCADE DEFERRABLE
> TABLE "service_pack_fileset_maps" CONSTRAINT "service_pack_fileset_maps_fileset_id_fkey" FOREIGN KEY (fileset_id) REFERENCES filesets(id) ON DELETE CASCADE DEFERRABLE
> TABLE "upd_pc_views" CONSTRAINT "upd_pc_views_fileset_id_fkey" FOREIGN KEY (fileset_id) REFERENCES filesets(id) ON DELETE CASCADE DEFERRABLE
Thank you again for your help,
pedz
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Help with sql
@ 2012-07-07 14:39 Chris Angelico <rosuav@gmail.com>
parent: Perry Smith <pedzsan@gmail.com>
0 siblings, 0 replies; 7+ messages in thread
From: Chris Angelico @ 2012-07-07 14:39 UTC (permalink / raw)
To: pgsql-general
On Sat, Jul 7, 2012 at 11:48 PM, Perry Smith <pedzsan@gmail.com> wrote:
> The database is mostly static. I run through a very lengthy process to populate the database maybe once a month and then it is 99% read-only.
Do you run an ANALYZE on the table after populating it? Postgres needs
up-to-date statistics for best results. I'd recommend doing an
explicit 'VACUUM ANALYZE' once your data's loaded, and then try your
queries after that - it might not do much, but it also might give a
massive improvement.
ChrisA
^ permalink raw reply [nested|flat] 7+ messages in thread
* Help with SQL
@ 2018-05-28 00:42 anand086 <anand086@gmail.com>
0 siblings, 1 reply; 7+ messages in thread
From: anand086 @ 2018-05-28 00:42 UTC (permalink / raw)
To: pgsql-general
Hi,
I have a requirement of writing plpgsql function to create partial indexes
on child tables if it exists on parent table. The function will have
schemname, childtablename, tableowner as input.
I am using the below code to identify the indexname and index definition
from parent table --
```
with idx as
(select indexrelid::regclass indexname, indisunique,
indisprimary from pg_index where indrelid in
(select oid from pg_class where relname in (select
tablename from pg_indexes where tablename='test_booking')
)
and indpred is not null
)
select idxs.tablename, idxs.indexname, idxs.indexdef,
idx.indisunique from pg_indexes as idxs join idx on
idxs.indexname=split_part(idx.indexname::text , '.' ,2)
```
Suppose the indexdef is on parent table is --
CREATE UNIQUE INDEX uniq_test_booking_1 ON demo.test_booking USING btree
(col1,col2 ,col3, col4,col5, col6) WHERE ((col4 IS NOT NULL) AND (col6 IS
NOT NULL))
Now, what I am trying to achieve is to create and execute the below sql,
wherein I replace the indexname with uniq_<child_table_name>_<randomtext>
and tablename with the childtablename part of function input.
CREATE UNIQUE INDEX uniq_test_booking_20180527_gdhsd ON
demo.test_booking_20180527 USING btree (col1,col2 ,col3, col4,col5, col6)
WHERE ((col4 IS NOT NULL) AND (col6 IS NOT NULL))
Using substring I am trying to break the SQL statement in 2 and then later
concatenate it.
The first part is substring(idxrec.indexdef from 0 for 21); --> output is
"create unique index " statement.
and for the 2nd part, starting for USING until the end. But I am unable to
get the 2nd part of sql.
```
if idxrec.indisunique='t' then
SELECT substr(concat(md5(random()::text), md5(random()::text)), 0,
7) into var;
idxname:='uniq_'||idxrec.tablename||'_'||var;
raise notice 'Index name will be %', idxname;
createStmts1:=substring(idxrec.indexdef from 0 for 21); --> gives me
the "create unique index"
raise notice 'String1 %', createStmts1;
createStmts2:=
raise notice 'String2 %', createStmts2;
```
Is this the correct way? Any better suggestion?
How can I achieve this?
--
Sent from: http://www.postgresql-archive.org/PostgreSQL-general-f1843780.html
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Help with SQL
@ 2018-05-28 03:19 Rob Sargent <robjsargent@gmail.com>
parent: anand086 <anand086@gmail.com>
0 siblings, 0 replies; 7+ messages in thread
From: Rob Sargent @ 2018-05-28 03:19 UTC (permalink / raw)
To: anand086 <anand086@gmail.com>; +Cc: pgsql-general
> On May 27, 2018, at 6:42 PM, anand086 <anand086@gmail.com> wrote:
>
> Hi,
>
> I have a requirement of writing plpgsql function to create partial indexes
> on child tables if it exists on parent table. The function will have
> schemname, childtablename, tableowner as input.
>
> I am using the below code to identify the indexname and index definition
> from parent table --
>
> ```
> with idx as
> (select indexrelid::regclass indexname, indisunique,
> indisprimary from pg_index where indrelid in
> (select oid from pg_class where relname in (select
> tablename from pg_indexes where tablename='test_booking')
> )
> and indpred is not null
> )
> select idxs.tablename, idxs.indexname, idxs.indexdef,
> idx.indisunique from pg_indexes as idxs join idx on
> idxs.indexname=split_part(idx.indexname::text , '.' ,2)
> ```
>
> Suppose the indexdef is on parent table is --
>
> CREATE UNIQUE INDEX uniq_test_booking_1 ON demo.test_booking USING btree
> (col1,col2 ,col3, col4,col5, col6) WHERE ((col4 IS NOT NULL) AND (col6 IS
> NOT NULL))
>
> Now, what I am trying to achieve is to create and execute the below sql,
> wherein I replace the indexname with uniq_<child_table_name>_<randomtext>
> and tablename with the childtablename part of function input.
>
> CREATE UNIQUE INDEX uniq_test_booking_20180527_gdhsd ON
> demo.test_booking_20180527 USING btree (col1,col2 ,col3, col4,col5, col6)
> WHERE ((col4 IS NOT NULL) AND (col6 IS NOT NULL))
>
> Using substring I am trying to break the SQL statement in 2 and then later
> concatenate it.
> The first part is substring(idxrec.indexdef from 0 for 21); --> output is
> "create unique index " statement.
> and for the 2nd part, starting for USING until the end. But I am unable to
> get the 2nd part of sql.
>
>
> ```
> if idxrec.indisunique='t' then
> SELECT substr(concat(md5(random()::text), md5(random()::text)), 0,
> 7) into var;
> idxname:='uniq_'||idxrec.tablename||'_'||var;
> raise notice 'Index name will be %', idxname;
> createStmts1:=substring(idxrec.indexdef from 0 for 21); --> gives me
> the "create unique index"
> raise notice 'String1 %', createStmts1;
> createStmts2:=
> raise notice 'String2 %', createStmts2;
> ```
>
> Is this the correct way? Any better suggestion?
>
> How can I achieve this?
>
>
Is using a stored procedure a required? To do so would require “dynamic sql” but imho that’s not the best use of stored procedures. Perhaps you could use your “with ids” sql to get the list of required indices with names, columns etc, place those values (sed, awk, perl, python, even sql) into versions of your create index code and stick those generated files in hopefully the same source code repository which has the definition of the parent and children tables. They will likely all need maintenace at the same time.
^ permalink raw reply [nested|flat] 7+ messages in thread
end of thread, other threads:[~2018-05-28 03:19 UTC | newest]
Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2012-07-06 21:34 Help with sql Perry Smith <pedzsan@gmail.com>
2012-07-07 00:43 ` Rob Sargent <robjsargent@gmail.com>
2012-07-07 00:56 ` Steve Crawford <scrawford@pinpointresearch.com>
2012-07-07 13:48 ` Perry Smith <pedzsan@gmail.com>
2012-07-07 14:39 ` Chris Angelico <rosuav@gmail.com>
2018-05-28 00:42 Help with SQL anand086 <anand086@gmail.com>
2018-05-28 03:19 ` Re: Help with SQL Rob Sargent <robjsargent@gmail.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