5

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

我有一张表格,可以跟踪不同客户拥有的资产。

CREATE TABLE asset (
    id serial PRIMARY KEY,
    description text NOT NULL,
    customer_id uuid NOT NULL
);

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

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

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

CREATE INDEX idx_name ON asset(customer_id, description);

我只能在 tsvector 上创建索引:

CREATE INDEX idx_name ON asset USING gist(to_tsvector('english', asset.description));

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

4

1 回答 1

0

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

CREATE INDEX index_name 
ON table_name 
USING gist (field_one gist_trgm_ops, field_two gist_trgm_ops);
于 2019-11-20T16:20:27.203 回答