Help with LIKE

Поиск
Список
Период
Сортировка
Искать

Help with LIKE

От:
"David Olbersen" <DOlbersen@stbernard.com>
Дата:
I have a table with 8,628,633 rows that I'd LIKE to search (ha ha).

I have a very simple query:
  SELECT * FROM tableA WHERE column1 LIKE '%something%';

tableA.column1 has an index on it and the database has been vacuumed recently. My problem is with the output of EXPLAIN:

+----------------------------------------------------------------+
|                         QUERY PLAN                             |
+----------------------------------------------------------------+
| Seq Scan on tableA  (cost=0.00..212651.61 rows=13802 width=46) |
|   Filter: (column1 ~~ '%something%'::text)                     |
+----------------------------------------------------------------+

I don't like that cost (2,12,651) at all! Is there anyway I can optimize this query? Make a different kind of index (it's currently btree)? Use substr or indexof or something instead of LIKE?

Thoughts?

--------------------------
David Olbersen 
iGuard Engineer
11415 West Bernardo Court 
San Diego, CA 92127 
1-858-676-2277 x2152

Re: [PERFORM] Help with LIKE

От:
Josh Berkus <josh@agliodbs.com>
Дата:
David,

> I have a table with 8,628,633 rows that I'd LIKE to search (ha ha).
> 
> I have a very simple query:
>   SELECT * FROM tableA WHERE column1 LIKE '%something%';

That's what's called an "unanchored text search".   That kind of query cannot 
be indexed using a regular index.

What you need is called "Full Text Indexing" or "Full Text Search".   Check 
out two resources:

1) contrib/tsearch in your PostgreSQL source code;
2) OpenFTS (www.openfts.org).

-- 
-Josh Berkus
 Aglio Database Solutions
 San Francisco

FAQ