SqlDependency 有哪些限制?
我使用表作为消息队列,并使用 SqlDependency“注册”更新。在我读过的所有地方,人们都在说“注意它的局限性”,但没有具体说明它们是什么。根据我的收集,当表的更新频率非常高时,您将会遇到问题;幸运的是,我每分钟最多只查看 10 - 20 个值。
SqlServer 上还有哪些其他限制/影响?
I am using a table as a message queue and "signing up" for updates by using a SqlDependency. Everywhere I read, people are saying "look out for the limitations of it" but not specifically saying what they are. From what I've gathered, you will have problems when the table has very high update frequency; fortunately, I'm only looking at 10 - 20 values per minute maximum.
What are the other limitations/impact on the SqlServer?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我能找到的最完整的列表(从这里)如下:
其他参考:
The most complete list I can find (from here) is as follows:
Additional reference:
除此之外,对于任何其他考虑使用 SqlDependency 接收有关更改的通知的人来说,我一直在生产中使用这种方法,并且我发现了它的问题。我正在调查它,看看问题是否与我的代码有关,但主要问题是:
如果您快速连续触发多个更改,您并不总是会获得相同数量的事件传递到代码。在我的代码中,如果一个接一个地插入 2 个新记录,我只会收到一条通知(针对最后一条)。
如果
无法知道添加的记录。因此,如果您添加一条新记录,并且代码触发以接收通知,则代码中无法知道该新记录的 id,因此您需要查询数据库来获取它。
In addition to this, for anyone else thinking about using SqlDependency to receive notifications about changes, I've been using this approach in production, and I'm finding problems with it. I'm looking into it to see if the problems are related to my code, but the main issues are:
If you fire multiple changes in quick succession, you don't always get the equivalent number of events coming through to the code. In my code, if 2 new records are inserted one after the other, I only get the one notification (for the last one).
There is no way to know the record that was added. So if you add a new record, and the code fires to receive the notification, there is no way in the code to know the id of that new record, so you need to query the database for it.
花了一天时间追查 SQL Service Broker 无法工作的问题,根本原因是在存储过程中引用数据库。
例如,此
select
在 SQL Management Studio 中工作正常:但是,这会被 SQL Service Broker 拒绝,因为我们在 select 语句中引用数据库,以及来自
SqlDependency
的回调> 在SqlNotificationEventArgs e
中返回Invalid
,请参阅 http://msdn.microsoft.com/en-us/library/ms189308.aspx< /a>.将传递到 SqlDependency 的 SQL 更改为以下语句消除了错误:
Update
上面的示例只是 SQL Service Broker 所依赖的 SQL 语句的众多限制之一。有关限制的完整列表,请参阅SqlDependency 有哪些限制。
原因是什么? SQL Service Broker 使用的 SQL 语句在后台转换为指令以监视 SQL 事务日志,用于记录对数据库的更改。这种监视是在 SQL Server 的核心中执行的,这使得在检测表的更改时速度非常快。然而,这种速度是有代价的:您不能只使用任何 SQL 语句,您必须使用可以转换为指令来监视 SQL 事务日志。
Spent a day chasing down an issue with SQL Service Broker not working, the root cause was referencing the database in the stored procedure.
For example, this
select
works fine in SQL Management Studio:However, this is rejected by SQL Service Broker because we are referencing the database in the select statement, and the callback from
SqlDependency
comes back withInvalid
inSqlNotificationEventArgs e
, see http://msdn.microsoft.com/en-us/library/ms189308.aspx.Altering the SQL passed into SqlDependency to the following statement eliminated the error:
Update
The example above is just one of many, many limitations to the SQL statement that SQL Service Broker depends on. For a complete list of limitations, see What are the limitations of SqlDependency.
The reason? The SQL statement that SQL Service Broker uses is converted, behind the scenes, into instructions to monitor the SQL Transaction Log for changes to the database. This monitoring is performed in the core of SQL Server, which makes it extremely fast when it comes to detecting changes to table(s). However, this speed comes at a cost: you can't use just any SQL statement, you must use one that can be converted into instructions to monitor the SQL Transaction Log.
请注意,您不能在存储过程中使用 nolock 提示,否则依赖项将始终保持无效状态,因此您对其进行的任何缓存都将永久重新查询数据库。
文档中似乎没有提到这一点(据我所知)。
在过程脚本之前需要以下 SET 选项
其他人认为这些 SET 选项也是必需的,但我认为并非如此。无论如何,像这样设置它们是个好主意。
Note that you cannot use a nolock hint in the stored procedure or the dependency will remain constantly invalid and therefore any cache you make on it will permanently re-query the database.
This does not appear to be mentioned in the documentation (as far as I can tell).
The following SET options are required prior to the procedure script
Others have argued that these SET options are also required, but I don't think they are. It's a good idea to set them like this anyway though.
这项技术的另一个大问题是:订阅者连接需要具有创建过程权限。我的应用程序的 Web 服务层目前以受限用户身份运行。要使用 SQLDependency 设置通知,我必须打开该用户来创建过程。听起来像是在拥有自己的道路上迈出了相当好的一步。
Another big issue I have with this technology: the need for the subscriber connection to have Create Procedure permissions. The web service layer of my application at work at the moment runs as a restricted user. To get notifications setup using SQLDependency I'd have to open up that user to create procs. Sounds like a pretty good step along the path of getting owned.
要克服这些限制,您可以尝试使用 SqlTableDependency。
看看 www.sqltabledependency.it
To overcome these limitations, you can try use the SqlTableDependency.
Have a look at www.sqltabledependency.it
它使用服务代理。因此,它不适用于非托管 SQL Azure 实例。因此,如果您正在使用或可能使用 SQL Azure,请务必小心。
https://learn.microsoft.com/en-我们/azure/sql-database/sql-database-features
因此,除非您的所有环境都可以使用它,否则可能不太适合!
It uses Service Broker. Therefore it won't work on non managed SQL Azure instances. So be cautious if you're using SQL Azure or ever might.
https://learn.microsoft.com/en-us/azure/sql-database/sql-database-features
So probably not a good fit unless all your environments can use it!
SqlDependency 不适用于内存优化表。我在文档中没有找到它,但在我的应用程序中却没有。
SqlDependency does not work with memory-optimized tables. I did not find it in the documentation but in my app it does not.