Re: planer picks a bad plan (seq-scan instead of index)

From: "Gregory S(dot) Williamson" <gsw(at)globexplorer(dot)com>
To: "Richard Huxton" <dev(at)archonet(dot)com>, "Thomas H(dot)" <me(at)alternize(dot)com>
Cc: <pgsql-general(at)postgresql(dot)org>
Subject: Re: planer picks a bad plan (seq-scan instead of index)
Date: 2006-11-09 10:35:31
Message-ID: 71E37EF6B7DCC1499CEA0316A256832802B3E84A@loki.wc.globexplorer.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I am admittedly speaking up somewhat late here, and may be completely off base, but it seems to me that the "LIKE" operation is almost always going to be a loser, performance-wise, when there is an initial wildcard, e.g. "%superman re%" will require a sequential scan, while "superman re%" would not (assuming proper indexes matching case and type).

I'd suggest tsearch2, possibly, which uses GIST indexes and may perhaps be a better match for this sort of problem.

HTH,

Greg Williamson
DBA
GlobeXplorer LLC

-----Original Message-----
From: pgsql-general-owner(at)postgresql(dot)org on behalf of Richard Huxton
Sent: Thu 11/9/2006 1:22 AM
To: Thomas H.
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: [GENERAL] planer picks a bad plan (seq-scan instead of index)

Thomas H. wrote:
> --------------------
> SELECT * FROM shop.dvds
> LEFT JOIN oldtables.movies ON mov_id = dvd_mov_id
> LEFT JOIN shop.data_soundmedia ON sm_info_ean = dvd_ean
> WHERE (lower(mov_name) LIKE '%superman re%' OR lower(dvd_name) like
> '%superman re%' OR lower(dvd_edition) LIKE '%superman re%')
> --------------------

Try putting your conditions as part of the join:
SELECT * FROM shop.dvds
LEFT JOIN
oldtables.movies
ON
mov_id = dvd_mov_id
AND (
lower(mov_name) LIKE '%superman re%'
OR lower(dvd_name) like '%superman re%'
OR lower(dvd_edition) LIKE '%superman re%'
)
LEFT JOIN shop.data_soundmedia ON sm_info_ean = dvd_ean

I'd also be tempted to look at a tsearch2 setup for the word searches.
--
Richard Huxton
Archonet Ltd

---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
choose an index scan if your joining column's datatypes do not
match

-------------------------------------------------------
Click link below if it is SPAM gsw(at)globexplorer(dot)com
"https://mailscanner.globexplorer.com/dspam/dspam.cgi?signatureID=4552efed289104295495211&user=gsw(at)globexplorer(dot)com&retrain=spam&template=history&history_page=1"
!DSPAM:4552efed289104295495211!
-------------------------------------------------------

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Shane Ambler 2006-11-09 11:03:23 Re: RULE - special variables?
Previous Message Richard Huxton 2006-11-09 10:19:47 Re: [SQL] [ADMIN] Is there anyway to...