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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:59:12+00:00 2026-05-25T20:59:12+00:00

I have a query formed by an UNION ALL from two tables. The results

  • 0

I have a query formed by an UNION ALL from two tables. The results have to be ordered and paginated (like the typical list of a web application).

The original query (simplified) is:

SELECT name, id
FROM _test1 -- conditions WHERE
UNION ALL
SELECT name, id
FROM _test2 -- conditions WHERE
ORDER BY name DESC LIMIT 10,20

The problem is that the 2 tables have more than 1 million rows each, and the query is very slow.

How can I get an optimized paginated list from a UNION ALL?

Postdata:

I’ve used the search of Stack Overflow and I’ve found some similar questions of this, but the answer was incorrect or the question isn’t exactly the same. Two examples:

Optimize a UNION mysql query

Combining UNION and LIMIT operations in MySQL query

I’m surprised that in Stack Overflow nobody could answered this question. Maybe it is impossible to do this query more efficiently? What could be a solution of this problem?

  • 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-25T20:59:13+00:00Added an answer on May 25, 2026 at 8:59 pm

    I would think that you could use something similar to the solution in your second link to at least help performance, but I doubt that you’ll be able to get great performance on later pages. For example:

    (   SELECT name, id
        FROM _test1 -- conditions WHERE
        ORDER BY name DESC LIMIT 0, 30
    )
    UNION ALL
    (   SELECT name, id
        FROM _test2 -- conditions WHERE
       ORDER BY name DESC LIMIT 0, 30
    )
    ORDER BY name DESC
    LIMIT 10, 20
    

    You’re basically limiting each subquery to the subset of possible rows that might be on the given page. In this way you only need to retrieve and merge 20 rows from each table before determining which 10 to return. Otherwise the server will potentially grab all of the rows from each table, order and merge them, then start trying to find the correct rows.

    I don’t use MySQL a lot though, so I can’t guarantee that the engine will behave how I think it should 🙂

    In any event, once you get to later pages you’re still going to be merging larger and larger datasets. HOWEVER, I am of the strong opinion that a UI should NEVER allow a user to retrieve a set of records that let them go to (for example) page 5000. That’s simply too much data for a human mind to find useful all at once and should require further filtering. Maybe let them see the first 100 pages (or some other number), but otherwise they have to constrain the results better. Just my opinion though.

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

Sidebar

Related Questions

I have formed few temp tables in my query (TSQL) like - #temphold1, #temphold2,
I have query like this : SELECT EXTRACT(MONTH FROM d.mydate) AS synmonth, SUM(apcp) AS
I have formed this query to produce the pitcher from each team who has
I have formed the following query in derby to compute all baseball player's slugging
Okay, basically, I have formed a mySQL query which returns a data set all
I have query with two MtM relations: $em = $this->getEntityManager(); $qb = $em->createQueryBuilder(); $qb
I have this query: (from r in gServiceContext.CreateQuery(opportunity) join c in gServiceContext.CreateQuery(contact) on ((EntityReference)r[new_contact]).Id
I have a query with about 6-7 joined tables and a FREETEXT() predicate on
I have a query that returns a date, and I'd like to format it
Let's say I have two existing tables, dogs and cats: dog_name | owner ---------+------

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.