i have a table called ORDERQUEUE laid out like this
create table [ORDERQUEUE] (oq_id int identity primary key, customer_id
int not null, grouping_id int null, queue_date datetime not null
default getdate().... other columns here)
clustered index is grouping_id/queue_date
the primary key is not clustered
the grouping_id is used group together related rows for processing. a
stored procedure gets the next sequence id and updates those records
for a customer where the grouping_id = null.
if another process comes along and runs the same procedure i don't want
it to update the same rows for a customer with a different grouping id.
I use set rowcount to keep the number of updates low. if the next batch
gets a different grouping id then that is ok. i order by the queue date
to get the early rows first.
i added a serializable hint to keep readers from getting the rows after
the insert into the #queue table.
/* order by the queue_date so we don't process the same rows next time
*/
set rowcount 10000
insert into #queue (oq_id, customer_id, grouping_id, queue_date)
select oq_id, customer_id, grouping_id, queue_date from order_queue
(serializable)
where customer_id = @customer_id and grouping_id is null
order by queue_date
set rowcount 0
update q
set grouping_id = @new_grouping_id
from order_queue oq inner join #queue q on q.oq_id = oq.oq_id
commit
by setting serializable on the table during the select i can hold onto
the rows while they are
being updated and then release them. i expect another execution of the
stored procedure for that customer will block but execution for any
other customer will go through.
also, since the grouping_id is part of the clustered index but can be
null can the optimizer still use it?
any opinions or suggestions?
thanks
derek foley
>> Stay informed about: need help with table design - opinions please