小编shy*_*ent的帖子

多个相关表的全文搜索:索引和性能

我们有以下数据库结构

CREATE TABLE objects (
    id       int   PRIMARY KEY,
    name     text,
    address  text
);

CREATE TABLE tasks (
    id int           PRIMARY KEY,
    object_id int    NOT NULL,
    actor_id int     NOT NULL,
    description text
);

CREATE TABLE actors (
    id   int  PRIMARY KEY,
    name text
);
Run Code Online (Sandbox Code Playgroud)

用户输入以空格分隔的单词列表(基本上是搜索词),我们必须搜索满足以下条件的任务:如果每个搜索词在任务描述的串联中至少出现一次,则该任务是“匹配”,其关联对象的名称和地址以及关联参与者的名称。

现在,如果我们不关心性能,我们可以这样做(给定查询“foo bar”):

SELECT t.id, t.description
FROM tasks AS t
INNER JOIN actors AS a ON t.actor_id = a.id
INNER JOIN objects AS o ON t.object_id = o.id
WHERE to_tsvector(concat_ws(' ', t.description, o.name, o.address, a.name)) @@ …
Run Code Online (Sandbox Code Playgroud)

postgresql performance full-text-search postgresql-performance

6
推荐指数
2
解决办法
6365
查看次数