SET READ_COMMITTED_SNAPSHOT ON 需要多长时间?

发布于 2024-07-08 00:36:35 字数 142 浏览 14 评论 0原文

运行需要多长时间

ALTER DATABASE [MySite] SET READ_COMMITTED_SNAPSHOT ON

我刚刚运行了一下,花了10分钟。

如何检查是否已应用?

How long should it take to run

ALTER DATABASE [MySite] SET READ_COMMITTED_SNAPSHOT ON

I just ran it and it's taken 10 minutes.

How can I check if it is applied?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

不喜欢何必死缠烂打 2024-07-15 00:36:35

您可以使用 sys.databases 视图检查 READ_COMMITTED_SNAPSHOT 设置的状态。 检查 is_read_comfilled_snapshot_on 列的值。 已经询问并回答了

至于持续时间,联机丛书指出,发生这种情况时不能有任何其他连接到数据库,但它不需要单用户模式。 因此,您可能会被其他活动连接阻止。 运行 sp_who (或 sp_who2)以查看还有哪些内容连接到该数据库。

You can check the status of the READ_COMMITTED_SNAPSHOT setting using the sys.databases view. Check the value of the is_read_committed_snapshot_on column. Already asked and answered.

As for the duration, Books Online states that there can't be any other connections to the database when this takes place, but it doesn't require single-user mode. So you may be blocked by other active connections. Run sp_who (or sp_who2) to see what else is connected to that database.

晚风撩人 2024-07-15 00:36:35

尝试这个:

ALTER DATABASE generic SET READ_COMMITTED_SNAPSHOT ON WITH ROLLBACK IMMEDIATE

Try this:

ALTER DATABASE generic SET READ_COMMITTED_SNAPSHOT ON WITH ROLLBACK IMMEDIATE
浅紫色的梦幻 2024-07-15 00:36:35

好吧(我是最初的提问者)所以事实证明我一直没有启用这个该死的东西。

这是要运行以启用快照模式的最终代码确保它已启用。

SELECT is_read_committed_snapshot_on, snapshot_isolation_state_desc,snapshot_isolation_state FROM sys.databases WHERE name='shipperdb'

ALTER DATABASE shipperdb SET allow_snapshot_isolation ON
ALTER DATABASE shipperdb SET SINGLE_USER WITH ROLLBACK IMMEDIATE
ALTER DATABASE shipperdb SET read_committed_snapshot ON
ALTER DATABASE shipperdb SET MULTI_USER

SELECT is_read_committed_snapshot_on, snapshot_isolation_state_desc,snapshot_isolation_state FROM sys.databases WHERE name='shipperdb'

即使连接处于活动状态,这也有效(大概您对它们被踢出感到满意)。

您可以看到之前和之后的状态,这应该几乎立即运行。


重要提示:

上面的 READ_COMMITTED_SNAPSHOT 选项对应于 .NET 中的 IsolationLevel.ReadCommitted
上面的选项 ALLOW_SNAPSHOT_ISOLATION 对应于 .NET 中的 IsolationLevel.Snapshot

关于不同版本控制的精彩文章< /a>


.NET 提示:

看起来代码中允许使用 Isolationlevel.ReadCommissed,即使数据库未启用也是如此。 不会引发任何警告。 所以帮自己一个忙,在你像我一样认为它可以使用 3 年之前确保它已打开!

如果您使用 C#,您可能需要 ReadCommissed IsolationLevel 而不是 Snapshot - 除非您在此事务中进行写入操作。

READ COMMITTED SNAPSHOT 执行乐观读取和悲观写入。 相反,SNAPSHOT 执行乐观读取和乐观写入。 (来自此处)

bool snapshotEnabled = true;

using (var t = new TransactionScope(TransactionScopeOption.Required,
               new TransactionOptions
{
     IsolationLevel = IsolationLevel.ReadCommitted
}))
{
     using (var shipDB = new ShipperDBDataContext())
     {

     }
}

此外,您可能会收到有关“无法促进交易。 在.NET Framework 2.0 中的 System.Transactions 简介

除非您正在做一些特殊的事情,例如连接到外部数据库(或第二个数据库),那么像创建新的 DataContext 这样简单的事情就可能导致这种情况。 我有一个缓存,它在初始化时“旋转”自己的数据上下文,这试图将事务升级为完全分布式事务。

