Re: [SQL] making 'like' queries quicker

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "tjk(at)tksoft(dot)com" <tjk(at)tksoft(dot)com>
Cc: admin(at)wtbwts(dot)com (admin), peter_e(at)gmx(dot)net, pgsql-sql(at)postgreSQL(dot)org
Subject: Re: [SQL] making 'like' queries quicker
Date: 1999-12-18 22:27:41
Message-ID: 1560.945556061@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

"tjk(at)tksoft(dot)com" <tjk(at)tksoft(dot)com> writes:
> A general rule of thumb is that indexes
> only work on exact matches.

Troy's rule of thumb is correct, but there's an important additional
property of some types of indexes: you can scan them in order (for
whatever kind of "order" is imposed by the index comparison operator).
Postgres' btree indexes work that way, but hash indexes don't.

An ordered index can be used to process inequalities and range
queries as well as exact-match queries. For example, with a btree
index you can do something like
WHERE lastname >= 'Smith' AND lastname <= 'Szekely'
fairly efficiently: you scan the portion of the index falling between
the given limits, and then extract the main-table records pointed to
by those index entries.

Therefore, it's practical to use a btree index to speed up match queries
that require a match at the start of the string. For example, given
WHERE lastname LIKE 'Smith%'
Postgres will generate additional clauses
lastname >= 'Smith' AND lastname <= 'Smith\377'
which can be used with a btree index to restrict the number of records
that have to be looked at. You still have to do the LIKE comparison,
in general (consider LIKE 'Smith%Jr') but you don't have to do it for
every record in the table.

There isn't any obvious way to apply this trick for an unanchored match,
though (as in LIKE '%Smith%').

However, if you are actually interested in searching for whole words,
you could consider making an index that lists all of the whole words in
your target field, and doing an exact match with that index. See
contrib/fulltextindex in the Postgres distribution for an example.

regards, tom lane

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 1999-12-18 22:48:07 Re: [SQL] [Q] Merging two queries?
Previous Message tjk@tksoft.com 1999-12-18 21:53:51 Re: [SQL] making 'like' queries quicker