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 > Sybase > help to tune this query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-08-10, 18:50
hugo_sots hugo_sots is offline
Registered User
 
Join Date: Dec 2010
Posts: 2
help to tune this query

Hello all,

Could someone please help me to tune this query:

DELETE FROM SomeTable where datediff(day,dateTime, getutcdate()) >30 and someField = 'xxxx'

It seems this query requires table scan. It's no good. Maybe the solution is to create indexes.

What's the ideal query?

Thank you.
Reply With Quote
  #2 (permalink)  
Old 12-09-10, 01:34
agrawal.meet agrawal.meet is offline
Registered User
 
Join Date: Jun 2010
Posts: 51
How many rows does the table have? If there are not many rows (only ~5000) then sybase may not choose to use index at all.

Do you have index on the column:
Code:
and someField = 'xxxx'
If not then create composite clustered index on this and datetime field.

Thanks,
Meet
__________________
Please always reply to the post if it was helpful. Others may find it helpful.
Reply With Quote
  #3 (permalink)  
Old 12-09-10, 02:56
pdreyer pdreyer is offline
Registered User
 
Join Date: May 2005
Location: South Africa
Posts: 1,268
Avoid WHERE clauses that are non-sargable
If you use a function on a column then any index defined on that column can't be used.
A better way is
where dateTime < dateadd(day, -30, getutcdate())

And it also depends on the percentage of qualifying rows
i.e. if the majority of rows qualify for
and someField = 'xxxx'
then an index on someField will not be of much use
Reply With Quote
  #4 (permalink)  
Old 12-15-10, 08:58
hugo_sots hugo_sots is offline
Registered User
 
Join Date: Dec 2010
Posts: 2
Thanks for your replies and sorry for the late response!

Could someone please help me to check why these two queries return different values?

select count(*) FROM WebUsageStatsTb where datediff(day,dateTime, getutcdate()) >30 and website = 'xxx'

select count(*) FROM WebUsageStatsTb where dateTime < dateadd(day, -30, getutcdate()) and website = 'xxx'

Actually I have to retire this query:

DELETE FROM WebUsageStatsTb where datediff(day,dateTime, getutcdate()) >30 and website = 'xxx'

and use a query that contains dateadd() instead of datediff(), in order to have a better performance.

Thank you.

Hugo.
Reply With Quote
  #5 (permalink)  
Old 01-10-11, 09:33
pdreyer pdreyer is offline
Registered User
 
Join Date: May 2005
Location: South Africa
Posts: 1,268
You need to truncate time from getutcdate() i.e.
Code:
...
where dateTime<dateadd(dd,datediff(dd,'',getutcdate())-30,'')
...
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