I've table,
example as
Id int primary key(clustered index)
Name varchar(255)not null (non-clustered index)
..
..
Other columns
I execute stored procedur as
update table1 set table1.Name=isnull(@Name,table1.Name) ,( updating other columns) where Id=@Id;
many times Name equals @Name;
In execution plan i saw
if i change stored procedure as
update table1 set ( updating other columns) where Id=@Id;
i saw Cost:15%
Question:
I need to change this procuder for two situation when
- table1.Name= @Name
- table1.Name <> Name.
In first case table1.Name doesn't change value or index doesn't update and clustered index update cost 15%;
in second case table1.Name changes value and clustered index update cost 50%.
Why index is updating when update a equal value?
i can't select "Name" and compare befor updatings
I know that create varchar column index isn't good idea
Update
update query with change Name column(execution CPU time 200ms)
update query without change Name column(execution CPU time 70ms)
Update
maybe anybody can help with trigger if it can help with this issue?
Best Answer
how many record will updated at one go and how frequently ?
Because optmizer is not smart enough to detect if you are updating same value.
It is good that index is rebuild of its own,though it is time consuming.
If you disable index then you have to rebuild it later.
your situation is very serious ,
1.one row, 30 000 per minute
Hope you have handle the requirement correctly.As by definition of index or disadvantage of index is that it slow down update statement if that table is so frequently updated.
Now you have to answer how frequently Name is use in predicate and how.is index helping you there.Depending upon all this it is better to drop index on Name.
Don't keep any non clustered index in these column which are use in this proc.
since
@name 10% is null
you need dynamic sql whether you drop index or not. it will improve your query on 90% occasion.sp_executeSql example (just an example)