Re: [SQL] qurey plan and indices - Mailing list pgsql-sql

From Patrick Giagnocavo
Subject Re: [SQL] qurey plan and indices
Date
Msg-id 37A99D4F.610E6F7C@redrose.net
Whole thread Raw
In response to qurey plan and indices  (MESZAROS Attila <tilla@chiara.csoma.elte.hu>)
Responses Re: [SQL] qurey plan and indices  (Tom Lane <tgl@sss.pgh.pa.us>)
Re: [SQL] qurey plan and indices  (MESZAROS Attila <tilla@chiara.csoma.elte.hu>)
List pgsql-sql
MESZAROS Attila wrote:
> 
> Hi,
> 
> I've experienced a brutal speedup (order of 2) separateing the following
> subquery and making it manually:
> 
> explain select name,description
> from descriptions
> where in (select name
>                 from descriptions
>                 where description like '%Bankverbindung%');
> 
> Seq Scan on descriptions  (cost=163.98 rows=3575 width=24)
>   SubPlan
>     ->  Seq Scan on descriptions  (cost=163.98 rows=2 width=12)
> [I had no patient to wait the resuls...]
>

Correct me if I am wrong, however if using 

LIKE '%something%'
(which means, the field contains 'something' somewhere in the field)

there is never a chance to use the index you have created - a
sequential table scan MUST be made, thus you have to read all 3575
rows to return the set.

However, if you change your query so that you are looking for 

LIKE 'Bankverbindung%'

then an index can be used becase you know what the first few
characters are known.

Hope this helps,

Cordially

Patrick Giagnocavo
a222@redrose.net


pgsql-sql by date:

Previous
From: rob caSSon
Date:
Subject: primary key view failure
Next
From: Tom Lane
Date:
Subject: Re: [SQL] primary key view failure