Postgresql 在自定义函数、php 上速度较慢,但​​如果使用带有 gin 索引的文本搜索直接在 psql 上输入则速度很快

发布于 2024-09-11 09:45:59 字数 1158 浏览 1 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绮烟 2024-09-18 09:45:59

数据库必须在了解有关参数之前准备内部查询。这可能会导致错误的查询计划。要避免函数中出现此问题,请使用 plpgsql 语言并在函数内使用 EXECUTE:

CREATE OR REPLACE FUNCTION queryNameFunc (TEXT) RETURNS TABLE(id INT, name TEXT) AS $
BEGIN
    RETURN QUERY EXECUTE '
        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)' USING $1;
END;
$ LANGUAGE plpgsql;

这适用于版本 8.4,并且您必须安装 plpgsql:

CREATE LANGUAGE 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:

CREATE OR REPLACE FUNCTION queryNameFunc (TEXT) RETURNS TABLE(id INT, name TEXT) AS $
BEGIN
    RETURN QUERY EXECUTE '
        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)' USING $1;
END;
$ LANGUAGE plpgsql;

This works in version 8.4 and you do have to install plpgsql:

CREATE LANGUAGE plpgsql;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文