Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 958463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:52:14+00:00 2026-05-16T00:52:14+00:00

I have 3 tables Person, Names, and Notes. Each person has multiple name and

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T00:52:15+00:00Added an answer on May 16, 2026 at 12:52 am

    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;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three tables Person (id, name, email) Address (id, Person_id, address, verified) Person_Claimed
I have three tables like this: Person table: person_id | name | dob --------------------------------
Assume I have the following tables and relationships: Person - Id (PK) - Name
I have two tables that look as Follows: Person (Table Name) Name1/Phone1/Email1/Address1/Organization1/Notes1 (Fields) Organization
I have the following database tables: table: person Columns: id, first_name, age, city, state
So here's my problem. I have three tables: Person Table person_id, name Events Table
hi i have tables like this: Persons: person_id, name then i have many langauge
I have these two tables people ============ id, name and answer_sheets ============ id, person_id,
I have a code: CREATE TABLE IF NOT EXISTS Person ( name varchar(24) ...
I have a table that has multiple rows with the following fields: PersonName SongName

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.