如何在 Postgresql 中创建多列 GiST 索引

And*_*ert 5 postgresql tsvector psql

postgresql文档指定 GiST 索引可以有多个列,但没有提供可能是什么样子的示例。

我有一个表格来跟踪不同客户拥有的资产。

CREATE TABLE asset (
    id serial PRIMARY KEY,
    description text NOT NULL,
    customer_id uuid NOT NULL
);
Run Code Online (Sandbox Code Playgroud)

我正在编写一个查询,允许客户根据描述中的词来搜索资产。

SELECT *
FROM asset
WHERE to_tsvector('english', asset.description) @@ plainto_tsvector('english', ?)
AND asset.customer_id = ?;
Run Code Online (Sandbox Code Playgroud)

如果这是一个非 tsvector 查询,我会构建一个简单的多列索引

CREATE INDEX idx_name ON asset(customer_id, description);
Run Code Online (Sandbox Code Playgroud)

只能在 tsvector 上创建索引:

CREATE INDEX idx_name ON asset USING gist(to_tsvector('english', asset.description));
Run Code Online (Sandbox Code Playgroud)

但是,查询优化器不使用 gist 索引,因为它似乎customer_id要先进行过滤。有没有办法可以customer_id以某种方式将非 tsvector 字段包含在要点索引中,或者我不走运?

Kev*_*ker 2

您可以使用以下命令在 Postrgres 中创建多列 GIN 或 GiST 索引:

CREATE INDEX index_name 
ON table_name 
USING gist (field_one gist_trgm_ops, field_two gist_trgm_ops);
Run Code Online (Sandbox Code Playgroud)