小编Fon*_*exn的帖子

使用 SIMILAR TO 和许多前缀优化查询

我在 PostgreSQL 9.6中有一个查询需要很长时间才能运行:

SELECT DISTINCT ON (gc.number)
       gc.number, gc.code, ch.client_id, ch.client_parent_id
FROM client gc, client_hierarchy ch
WHERE gc.code = ch.client_id
AND gc.number in (SELECT NUMB FROM directory
                  WHERE ACTIVE = TRUE
                  AND NUMB SIMILAR TO '(ABTR|GREW|POEW)%');
Run Code Online (Sandbox Code Playgroud)

SIMILAR TO接收许多不同NUMB的作为参数。ABTR|GREW|POEW大约 3.500 多。

表定义:

CREATE TABLE directory (
    id BIGINT PRIMARY KEY NOT NULL,
    active BOOLEAN NOT NULL,
    numb VARCHAR(8),
    branch VARCHAR(20),
    city VARCHAR(50),
    modified_date TIMESTAMP NOT NULL,
    name VARCHAR(200)
);
CREATE INDEX dir_numb_index ON directory (numb); …
Run Code Online (Sandbox Code Playgroud)

postgresql performance index postgresql-performance

5
推荐指数
1
解决办法
123
查看次数