Re: optimal sql

From: Tomasz Myrta <jasiek(at)klaster(dot)net>
To: Michael Hostbaek <mich(at)the-lab(dot)org>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: optimal sql
Date: 2003-01-22 10:02:23
Message-ID: 3E2E6C2F.6020902@klaster.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Michael Hostbaek wrote:

>Hi,
>
>I am running postgresql 7.2.3 on a test server (with potential of
>becoming my production server).
>
>On the server I have a perl script, that is grabbing some data from a
>inventory database (local) - with some subselects.
>The query is like this:
>
>
>my $sth = $ppdb->prepare("
> select partno, create_date, mfg, condition, gescode, qty,
>cmup,(SELECT partno_main FROM partno_lookup where
> partno_lookup.partno_alias ilike (?|| inventory.partno ||?) and mfg
>ilike ? limit 1)
> as partno_main, (SELECT subcat FROM partno_lookup where
>partno_lookup.partno_alias ilike
> (?|| inventory.partno ||?) and mfg ilike ? limit 1) as subcat,
> (SELECT key_search FROM partno_lookup where
>partno_lookup.partno_alias ilike (?|| inventory.partno ||?) and
> mfg ilike ? limit 1) as key_search,
> (SELECT text_desc FROM descriptions where
>descriptions.partno=(SELECT partno_main FROM partno_lookup
> where partno_lookup.partno_alias ilike (?|| inventory.partno ||?)
>and mfg ilike ? limit 1)
> limit 1) as descri from inventory where mfg ilike ? and ? <
>create_date $refurbed order by key_search,
> subcat, partno_main, status DESC ");

1. Probably your query can't use index on table partno_lookup.partno_alias.
Consider creating table aliases which contains all possible parts aliases.
You can change then "ilike" into "=" which will use indexes.

2. You don't need subselects in your query. You can change them into ordinary
table joins and use "group by" or "distinct on". In your case selecting from
partno_lookup is executed several times per one row.

3. Explain analyze would be helpful like in most performance cases...
The same with SQL query instead of Perl script.

4. This is rather a sql problem, than hardware/configuration one.

Regards,
Tomasz Myrta

In response to

  • optimal sql at 2003-01-22 08:54:54 from Michael Hostbaek

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Michael Paesold 2003-01-22 10:03:47 Re: optimal sql
Previous Message Ludwig Lim 2003-01-22 09:53:11 Re: NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index