INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

"SELECT" question

"SELECT" question

(OP)
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."

RE: "SELECT" question

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

RE: "SELECT" question

(OP)
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."

RE: "SELECT" question

(OP)

Quote (Beilstwh)

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."

RE: "SELECT" question

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

RE: "SELECT" question

(OP)

Quote (Beilstwh)

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."

RE: "SELECT" question

Quote:

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?"

That's a question you're going to have to ask those people, not us.

-- Chris Hunt
Webmaster & Tragedian
Extra Connections Ltd

RE: "SELECT" question

(OP)
Thank you for the time Sir. orientalbow

kilroy knight
philippines

"Once a king, always a king. But being a knight is more than enough."

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close