pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
search on tables
2+ messages / 2 participants
[nested] [flat]

* search on tables
@ 2003-03-26 22:26  Sidar Lopez Cruz <sidarlopez@hotmail.com>
  0 siblings, 0 replies; 2+ messages in thread

From: Sidar Lopez Cruz @ 2003-03-26 22:26 UTC (permalink / raw)
  To: pgsql-admin

why searching with like or ilike on tables with too many records is too slow?
i have a table like this

create table sujetos (cedula varchar(20) not null primary key, nombre varchar(255), id_tipo_documento bigint);
create index idx_nombre on sujetos (nombre);

and them, i insert 5,000,000 of records from mssql

then, i do something like that, select * from sujetos where nombre like 'LOPEZ CRUZ SIDAR%', and the request from the server is normal, but when i do this, select * from sujetos where nombre like 'lopez%sidar%';

the server die.... searching this string....

:-) Sidar Lopez Cruz
- Cero Riesgo, S.A.

^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: search on tables
@ 2003-03-27 02:14  Nikolaus Dilger <nikolaus@dilger.cc>
  0 siblings, 0 replies; 2+ messages in thread

From: Nikolaus Dilger @ 2003-03-27 02:14 UTC (permalink / raw)
  To: sidarlopez@hotmail.com; +Cc: pgsql-admin

Sidar,

1) nombre like 'LOPEZ CRUZ SIDAR%' is much more
selective than

2) nombre like 'lopez%sidar%'

The database needs to get all rows that match up to the
first wildcard.
In 1) that is most likely a very low number and your
index will help PostgreSQL to find those rows quickly.

In 2) that is potetially 1,000s of rows out of the
5,000,000 in your table.  In a second step the end
result is selected from the records that match 'lopez%'

From a performance standpoint the worst you can do with
LIKE is to have the wildcard at the very beginning. 
Then PostgreSQL needs to read all records.
For example
WHERE nombre LIKE '%lopez%';
However, this may get you a match for "Jennifer Lopez".

Many applications have a column first_name and another
for last_name.
Then you can have a more selective WHERE clause
WHERE last_name='Lopez'
AND first_name like 'J%'

Regards,
Nikolaus Dilger

On Wed, 26 Mar 2003, "Sidar Lopez Cruz" wrote:








why searching with like or ilike on tables with too 
many records is too slow?
i have a table like this
 
create table sujetos (cedula varchar(20) not null 
primary key, nombre varchar(255), id_tipo_documento
bigint);
create index idx_nombre on sujetos 
(nombre);
 
and them, i insert 5,000,000 of records from 
mssql
 
then, i do something like that, select * from 
sujetos where nombre like 'LOPEZ CRUZ SIDAR%', and the
request from the server 
is normal, but when i do this, select * from sujetos
where nombre like 
'lopez%sidar%';
 
the server die.... searching this 
string....
:-) Sidar Lopez Cruz- Cero Riesgo, 
S.A.




^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2003-03-27 02:14 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2003-03-26 22:26 search on tables Sidar Lopez Cruz <sidarlopez@hotmail.com>
2003-03-27 02:14 Re: search on tables Nikolaus Dilger <nikolaus@dilger.cc>

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