If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > Question on Efficiency of Subquery

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-31-10, 16:18
ozzii ozzii is offline
Registered User
 
Join Date: Mar 2007
Posts: 194
Question on Efficiency of Subquery

I have table as follows


tbl_tickets

id - INT primary key
ticket_id VARCHAR(80)
start_time DATETIME
end_time DATETIME

I want to work out the average transaction time (end_time - start_time) only on distinct records

This is the way I would do this:

Code:
SELECT AVG(datediff(ss, end_time, start_time))
FROM( SELECT DISTINCT  ticket_id
                                  , start_time
                                  , end_time
          FROM tbl_tickets) x
If the table had a million records the subquery would pull out a large chunk of data - isnt this inefficient? Is there any other way to do this more efficiently without redesigning the database?
Reply With Quote
  #2 (permalink)  
Old 08-31-10, 17:26
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,602
Quote:
Originally Posted by ozzii View Post
If the table had a million records the subquery would pull out a large chunk of data
The number of distinct rows would depend on the data distribution, it might be large or small.

Quote:
Originally Posted by ozzii View Post
isnt this inefficient?
Not if it is the most efficent way to compute what you need.

Quote:
Originally Posted by ozzii View Post
Is there any other way to do this more efficiently without redesigning the database?
Probably, depending on what you need.

-PatP
__________________
In theory, theory and practice are identical. In practice, theory and practice are unrelated.
Reply With Quote
  #3 (permalink)  
Old 09-01-10, 10:21
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,726
What constitutes a distinct record? You have a surrogate ID, so there should not be any duplicates. What is the natural key for your table?
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
Reply With Quote
  #4 (permalink)  
Old 09-02-10, 14:18
ozzii ozzii is offline
Registered User
 
Join Date: Mar 2007
Posts: 194
Quote:
Originally Posted by blindman View Post
What constitutes a distinct record? You have a surrogate ID, so there should not be any duplicates. What is the natural key for your table?
Natural key is id but i want to work out average on the ticket_id of which there can be duplicates.
Reply With Quote
  #5 (permalink)  
Old 09-02-10, 18:25
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by ozzii View Post
... there can be duplicates.
there was another guy who has a similar problem as yours -- duplicates

see Aggregate Incorrect with INNER JOIN
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 09-02-10, 23:26
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,726
Then as Pat said, any inefficiencies are due to your database design, and the query you posted is going to be about as fast as anything else.
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On