chang.advits.com



February 10, 2012, 09:22:11 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Welcome to this forum
 
Pages: [1]   Go Down
  Print  
Author Topic: Any difference in these queries?  (Read 652 times)
0 Members and 2 Guests are viewing this topic.
Chang Gee Guan
Administrator
Jr. Member
*****

Karma: 0
Offline Offline

Posts: 69



View Profile
« on: July 09, 2008, 08:14:47 PM »

Let say i have a Table (Customer)

And i had written two queries:

i. select cust_id, cust_name, cust_add, cust_age from Customer where cust_id>1000
ii.select c.cust_id, c.cust_name, c.cust_add, c.cust_age from Customer c where cust_id>1000

These two queries will give the same output, but, they have differences in terms of performance.
Which 1 will perform better? Or they are just the same?
Logged
CK
Moderator
Newbie
*****

Karma: 1
Offline Offline

Posts: 11


View Profile
« Reply #1 on: July 09, 2008, 08:16:22 PM »

surely is the (ii) o Wink becoz AOA Tongue
Logged
Chang Gee Guan
Administrator
Jr. Member
*****

Karma: 0
Offline Offline

Posts: 69



View Profile
« Reply #2 on: July 09, 2008, 08:22:18 PM »

surely is the (ii) o Wink becoz AOA Tongue

because AOA??? hahahaha....

any more concrete answer why second query will run faster than the first 1?
Logged
CK
Moderator
Newbie
*****

Karma: 1
Offline Offline

Posts: 11


View Profile
« Reply #3 on: July 10, 2008, 08:44:07 AM »

what i ting is because the sql statement had create a alias table name for customer which is "c".
so when select stament "c.cust_id" is execute, it is faster compare to normal select statement "cust_id" which actually equally to "customer.cust_id" where it will search by the long table name "customer".
 
Logged
CK
Moderator
Newbie
*****

Karma: 1
Offline Offline

Posts: 11


View Profile
« Reply #4 on: July 10, 2008, 08:46:16 AM »

(i)SELECT city FROM country WHERE city!= 'Vancouver' AND city!= 'Toronto'; GROUP BY city;
 
(ii)SELECT city FROM country GROUP BY city HAVinG city!= 'Vancouver' AND city!= 'Toronto';

so, any idea which queries is faster,hehe Wink
Logged
mysteriousdrx
Newbie
*

Karma: 0
Offline Offline

Posts: 9



View Profile
« Reply #5 on: July 12, 2008, 02:14:13 PM »

which is faster?mm...i think that depends on your processor speed..haha  Grin
but i think its the first one..
Logged
CK
Moderator
Newbie
*****

Karma: 1
Offline Offline

Posts: 11


View Profile
« Reply #6 on: July 12, 2008, 04:04:29 PM »

(i)SELECT city FROM country WHERE city!= 'Vancouver' AND city!= 'Toronto'; GROUP BY city;
 
(ii)SELECT city FROM country GROUP BY city HAVinG city!= 'Vancouver' AND city!= 'Toronto';

so, any idea which queries is faster,hehe Wink

the ans is avoid a HAVING clause in SELECT statements becoz it only filters selected rows after all the rows have been returned. Use HAVING only when summary operations applied to columns will be restricted by the clause. A WHERE clause may be more efficient.
Logged
Chang Gee Guan
Administrator
Jr. Member
*****

Karma: 0
Offline Offline

Posts: 69



View Profile
« Reply #7 on: July 13, 2008, 03:30:58 PM »

haha...
thanks for sharing these info.
hope it will help others to optimize their code.
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by SMF 1.1.6 | SMF © 2006-2008, Simple Machines LLC