Postgresql 在自定义函数、php 上速度较慢,但如果使用带有 gin 索引的文本搜索直接在 psql 上输入则速度很快
我有 3 个表“人员”、“姓名”和“注释”。每个人都有多个名字并有可选的注释。我对名称和注释的某些列进行了全文搜索(见下文),如果我搜索的单词在结果集中或在数据库中,它们就可以完美工作,这适用于自定义函数、php 和 psql。现在的问题是,当我搜索的单词不存在于数据库中时,查询在 php 和自定义函数中变得超级慢,但在 psql 上仍然很快。在psql上不到1s,其他的都在10s以上。
表格:
Person | id, birthday
Name | person_id, name, fs_name
Notes | person_id, note, fs_note
除了PK和FK索引之外,fs_name和fs_note上还有Gin索引。
函数/查询
create or replace function queryNameFunc (TEXT)
returns TABLE(id int, name TEXT) as $$
select id, name
from person_name pnr
inner join person pr on (pnr.person_id=pr.id)
left join personal_notes psr on (psr.person_id = pr.id)
where pr.id in
(select distinct(id)
from person_name pn
inner join person p on (p.id = pn.person_id)
left join personal_notes ps on (ps.person_id = p.id)
where tname @@ to_tsquery($1)
limit 20);
$$ language SQL;
这里的 where 条件被精简,例如,如果我执行 'john & james' 在 $1 上并且数据在数据库中,那么结果很快,但如果 'john 和 james' 不在数据库中,那么结果很慢。这变得更慢,因为我有 1M 个人记录和 3M+ 姓名记录(所有虚拟记录)。知道如何解决这个问题吗?我尝试重新启动服务器,重新启动 postgresql。
I have 3 tables Person, Names, and Notes. Each person has multiple name and has optional notes. I have full text search on some columns on names and notes (see below), they are working perfectly if the word I search with is in the result set or is in the db, this is for custom function, php, and psql. The problem now is that when the word I search is not present in the db the query gets super slow in php and custom function but still fast on psql. On psql it's less than 1s, others are more than 10s.
Tables:
Person | id, birthday
Name | person_id, name, fs_name
Notes | person_id, note, fs_note
Beside PK and FK index, Gin index on fs_name and fs_note.
Function/Query
create or replace function queryNameFunc (TEXT)
returns TABLE(id int, name TEXT) as $
select id, name
from person_name pnr
inner join person pr on (pnr.person_id=pr.id)
left join personal_notes psr on (psr.person_id = pr.id)
where pr.id in
(select distinct(id)
from person_name pn
inner join person p on (p.id = pn.person_id)
left join personal_notes ps on (ps.person_id = p.id)
where tname @@ to_tsquery($1)
limit 20);
$ language SQL;
The where condition is trimmed down in here, so for example if I do 'john & james' on $1 and the data is on the db then results is fast but if 'john and james' are not in db then its slow. This got slower as I have 1M records on person and 3M+ on names (all dummy records). Any idea on how to fix this? I tried restarting the server, restarting postgresql.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
数据库必须在了解有关参数之前准备内部查询。这可能会导致错误的查询计划。要避免函数中出现此问题,请使用 plpgsql 语言并在函数内使用 EXECUTE:
这适用于版本 8.4,并且您必须安装 plpgsql:
The database has to preprare the inner query before it has any knowledge about the parameter. This might result in a bad queryplan. To avoid this problem in a function, use the plpgsql-language and use EXECUTE inside the function:
This works in version 8.4 and you do have to install plpgsql: