我有一個數據庫,並希望能夠在表中查找類似於以下內容的搜索: select * from table where where column like「abc%def%ghi 「 或 select * from table where'column def%ghi' 有沒有一種方法來索引列,以便它不太慢?有沒有辦法在Postgres中進行快速子串搜索
編輯: 我還可以澄清,數據庫是隻讀的,不會經常更新。
我有一個數據庫,並希望能夠在表中查找類似於以下內容的搜索: select * from table where where column like「abc%def%ghi 「 或 select * from table where'column def%ghi' 有沒有一種方法來索引列,以便它不太慢?有沒有辦法在Postgres中進行快速子串搜索
編輯: 我還可以澄清,數據庫是隻讀的,不會經常更新。
爲like
操作者使用操作符類varchar_pattern_ops
的一個或text_pattern_ops
create index test_index on test_table (col varchar_pattern_ops);
如果模式沒有在這種情況下,需要有新的戰略%
開始只會工作。文本搜索和索引
如果需要全文搜索, [pg_trgm](http://www.postgresql.org/docs/current/static/pgtrgm.html)可能有效。我過去使用過它,在某些條件下它運行得非常好。應該指出的是,指數變得相當大。 IIRC,大約是索引列大小的2.5倍。 – bma
選項包括:
full-text indexing與基於字典的搜索,包括前綴搜索支持,如to_tsvector(mycol) @@ to_tsquery('search:*')
text_pattern_ops
indexes支持前綴字符串匹配如LIKE 'abc%'
但不中文搜索like %blah%
;。 A reverse()
d索引可以用於後綴搜索。
pg_tgrm
trigram indexes較新版本as demonstrated in this recent dba.stackexchange.com post。
外部搜索和索引工具,如Apache Solr。
從上面給出的最少信息,我會說,只有三元指數將能幫助你,因爲你上的繩子做綴搜索,而不是找字典單詞。不幸的是,trigram指數是巨大的,而且效率低下;不要期待某種神奇的性能提升,並且要記住,他們需要花費大量的工作才能建立並保持最新的數據庫引擎。
如果你只需要到,例如,在整個表中獲取獨特的子串,你可以創建一個子指數:
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.
我認爲你需要一個全文[ index](http://www.postgresql.org/docs/8.3/static/textsearch.html) – 2013-07-13 19:35:53
此問題更適合http://dba.stackexchange.com/ –