2013-07-13 79 views
6

我有一個數據庫,並希望能夠在表中查找類似於以下內容的搜索: select * from table where where column like「abc%def%ghi 「 或 select * from table where'column def%ghi' 有沒有一種方法來索引列,以便它不太慢?有沒有辦法在Postgres中進行快速子串搜索

編輯: 我還可以澄清,數據庫是隻讀的,不會經常更新。

+0

我認爲你需要一個全文[ index](http://www.postgresql.org/docs/8.3/static/textsearch.html) – 2013-07-13 19:35:53

+0

此問題更適合http://dba.stackexchange.com/ –

回答

2

like操作者使用操作符類varchar_pattern_ops的一個或text_pattern_ops

create index test_index on test_table (col varchar_pattern_ops); 

如果模式沒有在這種情況下,需要有新的戰略%開始只會工作。文本搜索和索引

+2

如果需要全文搜索, [pg_trgm](http://www.postgresql.org/docs/current/static/pgtrgm.html)可能有效。我過去使用過它,在某些條件下它運行得非常好。應該指出的是,指數變得相當大。 IIRC,大約是索引列大小的2.5倍。 – bma

13

選項包括:

從上面給出的最少信息,我會說,只有三元指數將能幫助你,因爲你上的繩子做綴搜索,而不是找字典單詞。不幸的是,trigram指數是巨大的,而且效率低下;不要期待某種神奇的性能提升,並且要記住,他們需要花費大量的工作才能建立並保持最新的數據庫引擎。

1

如果你只需要到,例如,在整個表中獲取獨特的子串,你可以創建一個子指數:

CREATE INDEX i_test_sbstr ON tablename (substring(columname, 5, 3)); 
-- start at position 5, go for 3 characters 

It is important that the substring() parameters in the index definition are 
the same as you use in your query. 

裁判:http://www.postgresql.org/message-id/[email protected]

相關問題