在 SQL Server 2008 中运行托管代码 - 有问题吗?

发布于 2024-09-11 20:44:34 字数 204 浏览 3 评论 0原文

我想知道与通过 SQL Server 2008 运行托管代码相关的性能问题。我听说过一些内存和速度问题。

具体来说,我想实现一个 SHA256 哈希 DLL,并使用 SQL Server 2008 将其作为存储过程执行。

或者,我可以简单地从我的 .Net 应用程序执行哈希,然后将字符串传递到我的存储过程。

优点/缺点?

谢谢。

I would like to know the performance issues associated with running managed code through SQL Server 2008. I've heard about some memory and speed issues.

Specifically, I want to implement a SHA256 hashing DLL and execute it as a sproc with SQL Server 2008.

Alternately, I could simply execute the hashing from my .Net app, then pass the string to my sprocs.

Pros/cons?

Thanks.

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

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

发布评论

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

评论(1

梦过后 2024-09-18 20:44:34

SQLCLR 相当快。如果您明确需要在 T-SQL 中执行 CLR 代码,请使用它。例如,假设您编写了一个具有签名的 SQLCLR 函数:

SqlString Hash(SqlString input)

并且您设法使其全部按应有的方式运行。您可以执行以下查询:

IF EXISTS(SELECT * FROM Users WHERE HashedPassword = dbo.Hash(@userPassword) AND UserName = @userName)
BEGIN
    SELECT 'You''re alright, Jack.';
END
ELSE
BEGIN
    SELECT 'Bogus.';
END

如果您可以设法对应用程序中的假设密码字段进行哈希处理,则最好在应用程序内执行此操作,并将哈希值传递到 SQL Server 中以运行如下查询:

SELECT *
FROM User
WHERE UserName = @userName AND HashedPassword = @hashedPassword

这对于有几个原因:

  1. 您没有将业务逻辑放入 SQL Server 中,这将很难测试。
  2. 查询本身不那么复杂,因此 SQL Server 将执行更少的工作,因此速度更快。
  3. 设置 SQLCLR 可能很痛苦(SQLCLR 默认情况下处于关闭状态)。将程序集部署到 SQL Server 中可能会很痛苦,尤其是在您无法直接访问生产服务器的情况下。
  4. 如果您不使用 SQLCLR,那么当您更新/部署应用程序时,您不必记得更新/部署 SQL Server 中的 CLR 内容。这减少了您的维护工作量。

SQLCLR is quite fast. Use it if you have a clear need to execute CLR code within T-SQL. For example, say you write a SQLCLR function with the signature:

SqlString Hash(SqlString input)

and you manage to get it all running the way it should. You could execute the following query:

IF EXISTS(SELECT * FROM Users WHERE HashedPassword = dbo.Hash(@userPassword) AND UserName = @userName)
BEGIN
    SELECT 'You''re alright, Jack.';
END
ELSE
BEGIN
    SELECT 'Bogus.';
END

If you can manage to hash the hypothetical password field in your app, you are better off to do so within the app and pass the hashed value into SQL Server to run a query like this:

SELECT *
FROM User
WHERE UserName = @userName AND HashedPassword = @hashedPassword

This is better for a few reasons:

  1. You aren't putting business logic into SQL Server, where it will be hard to test.
  2. The query itself is much less complicated, so SQL Server will be doing less work, and therefore be faster.
  3. Setting SQLCLR up can be a pain (SQLCLR is off by default). Deploying your assemblies into SQL Server can be a pain, especially if you don't have direct access to the production server.
  4. If you don't use SQLCLR, when you update/deploy your app, you won't have to remember to update/deploy the CLR stuff in SQL Server. This reduces your maintenance efforts.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文