Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"SELECT" question

Status
Not open for further replies.

torturedmind

Programmer
Jan 31, 2002
1,052
PH
Hello Sirs/Ma'ams,

If the 1st SQL statement is faster than the 2nd one, then why do people keep using the 1st?

Statement 1
Code:
SELECT * FROM (SELECT * FROM anytable) datsub1 ORDER BY 1 DESC

Statement 2
Code:
SELECT * FROM anytable ORDER BY 1 DESC

kilroy [knight]
philippines

"Once a king, always a king. But being a knight is more than enough."
 
What makes you think that the first is faster then the second. The optimizer will simply rewrite the first to act like the second.

Bill
Lead Application Developer
New York State, USA
 
My apologies. Let me re-phrase my question - "Why is it that people use the 1st when the 2nd is much faster to write and much easier to understand?"

kilroy [knight]
philippines

"Once a king, always a king. But being a knight is more than enough."
 
Beilstwh said:
The optimizer will simply rewrite the first to act like the second.

So this means that the optimizer will still need some time to re-write rather than directly executing without re-writing. If I put it in nested query, won't it affect performance?

kilroy [knight]
philippines

"Once a king, always a king. But being a knight is more than enough."
 
Any query used is parsed by the optimizer. The reason for people using the first form is typically inexperience. The longer you program the more bad habits you (hopefully) will learn not to use. That being said there are many times when using a sub select is necessary. it is just in your example it makes no sense.

Bill
Lead Application Developer
New York State, USA
 
Beilstwh said:
it is just in your example it makes no sense.

Well, I've seen such queries in our production server in the exact format. The only difference is the table name. The table used contains hundreds of thousands of records which produces high wait and high concurrency. I already informed the devs to change such SQLs to the 2nd format and hopefully see the changes in a few days to come.

kilroy [knight]
philippines

"Once a king, always a king. But being a knight is more than enough."
 
Thank you for the time Sir. [orientalbow]

kilroy [knight]
philippines

"Once a king, always a king. But being a knight is more than enough."
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top