Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Database Server Software > Sybase > Strage sp parameter behaviour

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-06-04, 06:43
mwlees mwlees is offline
Registered User
 
Join Date: Oct 2004
Posts: 2
Strage sp parameter behaviour

I have a table, 'ct' which has around 4 million rows in it.

I have a clustered index on one of the date columns, 'ct_date', inside this table.

I have a stored procedure I use to delete 'ct' entries from a particular date or todays date if non supplied.

Here is the sp.

Code:
create proc del_ct @from_date datetime = '1/1/1900' as begin declare @error int, @deleted int if @from_date = '1/1/1900' begin select @from_date = convert( char( 10 ), getdate(), 101 ) end begin transaction delete from ct where ct_date > @from_date select @error = @@error, @deleted = @@rowcount if @error = 0 begin commit transaction print "%1! trades deleted", @deleted end else begin print "Error %1!", @error rollback transaction end end go


For some reason the clustered index is not chosen when I examine the query plan. This is borne out by the fact that this sp takes ages to run, i.e. it is table scanning 4 million rows....

I modified it to this
Code:
create proc del_ct @from_date datetime = '1/1/1900' as begin declare @error int, @deleted int, @actual_date datetime if @from_date = '1/1/1900' begin select @actual_date = convert( char( 10 ), getdate(), 101 ) end else begin select @actual_date = @from_date end begin transaction delete from ct where created_date > @actual_date select @error = @@error, @deleted = @@rowcount if @error = 0 begin commit transaction print "%1! trades deleted", @deleted end else begin print "Error %1!", @error rollback transaction end end go

And the clustered index is now being used...

Can anyone shed any light on this. It appears that the use of parameters, or the possible change of a parameter, precludes use of an index.

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

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