Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I am experiencing slow query performance in my PostgreSQL database #14

Open
ERJavier opened this issue Oct 3, 2023 · 1 comment
Open

Comments

@ERJavier
Copy link

ERJavier commented Oct 3, 2023

Even after optimizing my Python code and database configuration. Queries are taking over 2 minutes and 30 seconds to execute.

I am running the following SQL queries:

  • Query 1: SELECT data FROM authors WHERE data ->> 'name' ILIKE %s
  • Extract the author key from the result of Query 1.
  • Use the author key in Query 2: SELECT * FROM works WHERE data-> 'authors' @> '[{{\"author\": {{\"key\": \"{author_key}\"}}}}]'

Environment Details

  • PostgreSQL Version: [14]
  • Operating System: [macOS]
  • Hardware: [M2, 24GB RAM]

I have already tried the following:

  • Optimizing Python code
  • Adjusting PostgreSQL configurations (shared_buffers, work_mem, etc.)
  • Adding appropriate indexes

I expected both queries to execute much faster, ideally within a few seconds.

Thank you 🙏

@DaveBathnes
Copy link
Member

DaveBathnes commented Feb 7, 2024

Hi

Thanks for raising this issue! Apologies for the extremely late reply, I've been slowly working on a refinement to the loading process, and it's been exceptionally long in getting time, plus waiting for the database to build and then try again, etc!

You're right that the database isn't optimised. Originally it was designed to take an existing identifier (mainly ISBN), and good text searching hasn't been done.

Trying your own steps, I get:

SELECT data FROM authors WHERE data ->> 'name' ILIKE 'Graham Greene'

~ 15 seconds

On the second example you don't need to query the JSON data within the works table because the author key has been linked as an indexed identifier through the author_works table. For example, using a key that I obtained above:

select *
from works w
join author_works aw
on w.key = aw.work_key
where author_key = '/authors/OL6856462A'

~0.3 seconds

If I can get full text searching on some key fields (like author name), the abive steps should all be able to be done in one single query. I'll create a branch for that under this issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

When branches are created from issues, their pull requests are automatically linked.

2 participants