English 中文(简体)
TSQL Snapshot Isolation
原标题:

Using SQL2k5, I have a staging table that contains columns that will populate numerous other tables. For instance, a statement like this:

 INSERT INTO [appTable1] ([colA], [colB])
 SELECT [appTable1_colA], [appTable1_colB]
 FROM [stageTable]

A trigger on [appTable1] will then populate the identity column values of the newly inserted rows back into [stageTable]; for this example, we ll say it s [stageTable].[appTable1_ID] which are then inserted into other tables as a FK. More similar statements follow like:

 INSERT INTO [appTable2] ([colA], [colB], [colC], [appTable1_FK])
 SELECT [appTable2_colA], [appTable2_colB], [appTable2_colC], [appTable1_ID]
 FROM [stageTable]

This process continues through numerous tables like this. As you can see, I m not including a WHERE clause on the SELECTs from the staging table as this table gets truncated at the end of the process. However, this leaves the possibility of another process adding records to this staging table in the middle of this transaction and those records would not contain the FKs previously populated. Would I want to issue this statement to prevent this?:

 SET TRANSACTION ISOLATION LEVEL SNAPSHOT

If this is the best solution, what are the downsides of doing it this way?

最佳回答

Can you add a batch id to your staging table, so that you can use it in where clauses to ensure that you are only working on the original batch of records? Any process that adds records to the staging table would have to use a new, unique batch id. This would be more efficient (and more robust) than depending on snapshot isolation, I think.

问题回答

All Isolation levels, including snapshot, affect only reads. SELECTs from stageTable will not see uncommited inserts, nor it will block. I m not sure that solves your problem of throwing everything into the stageTable without any regard for ownership. What happens when the transaction finally commits, the stageTable is left with all the intermediate results ready to be read by the next transaction? Perhaps you should use a temporary #stageTable that will ensure a natural isolation between concurent threads.

To understand the cost of using Snapshot isolation, read Row Versioning Resource Usage:

  • extra space consumed in tempdb
  • extra space consumed in each row in the data table
  • extra space consumed in BLOB storage for large fields




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签