开发者

what is the difference between multiple select and single select Query

开发者 https://www.devze.com 2023-03-02 16:54 出处:网络
I like to know difference between multiple and single select Query single Query SELECT name,age,country FROM user

I like to know difference between multiple and single select Query

single Query

SELECT name,age,country FROM user 

Multiple Query

SELECT name FROM user 
SELECT age FROM user 
SELECT country FROM user 

I am having functions to get values for each column in user table... That why this q开发者_如何学运维uestion arises.. This may be silly but i like to know


In the first case you'll go to the grocery 1 time and back to home with a big bag, in the second case you'll go to the grocery 3 times and back to home with a small bag. If your are late you should prefere the first way...


The second query is pointless. It would be more worthwhile explaining the difference if it's a UNION join:

(SELECT name FROM user) UNION
(SELECT age FROM user) UNION
(SELECT country FROM user)

Difference? It takes longer for no real gain or reason. Pointless again.

SELECT name, age, country FROM user

This is ineffective as well. You should only select data you want, this selects for ALL users -- bad bad bad.


A better way to optimize a query

SELECT name, age, country FROM user WHERE username IN ('gary', 'john')

Select only the data you want. Don't:

  • don't: SELECT *
  • don't: select all record
  • don't: do multiple selects that are pointless.

DO: think logically about your data, what information you really want. Always use the EXPLAIN query to get a real idea of the performance of each query.


In the first example you get all the data you need in a single query, whereas in the second you'll have to do three queries (this is bad) and get three different datasets.


Exactly what it looks like.

With a single query you get everything in one go.

With multiple queries you get one thing. Then you ask for another thing and get that. Then you ask for another thing and get that.

i.e. asking three separate times is slow and wasteful and will, when computers get sentience, result in unhappy robots beating you up.


Why you'd want to run three queries with statically defined column headers, instead of picking out:

SELECT name, age, country FROM user

Is beyond me!

Unions are useful if you have two tables with the same table structure, but definitely not needed for this case.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号