pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: tjk@tksoft.com <tjk@tksoft.com>
Cc: admin@wtbwts.com
Cc: peter_e@gmx.net
Cc: pgsql-sql@postgreSQL.org
Subject: Re: [SQL] making 'like' queries quicker
Date: Sat, 18 Dec 1999 17:27:41 -0500
Message-ID: <1560.945556061@sss.pgh.pa.us> (raw)
In-Reply-To: <199912182153.NAA11586@uno.tksoft.com>

"tjk@tksoft.com" <tjk@tksoft.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



view thread (6+ messages)  latest in thread

Message-ID: <1560.945556061@sss.pgh.pa.us>
Permalink:  ../1560.945556061@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/1560.945556061@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: tgl@sss.pgh.pa.us, tjk@tksoft.com, admin@wtbwts.com, peter_e@gmx.net
  Subject: Re: [SQL] making 'like' queries quicker
  In-Reply-To: <1560.945556061@sss.pgh.pa.us>
  References: <199912182153.NAA11586@uno.tksoft.com> <1560.945556061@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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