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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:05:31+00:00 2026-06-10T10:05:31+00:00

I have a big SQL query (for MySQL) that is slow. It’s a union

  • 0

I have a big SQL query (for MySQL) that is slow. It’s a union of two select statements. I have tried different things, but any slight variance gives me a different result set from the original. Any help with improving it will be greatly appreciated. Thanks. Here is the SQL:

   (SELECT  
            CONCAT(city_name,', ',region) value, 
            latitude,
            longitude,
            id,
            population,
    ( 3959 * acos( cos( radians($latitude) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians($longitude) ) + sin( radians($latitude) ) * sin( radians( latitude ) ) ) ) 
    AS distance,
    CASE region
    WHEN '$region' THEN 1
    ELSE 0
    END AS region_match
    FROM `cities` 
    $where and foo_count > 5
    ORDER BY region_match desc, foo_count desc
    limit 0, 11)
    UNION
    (SELECT   
            CONCAT(city_name,', ',region) value, 
            latitude,
            longitude,
            id,
            population,
    ( 3959 * acos( cos( radians($latitude) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians($longitude) ) + sin( radians($latitude) ) * sin( radians( latitude ) ) ) ) 
    AS distance,
    CASE region
    WHEN '$region' THEN 1
    ELSE 0
    END AS region_match
    FROM `cities` 
    $where
    ORDER BY region_match desc, population desc, distance asc
    limit 0, 11)
    limit  0, 11

The SQL does take some interpolated values (prefixed with the dollar sign($)).

  • 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-06-10T10:05:33+00:00Added an answer on June 10, 2026 at 10:05 am

    The following might give the same result (I’m not sure about how the maximum/minimum functions are called in SQL, but you should get an idea — you need two fields derived from foo_count which separate the items of the first part of your UNION from those of the second one and allow ordering within the first part without disturbing the order in the second part) — of course, you later need a second query to throw the additional fields out again:

    SELECT   
                CONCAT(city_name,', ',region) value,  
                latitude, 
                longitude, 
                id, 
                population, 
        ( 3959 * acos( cos( radians($latitude) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians($longitude) ) + sin( radians($latitude) ) * sin( radians( latitude ) ) ) )  
        AS distance,
        min ( 6, max (foo_count, 5)) AS group_discriminator,
        max ( 6, foo_count) AS rank_for_use_in_first_group,
        CASE region 
        WHEN '$region' THEN 1 
        ELSE 0 
        END AS region_match 
        FROM `cities`  
        $where
        ORDER BY group_discriminator desc, region_match desc, rank_for_use_in_first_group desc, population desc, distance asc
        limit 0, 11
    

    EDIT: Improvements

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

Sidebar

Related Questions

I have a big giant sql query where I select and format a bunch
I have a select SQL query which is really big and it should be
I have been running a mammoth SQL query that is as so: select SessionID,
I have SQL Server 2008 Ent and OLTP database with two big tables. How
I have a website that is running a query off to Mysql when a
I have a very big SQL query with the following structure in a Java
I have an application where I create a big SQL query dynamically for SQL
I have the following SQL query: SELECT Phrases.* FROM Phrases WHERE (((Phrases.phrase) Like *ing
I have an sql query with inner joins of four tables that takes more
In SQL Server 2005, I have a query that involves a bunch of large-ish

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.