根据我迄今为止收集到的信息,如果您需要对 PostgreSQL(psql 9.6.2,服务器 9.6.5)数据库中包含大量条目(例如 1.2M+ 的订单)的表运行全文搜索),推荐的方法是为该表创建一个索引(在本例中我们创建了一个 GIN 索引),它应该允许您运行如下查询:
SELECT * FROM speech WHERE speech_tsv @@ plainto_tsquery('a text string')
Run Code Online (Sandbox Code Playgroud)
除了此查询的结果有时不包含任何相关搜索字符串之外,它通常需要 8 到 10 秒。
该数据库部署在一个相当大的多核 EC2 实例上,所以我在想,我们是否可以对数据库做其他事情来帮助这些查询运行得更快?
或者考虑到我们要求它搜索的大量文件和文本(即使通过索引),这个查询执行时间大约是合理的?
该表如下所示:
Table "public.speech"
Column | Type | Modifiers
---------------+-----------------------------+-----------------------------------------------------
speech_id | integer | not null default nextval('speech_id_seq'::regclass)
speechtype_id | smallint | not null
title | character varying | not null default ''::character varying
speechdate | date | default now()
location | character varying | not null default ''::character varying
source | character varying …
Run Code Online (Sandbox Code Playgroud)