交易 - 将表可用于其他查询,直到准备提交给它
我有一个存储过程,需要一段时间,并重建其他查询用于查找的文件。
因此,我想:
- 开始交易
- 重建表(需要一段时间)
- 在步骤2期间提交交易
,我希望其他无关的查询来读取此表(没有程序内更新。)对我来说,可以看到数据是很好的步骤1开始之前。
显然,我不希望查询等待交易完成。
问题:我需要在交易上指定的参数(例如隔离级别)吗?我需要在选定语句上“用nolock”做?
如果有人可以将我指向正确的方向?很难搜索这个问题。
这是一个模型,我想在存储过程正在运行时查询表IREALLYWANTTOQUERYTHISTAST,并在交易开始之前查看数据:
create procedure bigoldlongprocedure
as
Begin Tran Doit
drop table if exists IReallyWantToQueryThisTable
create table IReallyWantToQueryThisTable
(Id int
, dataforme varchar(20)
)
-- Takes a long time
exec dbo.spReLoad_IReallyWantToQueryThisTable
Commit Tran Doit
Return 0
I have a stored procedure that takes awhile and rebuilds a file that other queries use for lookup.
So i want to:
- Begin a transaction
- Rebuild a table (takes awhile)
- Commit the transaction
During step 2, I want other unrelated queries to read this table (without the in-progress updates.) It's fine for me to see the data that was there before step 1 began.
And obviously, I don't want the query to wait for the transaction to finish.
Question: Are there parameters (like isolation level) that I need to specify on the transaction? Do i need to do "with nolock" on the select statements?
if someone could point me in the right direction? It's been a little hard to search for this.
Here's an mockup where i would want to query table IReallyWantToQueryThisTable while the stored procedure is running and see the data from before the transaction started:
create procedure bigoldlongprocedure
as
Begin Tran Doit
drop table if exists IReallyWantToQueryThisTable
create table IReallyWantToQueryThisTable
(Id int
, dataforme varchar(20)
)
-- Takes a long time
exec dbo.spReLoad_IReallyWantToQueryThisTable
Commit Tran Doit
Return 0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在此交易中,无需为表IREALLYWANTTOWISSISTAISS设置任何可读的隔离水平。
交易的五种隔离水平是
读取不承诺
|读取
|可重复阅读
|快照
|可序列化
该表对于其他tranc是不可读的,就在设置可序列化时。
there is no need to set any readable isolation level for the table IReallyWantToQueryThisTable in this transaction.
five types of isolation level for a transaction are
READ UNCOMMITTED
| READ COMMITTED
| REPEATABLE READ
| SNAPSHOT
| SERIALIZABLE
the table is unreadable for other tranc,just when setting SERIALIZABLE.so just make sure the isolation level in dbo.spreload is not SERIALIZABLE.