使用 SQL CLR 存储过程进行表记录插入的最佳实践?

发布于 2024-07-27 01:43:27 字数 473 浏览 2 评论 0原文

最近我们将一组复杂的基于C#的调度逻辑转换为SQL CLR存储过程(在SQL Server 2005中运行)。 我们相信我们的代码是一个很好的 SQL CLR 候选者,因为:

  • 逻辑涉及来自 SQL Server 的大量数据。
  • 逻辑很复杂,很难使用 TSQL 来完成。
  • 没有线程或同步,也没有从沙箱外部访问资源。

到目前为止,我们的 sp 的结果相当不错。 但是,由于逻辑的输出采用多个数据表的形式,因此我们不能仅返回单个行集作为 sp 的结果。 相反,在我们的代码中,我们在 foreach 循环中有很多“INSERT INTO ....”语句,以便将 C# 通用集合中的每条记录保存到 SQL 表中。 在代码审查期间,有人担心 SQL CLR 中的内联 SQL INSERT 方法是否会导致性能问题,并想知道是否有其他更好的方法来转储数据(从我们的 C# 通用集合中)。

那么,有什么建议吗?

Recently we turned a set of complicate C# based scheduling logic into SQL CLR stored procedure (running in SQL Server 2005). We believed that our code is a great SQL CLR candidate because:

  • The logic involves tons of data from sQL Server.
  • The logic is complicate and hard to be done using TSQL
  • There is no threading or sychronization or accessing resources from outside of the sandbox.

The result of our sp is pretty good so far. However, since the output of our logic is in form of several tables of data, we can't just return a single rowset as the result of the sp. Instead, in our code we have a lot of "INSERT INTO ...." statements in foreach loops in order to save each record from C# generic collection into SQL tables. During code review, someone raised concern about whether the inline SQL INSERT approach within the SQL CLR can cause perforamnce problem, and wonder if there's other better way to dump data out (from our C# generic collections).

So, any suggestion?

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

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

发布评论

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

评论(2

焚却相思 2024-08-03 01:43:27

几个月前,我在处理 SQLite 项目时遇到了这个,并发现它很有启发性。 我想这可能就是您正在寻找的。

...

最快的通用插入数据方式
使用标准 ADO.NET 构造

现在慢的东西已经消失了
顺便,我们来谈谈一些硬核的
批量装载。 除了 SqlBulkCopy 之外
和专门的结构涉及
ISAM 或自定义批量插入类
从其他供应商那里,有简单的
没有击败的原始力量
对参数化的 ExecuteNonQuery()
插入语句。 我将演示:

内部静态 void FastInsertMany(DbConnection cnn) 
  { 

      使用 (DbTransaction dbTrans = cnn.BeginTransaction()) 
      { 

          使用 (DbCommand cmd = cnn.CreateCommand()) 
          { 

              cmd.CommandText = "插入测试用例(MyValue) 值(?)"; 

              DbParameter Field1 = cmd.CreateParameter(); 

              cmd.Parameters.Add(Field1); 

              for (int n = 0; n < 100000; n++) 
              { 

                  Field1.Value = n + 100000; 

                  cmd.ExecuteNonQuery(); 

              } 

          } 

          dbTrans.Commit(); 

      } 

  } 
  

I ran across this while working on an SQLite project a few months back and found it enlightening. I think it might be what you're looking for.

...

Fastest universal way to insert data
using standard ADO.NET constructs

Now that the slow stuff is out of the
way, lets talk about some hardcore
bulk loading. Aside from SqlBulkCopy
and specialized constructs involving
ISAM or custom bulk insert classes
from other providers, there is simply
no beating the raw power of
ExecuteNonQuery() on a parameterized
INSERT statement. I will demonstrate:

internal static void FastInsertMany(DbConnection cnn)
{

    using (DbTransaction dbTrans = cnn.BeginTransaction())
    {

        using (DbCommand cmd = cnn.CreateCommand())
        {

            cmd.CommandText = "INSERT INTO TestCase(MyValue) VALUES(?)";

            DbParameter Field1 = cmd.CreateParameter();

            cmd.Parameters.Add(Field1);

            for (int n = 0; n < 100000; n++)
            {

                Field1.Value = n + 100000;

                cmd.ExecuteNonQuery();

            }

        }

        dbTrans.Commit();

    }

}
陪我终i 2024-08-03 01:43:27

您可以返回一个包含 2 列的表 (COLLECTION_NAME nvarchar(max), CONTENT xml),其中填充的行数与您拥有的内部集合一样多。 CONTENT 将是集合中数据的 XML 表示形式。

然后,您可以使用 SQL 2005/2008 的 XML 功能将每个集合的 XML 解析为表,并对整个表执行 INSERT INTO 或 MERGE 语句。

这应该比 C# 代码中的单独插入更快。

You could return a table with 2 columns (COLLECTION_NAME nvarchar(max), CONTENT xml) filled with as many rows as internal collections you have. CONTENT will be an XML representation of the data in the collection.

Then you can use the XML features of SQL 2005/2008 to parse each collection's XML into tables, and perform your INSERT INTO's or MERGE statements on the whole table.

That should be faster than individual INSERTS inside your C# code.

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