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

  • SEARCH
  • Home
  • 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 6783967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:56:02+00:00 2026-05-26T16:56:02+00:00

I have a query that runs a lot slower (~5 minutes) when I run

  • 0

I have a query that runs a lot slower (~5 minutes) when I run it with the default enable_nestloop=true and enable_nestloop=false (~10 secs).

Explain analyse result for both cases:

Machine A nestloop=true – http://explain.depesz.com/s/nkj0 (~5 minutes)
Machine A nestloop=false – http://explain.depesz.com/s/wBM (~10 secs)

On a different slightly slower machine, copying the database over and leaving the default enable_nestloop=true it takes ~20 secs.

Machine B nestloop=true – (~ 20secs)

For all the cases above I ensured that I did an ANALYZE before running the queries. There were no other queries running in parallel.

Both machines are running Postgres 8.4. Machine A is running Ubuntu 10.04 32 bit while Machine B is running Ubuntu 8.04 32 bit.

The actual query is available here . It is a reporting query with many joins as the database is mainly used for transaction processing.

  1. Without resorting to putting in something like materialized views what can I do to make the planner do what I achieved by setting enable_nestloop=false ?

  2. From the research I have done it seems to be that the reason the planner is choosing the seemingly unoptimal query is because of the huge difference between the estimated and actual rows. How can I get this figure closer ?

  3. If I should rewrite the query, what should I change ?

  4. Why is it that the planner seems to be doing the right thing for Machine B. What should I be comparing in both the machines ?

  • 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-26T16:56:03+00:00Added an answer on May 26, 2026 at 4:56 pm

    Turns out rewriting the query was the best fix. The query was written in a way that it relied heavily on left joins and had many joins. I flattened it out and reduced the left joins by using my knowledge of the join nature of the data in the tables the query was joining. I guess the rule of thumb is if the planner is coming out with real crappy estimates, there might be a better way of writing the query.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySQL query that runs via cron every 30 minutes to delete
I have a query that runs in about 2-4 minutes on production but runs
I have the following query that runs in my Oracle database and I want
I have a quite complex multi-join TSQL SELECT query that runs for about 8
For example I have a mysql query that gets some data. Then runs another
I have a Delphi 2009 application that runs a query over a database using
I have a php script that runs a mysql query, then loops the result,
I am using in C# MYsql .I have query that works if I run
I have a query that runs based on the end date picked. Essentially the
I have a SQL query that runs on a SQL 2008 R2 server. It

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.