实现说明
这个文件依次执行 SELECT / INSERT / CREATE 语句,涉及 docs。
机制上由这些部分组成:WHERE 把语句的作用范围收窄到满足条件的行;必填列上的 NOT NULL 限定;强制行身份的 PRIMARY KEY。
常见问题
它引用了哪些表或 CTE?
docs。
核心语句
SELECT id
FROM docs
WHERE to_tsvector('english', body) @@ to_tsquery('english', 'btree <-> index');
SELECT id
FROM docs
WHERE to_tsvector('english', body) @@ to_tsquery('english', 'index <-> btree');
INSERT INTO docs VALUES
(1, 'Btree index tuning for large tables'),
(2, 'Index btree internals explained'),
(3, 'Tuning guide for planners');
代码示例
CREATE TABLE docs (
id int PRIMARY KEY,
body text NOT NULL
);
INSERT INTO docs VALUES
(1, 'Btree index tuning for large tables'),
(2, 'Index btree internals explained'),
(3, 'Tuning guide for planners');
-- 短语搜索:to_tsquery 的 <->(FOLLOWED BY)要求两词相邻且按序
SELECT id
FROM docs
WHERE to_tsvector('english', body) @@ to_tsquery('english', 'btree <-> index');
-- 换序不命中:短语有方向;n 个词用 n-1 个 <-> 串联
SELECT id
FROM docs
WHERE to_tsvector('english', body) @@ to_tsquery('english', 'index <-> btree');