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 > Data Access, Manipulation & Batch Languages > ANSI SQL > Need to Optimize the SQl Query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-29-04, 05:12
arunprasadlv arunprasadlv is offline
Registered User
 
Join Date: Mar 2004
Location: India
Posts: 41
Need to Optimize the SQl Query

Hi Everyb,
I need to optimize the below query.the following query returns only 632 out 4460000 rows.

Datatypes:

Region varchar
Status int
Server varchar
Directory varchar
PercentUsed decimal
AvailableSpace int
scantime datetime



select Max(status),server,max(scantime)
from Tbl_LotusServerDiskSpaceReport b
where
scantime=(select max(scantime)
from Tbl_LotusServerDiskSpaceReport a
where
a.server=b.server
and
a.percentused is not null
and
a.AvailableSpace is not null)
and b.percentused is not null
and b.AvailableSpace
is not null
group by server order by server


Thanks in Advance ,
Arun
Reply With Quote
  #2 (permalink)  
Old 09-29-04, 07:17
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
First, the max on scantime in line 1 is not required, because you are only getting one scantime value per server, thanks to the subquery. So your query becomes:
Code:
select Max(status),server,scantime
from Tbl_LotusServerDiskSpaceReport b
where scantime=
(select max(scantime)
 from Tbl_LotusServerDiskSpaceReport a
 where a.server=b.server
 and a.percentused is not null
 and a.AvailableSpace is not null)
and b.percentused is not null
and b.AvailableSpace is not null
group by server,scantime
order by server
Now, if (server, scantime) form a unque key for the table then you don't need the max and group by at all:
Code:
select status,server,scantime
from Tbl_LotusServerDiskSpaceReport b
where scantime=
(select max(scantime)
 from Tbl_LotusServerDiskSpaceReport a
 where a.server=b.server
 and a.percentused is not null
 and a.AvailableSpace is not null)
and b.percentused is not null
and b.AvailableSpace is not null
order by server
I assume there is an index on the server column at least?
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
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