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 > table scan using getdate()

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-15-11, 12:49
NeoMonk NeoMonk is offline
Registered User
 
Join Date: Nov 2011
Posts: 2
table scan using getdate()

I'm working on the following query on Sybase ASE 12.5.4:

select distinct t2.pay_method_number "Cnumber",
t1.order_number,
t4.ship_company_name,
t2.method_of_payment,
t5.amount 'GC Amt$'

from order_header t1, payment_header t2, shipping_detail t3, shipping_header t4, payment_detail t5

where t1.order_number = t2.order_number
and t1.order_number = t4.order_number
and t2.order_number=t5.order_number
and t2.payment_number=t5.payment_number
and t3.date_shipped >= dateadd(dd,-1,"11/15/11")
and t3.date_shipped < dateadd(dd,0,"11/15/11")
and t1.order_number=t3.order_number
and t1.source_key = "RNWHUMC"
and t1.order_status not in ("H", "SH", "DE")
and t2.method_of_payment = "GC"
and t1.customer_number != XXXXXX

when in this form it returns the proper record in 1 sec.

if I change the following from:
and t3.date_shipped >= dateadd(dd,-1,"11/15/11")

and make it:
and t3.date_shipped >= dateadd(dd,-1,getdate())

the query never returns. Show plan using the "11/15/11" show indexes being used on all the tables. When I change the date to getdate() the plan shows a table scan on a very large table. And it's not the t3 table, but the t1 table.

Any insight is greatly appreciated.
Eric
Reply With Quote
  #2 (permalink)  
Old 11-17-11, 09:33
pdreyer pdreyer is offline
Registered User
 
Join Date: May 2005
Location: South Africa
Posts: 1,268
When last did you update statistics?
Try running "update index statistics" on the tables

PS. Your query might not work as expected as getdate() include time, I suggest truncate the time with
dateadd(dd, datediff(dd, '' ,getdate()), '')
dateadd(dd, datediff(dd, '' ,getdate())-1, '')
Reply With Quote
  #3 (permalink)  
Old 11-17-11, 16:28
NeoMonk NeoMonk is offline
Registered User
 
Join Date: Nov 2011
Posts: 2
table scan using getdate()

Thanks for the information. I did find out that running it on 12.5.4 using a function or variable is most likely to get a table scan.

I ran the query using getdate() on 15.5 (test server) and it ran very fast, and did not perform the scan.

I will try the update index stats since I have to get this into production on 12.5.4.

Thanks again,
eric
Reply With Quote
  #4 (permalink)  
Old 11-18-11, 11:52
pdreyer pdreyer is offline
Registered User
 
Join Date: May 2005
Location: South Africa
Posts: 1,268
Using a function should not be a problem as long as it is not on the indexed column
i.e. this is OK
Code:
SELECT mydate
FROM mytable 
WHERE mydate=dateadd(dd,datediff(dd,'',getdate())-1,'')
But this can't use an index
Code:
SELECT mydate
FROM mytable 
WHERE dateadd(dd,datediff(dd,'',mydate)+1,'')='20111118'
I still suggest you update index statistics on ALL tables involved.
As a 2nd option try recreating the indexes or reorg rebuild the indexes.
Reply With Quote
Reply

Tags
getdate, show plan, table scan

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