gpt4 book ai didi

sql - 如何加速 pg_trgm?

转载 作者:行者123 更新时间:2023-11-29 12:02:32 26 4
gpt4 key购买 nike

创建表:

CREATE TABLE public.personal
(
id bigserial NOT NULL PRIMARY KEY,
first_name character varying(255) NOT NULL,
last_name character varying(255),
middle_name character varying(255),
deleted integer NOT NULL DEFAULT 0
);

查询:

select last_name, first_name, middle_name, similarity(concat_ws(' ', last_name, first_name, middle_name), 'Smit') AS sml
from personal ORDER BY sml DESC LIMIT 100

如何加速这个查询?

最佳答案

参见 the documentation :

CREATE INDEX ON personal USING gist
((coalesce(last_name, '') || ' ' ||
coalesce(first_name, '') || ' ' ||
coalesce(middle_name, '')) gist_trgm_ops);

这样查询可能会更快:

SELECT last_name, first_name, middle_name,
(coalesce(last_name, '') || ' ' ||
coalesce(first_name, '') || ' ' ||
coalesce(middle_name, '') <-> 'Smit') AS dist
FROM personal
ORDER BY dist LIMIT 100;

关于sql - 如何加速 pg_trgm?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49193427/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com