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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:25:58+00:00 2026-05-20T00:25:58+00:00

Say I have a sequence of queries that progressively get more expensive. I can

  • 0

Say I have a sequence of queries that progressively get more expensive. I can join all these with a union but I’m only interested in the top 10 results so I add a LIMIT. The query looks something like this:

(SELECT col1, col2 FROM table WHERE colx = 'x')
UNION
(SELECT col1, col2 FROM table WHERE colx LIKE '%x%')
UNION
(SELECT col1, col2 FROM table WHERE colx LIKE '%x%' AND unindexedcol LIKE '%y%')
LIMIT 10

I know that this is not guaranteed to be the top 10 from the first query as the MySQL documentation states

UNION by default produces an unordered
set of rows

but in practice it appears that the results are ordered select 1 followed by select 2 etc until the limit is reached.

So does MySQL execute the queries sequentially and stop when it hits the limit? If not then what is a better way to execute this style of query where only a subset of results are needed and prioritisation of less expensive queries is preferred to minimize execution time?

  • 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-20T00:25:59+00:00Added an answer on May 20, 2026 at 12:25 am

    I have made a test to demonstrate MySQL does not optimize that kind of query:
    First of all I execute this query:

    select @a1 := 0;
    select @a2 := 0;
    
    (SELECT id, @a1:=@a1+1 FROM companies)
    UNION
    (SELECT id, @a2:=@a2+1 FROM invoices)
    LIMIT 10;
    

    Then I watch the values of @a1 and @a2 variables:

    select @a1 as a1, @a2 as a2;
    

    In my tests they are a1 = 81 and a2 = 467.
    So the single selects of union query are not limited.

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

Sidebar

Related Questions

Lets say I have a sequence of 8 items, these loop such that after
Lets say I have a database that looks like this: tblA: ID, Name, Sequence,
Say I have a SSI script that uses exec, or a PHP script that
Say I have a factory method that churns out instances of type T, and
Let's say that I have a list of data: {1, 2, 3, 4, 5,
Let's say I have a sequence. IEnumerable<int> sequence = GetSequenceFromExpensiveSource(); // sequence now contains:
Say I have 2 interfaces, which follow the same sequence of function calls. The
Say i have a function that takes to coordinates, x and y. For x
Let's say I have an extremely large sequence of characters of A-D, 4 Billion
If I have a sequence as follows (let's say it's an IEnumerable<T> ): [A,

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.