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 798855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:02:15+00:00 2026-05-14T23:02:15+00:00

A while ago I posted a message about optimizing a query in MySQL. I

  • 0

A while ago I posted a message about optimizing a query in MySQL. I have since ported the data and query to PostgreSQL, but now PostgreSQL has the same problem. The solution in MySQL was to force the optimizer to not optimize using STRAIGHT_JOIN. PostgreSQL offers no such option.

Update Revised

I have isolated the part of the query that fixes the problem (d.month_ref_id = 1):

select
  d.*
from
  daily d
join month_ref m on m.id = d.month_ref_id 
join year_ref y on y.id = m.year_ref_id
where
  m.category_id = '001' and
  d.month_ref_id = 1 

However, I can’t hard-code a month reference to 1. The query that produces a full table scan is:

select
  d.*
from
  daily d
join month_ref m on m.id = d.month_ref_id 
join year_ref y on y.id = m.year_ref_id
where
  m.category_id = '001'

The index on daily.month_ref_id is:

CREATE INDEX daily_month_ref_idx
  ON climate.daily
  USING btree
  (month_ref_id);

Why is the query performing a full table scan and what can be done to avoid it?

Thank you!

  • 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-14T23:02:15+00:00Added an answer on May 14, 2026 at 11:02 pm
    1. Even though it might not make much a performance difference, I would use Join clauses for join the tables instead of cross joins and the Where clause.
    2. You are calling a function in the Where clause which will cause the system to do a table scan. It will not matter what database you use, this is going to be true.
    3. Why the Left Join on City? Do you know for a fact that the given Id will exist (in this case 10663? If so, you should use an inner join.
    4. You might be able to given the compiler hints about how to formulate the query using parenthesis (I’m not sure if Postgres will honor them).
    Select  avg(d.amount) AS amount,  y.year
    From (station s
            Left Join city c -- You want to cross join on city? Why not use an Inner join?
                On c.id = 10663
                    And 6371.009 
                      * SQRT( 
                            POW(RADIANS(c.latitude_decimal - s.latitude_decimal), 2) 
                            + (
                                COS(RADIANS(c.latitude_decimal + s.latitude_decimal) / 2) 
                                * POW(RADIANS(c.longitude_decimal - s.longitude_decimal), 2)
                                )
                            ) <= 50)
        Join station_district sd
            On sd.Id = s.station_district_id
        Join year_ref y
            On y.station_district_id = sd.id
        Join month_ref m
            On m.year_ref_id = y.id
        Join daily d
            On d.month_ref_id = m.id
    Where s.elevation Between 0 And 2000 
        And y.year Between 1980 And 2000
        And m.month = 12
        And m.category_id = '001'
        And d.daily_flag_id <> 'M'
    Group By y.year

    Since you are not using the station, station_district nor city table in the results, you might be able to move those to an exists statement:

    Select  avg(d.amount) AS amount,  y.year
    From year_ref y
        Join month_ref m
            On m.year_ref_id = y.id
        Join daily d
            On d.month_ref_id = m.id
    Where y.year Between 1980 And 2000
        And m.month = 12
        And m.category_id = '001'
        And d.daily_flag_id <> 'M'
        And Exist   (
                    Select 1
                    From station s1
                        Join city c1
                            On c1.id = 10663
                    Where 6371.009 
                          * SQRT( 
                                POW(RADIANS(c1.latitude_decimal - s1.latitude_decimal), 2) 
                                + (
                                    COS(RADIANS(c1.latitude_decimal + s1.latitude_decimal) / 2) 
                                    * POW(RADIANS(c1.longitude_decimal - s1.longitude_decimal), 2)
                                    )
                                ) <= 50
                        And S1.station_district_id = y.station_district_id
                    )
    Group By y.year
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have posted this question a while ago but got a partial answer to
A while ago, i posted a question about a parent div not reacting to
i have posted a few hours ago question about newtons method,i got answers and
I posted a similar question a while ago but haven't received any answers so
A while ago, I was writing some code, but accidentally deleted some good code
A while ago I did some work in Qt for C++; now I'm working
This is an expansion of a question I posted a while ago: Fitting a
Somebody has posted an hour ago or so a question that was about the
Not long ago I posted a question about a design decision of my F1
A while ago I asked a question about square detection and karlphillip came up

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.