解决方案很简单:

        using (var tran = new TransactionScope(TransactionScopeOption.Suppress))
        {
            using (var shipDB = new ShipperDBDataContext())
            { 
                 // initialize cache
            }
        }

另请参阅@CodingHorror 的 死锁 文章

OK (I am the original questioner) so it turns out this whole time I didn't even have the darn thing enabled.

Here's the ultimate code to run to enable snapshot mode and make sure it is enabled.

SELECT is_read_committed_snapshot_on, snapshot_isolation_state_desc,snapshot_isolation_state FROM sys.databases WHERE name='shipperdb'

ALTER DATABASE shipperdb SET allow_snapshot_isolation ON
ALTER DATABASE shipperdb SET SINGLE_USER WITH ROLLBACK IMMEDIATE
ALTER DATABASE shipperdb SET read_committed_snapshot ON
ALTER DATABASE shipperdb SET MULTI_USER

SELECT is_read_committed_snapshot_on, snapshot_isolation_state_desc,snapshot_isolation_state FROM sys.databases WHERE name='shipperdb'

This works even with connections active (presumably you're fine with them getting kicked out).

You can see the before and after state and this should run almost immediately.


IMPORTANT:

The option READ_COMMITTED_SNAPSHOT above corresponds to IsolationLevel.ReadCommitted in .NET
The option ALLOW_SNAPSHOT_ISOLATION above corresponds to IsolationLevel.Snapshot in .NET

Great article about different versioning


.NET Tips:

Looks like Isolationlevel.ReadCommitted is allowed in code even if not enabled by the database. No warning is thrown. So do yourself a favor and be sure it is turned on before you assume it is for 3 years like I did!!!

If you're using C# you probably want the ReadCommitted IsolationLevel and not Snapshot - unless you are doing writes in this transaction.

READ COMMITTED SNAPSHOT does optimistic reads and pessimistic writes. In contrast, SNAPSHOT does optimistic reads and optimistic writes. (from here)

bool snapshotEnabled = true;

using (var t = new TransactionScope(TransactionScopeOption.Required,
               new TransactionOptions
{
     IsolationLevel = IsolationLevel.ReadCommitted
}))
{
     using (var shipDB = new ShipperDBDataContext())
     {

     }
}

In additional you may get an error about being 'unable to promote' a transaction. Search for 'promotion' in Introducing System.Transactions in the .NET Framework 2.0.

Unless you're doing something special like connecting to an external database (or second database) then something as simple as creating a new DataContext can cause this. I had a cache that 'spun up' its own datacontext at initialization and this was trying to escalate the transaction to a full distributed one.

The solution was simple :

        using (var tran = new TransactionScope(TransactionScopeOption.Suppress))
        {
            using (var shipDB = new ShipperDBDataContext())
            { 
                 // initialize cache
            }
        }

See also Deadlocked article by @CodingHorror

三岁铭 2024-07-15 00:36:35

试试这个代码:

if(charindex('Microsoft SQL Server 2005',@@version) > 0)
begin
    declare @sql varchar(8000)
    select @sql = '
    ALTER DATABASE ' + DB_NAME() + ' SET SINGLE_USER WITH ROLLBACK IMMEDIATE ;
    ALTER DATABASE ' + DB_NAME() + ' SET READ_COMMITTED_SNAPSHOT ON;
    ALTER DATABASE ' + DB_NAME() + ' SET MULTI_USER;'

    Exec(@sql)
end

Try this code:

if(charindex('Microsoft SQL Server 2005',@@version) > 0)
begin
    declare @sql varchar(8000)
    select @sql = '
    ALTER DATABASE ' + DB_NAME() + ' SET SINGLE_USER WITH ROLLBACK IMMEDIATE ;
    ALTER DATABASE ' + DB_NAME() + ' SET READ_COMMITTED_SNAPSHOT ON;
    ALTER DATABASE ' + DB_NAME() + ' SET MULTI_USER;'

    Exec(@sql)
end
叫嚣ゝ 2024-07-15 00:36:35

我尝试了命令:

ALTER DATABASE MyDB SET READ_COMMITTED_SNAPSHOT ON
GO

针对开发盒,但花了 10 多分钟,所以我杀死了它。

然后我发现了这个:

https://willwarren.com/2015 /10/12/sql-server-read-comfilled-snapshot/

并使用他的代码块(运行时间约为 1:26):

USE master
GO

/** 
 * Cut off live connections
 * This will roll back any open transactions after 30 seconds and
 * restricts access to the DB to logins with sysadmin, dbcreator or
 * db_owner roles
 */
ALTER DATABASE MyDB SET RESTRICTED_USER WITH ROLLBACK AFTER 30 SECONDS
GO

-- Enable RCSI for MyDB
ALTER DATABASE MyDB SET READ_COMMITTED_SNAPSHOT ON
GO

-- Allow connections to be established once again
ALTER DATABASE MyDB SET MULTI_USER
GO

-- Check the status afterwards to make sure it worked
SELECT is_read_committed_snapshot_on
FROM sys.databases
WHERE [name] = 'MyDB '

I tried the command:

ALTER DATABASE MyDB SET READ_COMMITTED_SNAPSHOT ON
GO

against a dev box but the it took 10+ minutes and so I killed it.

I then found this:

https://willwarren.com/2015/10/12/sql-server-read-committed-snapshot/

and used his code block (which took about 1:26 to run):

USE master
GO

/** 
 * Cut off live connections
 * This will roll back any open transactions after 30 seconds and
 * restricts access to the DB to logins with sysadmin, dbcreator or
 * db_owner roles
 */
ALTER DATABASE MyDB SET RESTRICTED_USER WITH ROLLBACK AFTER 30 SECONDS
GO

-- Enable RCSI for MyDB
ALTER DATABASE MyDB SET READ_COMMITTED_SNAPSHOT ON
GO

-- Allow connections to be established once again
ALTER DATABASE MyDB SET MULTI_USER
GO

-- Check the status afterwards to make sure it worked
SELECT is_read_committed_snapshot_on
FROM sys.databases
WHERE [name] = 'MyDB '
陌生 2024-07-15 00:36:35

您需要做的就是:
更改数据库 xyz 将 READ_COMMITTED_SNAPSHOT 设置为立即回滚;

无需将数据库置于单用户模式。
不过,您将回滚未提交的事务。

All you need to do is this:
ALTER DATABASE xyz SET READ_COMMITTED_SNAPSHOT ON WITH ROLLBACK IMMEDIATE;

No need to put the database into single user mode.
You will rollback uncommitted transactions though.

写给空气的情书 2024-07-15 00:36:35

在更改当前数据库之前尝试使用 master 数据库。

USE Master
GO

ALTER DATABASE [YourDatabase] SET READ_COMMITTED_SNAPSHOT ON
GO

Try use master database before altering current database.

USE Master
GO

ALTER DATABASE [YourDatabase] SET READ_COMMITTED_SNAPSHOT ON
GO
故人爱我别走 2024-07-15 00:36:35

使用“ROLLBACK IMMEDIATE”,我的 300GB 数据库花费了大约 20-30 秒。

ALTER DATABASE DBNAME SET READ_COMMITTED_SNAPSHOT ON WITH ROLLBACK IMMEDIATE

With "ROLLBACK IMMEDIATE" it took about 20-30 seconds on my db which is 300GB.

ALTER DATABASE DBNAME SET READ_COMMITTED_SNAPSHOT ON WITH ROLLBACK IMMEDIATE
落在眉间の轻吻 2024-07-15 00:36:35

当我将数据库更改为单用户时,我没有花一秒钟的时间

I didn't take a second for me when i changed my DB to single user

陪我终i 2024-07-15 00:36:35

尝试关闭其他 SQL 服务,以便仅 SQL Server 服务在运行。

我的运行了 5 分钟,然后我取消了它,因为很明显什么也没发生。 它是一个全新的服务器,因此没有其他用户连接。 我关闭了 SQL 报告服务,然后再次运行它......不到一秒钟就完成了。

Try Shut off the other SQL services so that only the SQL server service is running.

Mine ran for 5 minutes then I cancelled it because it was obvious nothing was happening. Its a brand new server so there are no other users connected. I shut off the SQL Reporting Services and then ran it again.. took less than a second to complete.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文