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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:06:27+00:00 2026-06-02T05:06:27+00:00

Which is more efficient/better practice/less costly in MySQL (with PHP)? SELECT ing two separate

  • 0

Which is more efficient/better practice/less costly in MySQL (with PHP)? SELECTing two separate statements, UNIONing them into one, or something else I found online about CREATEing a temporary table as the MySQL equivalent of SELECT INTO? Or anything else you can think of?

The result will be used in a PHP script.

Queries:

  • The first SELECT (let’s call it Query A) will always yield 10 rows, 4 columns no matter what.
  • The second SELECT (Query B) will always yield 1 row, 4 columns no matter what.

Scenarios:

  • If I SELECT the two statements, I will iterate Query B only if Query A[0]->Col1 > 0 : total of 2 db queries + PHP processing.
  • If I UNION the two statements into Query C in such a way that Query B will be the first row (Query C[0] is Query B) and Query A will be rows 2-11 (Query C[1..10] is Query A), I will only iterate rows 2-11 if Query C[0]->Col1 > 0 : total of 1 db query + PHP processing.

Non-negotiables:

  • I have to iterate over Query A no matter what.
  • Query A’s iteration depends on the value of Query B.

Is the 1 less SELECT query worth the UNION operation? Is there a more efficient/better practice/less costly solution to this? Anything I’m missing here? Thanks in advance.

  • 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-02T05:06:30+00:00Added an answer on June 2, 2026 at 5:06 am

    It totally depends on the environment and the way you are managing- I have to say- everything else. If the server is located on another network, on the other side of the planet, network is your bottleneck.

    To make a better call I would make some assumptions: the server is on localhost, the connections are managed properly, and it is just the query itself that should be optimized. If that’s the case for you, you could use Explain command the usage as explained here is very simple:

    Explain select col1 from table1 Union select col2 from table2
    

    There are tons of article out there explaining for you how to use Explain. here is one.

    One last thing, it is not just the number of rows that matter. It is also important how many times your select code is being executed too. Assume you have a loop which returns 11 rows on each run and has 1000 iterations. Just assume that the rows are different in each iteration, then you will see the effect of your connection management and caching.

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

Sidebar

Related Questions

which is more efficient (when managing over 100K records): A. Mysql SELECT * FROM
I pretty much don't know anything about these two. Which one is better/more efficient
Which is more efficient for the compiler and the best practice for checking whether
Which is more efficient? SELECT theField FROM theTable GROUP BY theField or SELECT DISTINCT
Give me some of your thoughts on which is a better coding practice/makes more
Which is better and more efficient, storing saved html page in a database or
Which is the more efficient/faster/better way to check if a key exists? if 'subject'
which is more efficient/better code. to use is object and then unbox if it
I am wondering which is more efficient, to store temporary data (related to that
In C# which is more memory efficient: Option #1 or Option #2? public void

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.