views:

277

answers:

2

Hello everyone,

I am using SQL Server 2008 and I have two tables which are of the same schema and I create a view which union the content of the two tables to provide a single view of "table" to external access.

One of the table is read only and the other table contains bulk insert/delete operation (on the other table, I will use bulk insert at some interval to insert everal thousand of rows and run another SQL Job to remove several Million rows daily).

My question is, if the other table is under bulk insert/delete operation, will the physical table be locked so that the access from external user to the union view of two tables are also blocked? (I am thinking of whether lock escalation applies in this scenario, row locks finally lock the table, which finally locks the access of the view?)

thanks in advance, George

+2  A: 

if the other table is under bulk insert/delete operation, will the physical table be locked so that the access from external user to the union view of two tables are also blocked?

Yes, with the caveat that, if the optimiser can find a way to execute the query that does not involve accessing the bulk insert table then access will not be blocked.

If you are looking to optimise bulk loading times make sure you have a read of this blog post.

EDIT

What is the actual problem you are experiencing? Do you really need to be using this view everywhere (for example are there places that only need data from one table, that are querying it via the view?)

If you want you view to be "online" all the time consider either snapshot isolation, or if you are loading up full sets into the bulk table (eg. full content is replaced daily), you can load the data into a separate table and sp_rename the table in (in a transaction)

Sam Saffron
Sorry my wording is confusing. I mean I have two operations the other table (1) bulk insert (2) delete millions of rows daily (not bulk delete and bulk insert). I think even if for (1) bulk insert query optimizer could find ways to optimize, for (2) delete operation, table will be locked (lock escalation occurs?) and query to the view will be locked?
George2
@sambo99, a further quesiton, I read the document you referred carefully, I am confused about what are the differences between heap table and table with clustered index?
George2
Yes, I need the view online all the time. "snapshot isolation" you mean nolock solution which reads dirty data?
George2
"or if you are loading up full sets into the bulk table (eg. full content is replaced daily), you can load the data into a separate table and sp_rename the table in (in a transaction)" -- not feasible since I will do bulk insert for new thousands of rows every 20 minutes.
George2
I think you need to post some separate questions about nolock and snapshot isolation.
Sam Saffron
+2  A: 

Mostly likely yes. It depends on lock escalation

To work around (not all options):

  • Use the WITH (NOLOCK) table hint to ignore and don't set any locks. If used on the view it also applies to both tables

  • Use WITH (READPAST) if you don't mind skipping locked rows in the BCP table

  • Change the lock granularity for the BCP table. Use sp_tableoption and set "table lock on bulk load" = false.

Edit: Now I've had coffee...

If you need to query the bulk table during load/delete operations and get accurate results and not suffer performance hits, I suggest you need to consider SNAPSHOT isolation

Edit 2: SNAPSHOT isolation

gbn
PLEASE for the love of all that is good lets not be suggesting with (nolock) as a cure for all your locking ailments.
Sam Saffron
Used correctly and with full understanding, it's OK.
gbn
@gbn, 1. I am confused about the nolock option. Will nolock option raise any risks for data integrity during concurrent operation (query/insert/delete)? Appreciate if you could recommend me some reading about how nolock works and in what scenario should we use nolock option? 2. I think using "READPAST" will not response accurate for query result? For example, there may be hit rows for queries in BCP operation, but if we use READPAST operation to bypass, client will not get anything. Any comments?
George2
NOLOCK will not affect loading of data. It does not use shared locks and it ignores other locks However, you have a risk of reading dirty data, data that is being deleted, data that has to be rolled back etc.READPAST will give inaccurate results during a load
gbn
Thanks @gbn, 1. I am confused. You mean using nolock on query client, and not using nolock in insert/delete process? 2. Will READPAST read dirty data (e.g. data not committed yet)?
George2
@sambo99, why you do not like nolock?
George2
1. Only for reading data 2. No, it will ignore anything being changed (has a lock)
gbn
@gbn, you provided 3 solutions above, and for "SNAPSHOT isolation" you are referring to which one?
George2
http://msdn.microsoft.com/en-us/library/ms189122(SQL.90).aspx
gbn