有效地将大量的文本到数据库

编程入门 行业动态 更新时间:2024-10-24 08:27:08
本文介绍了有效地将大量的文本到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个C#应用程序,需要大量的文字添加到SQL Server数据库(一个行可能有多达文字1GB)。所以,我有一些code,看起来像:

I have a C# app that needs to add large amounts of text to a SQL Server database (one row could have up to 1GB of text). So, I have some code that looks like:

SqlParameter param = command.Parameters.Add("@text", SqlDbType.NVarChar); param.Value = new string(buffer);

的文本块中添加,所以缓冲区说,20 MB。我试着通过重复使用的缓冲区,以减少内存pressure(而不是做一个新的char [千万]每次我需要增加一大块时间)。我所注意到的是,ADO.NET似乎被分配一个新的缓冲区,每次我的存储过程调用。每次存储过程被调用时,我看到我的进程的工作集上升了几乎整整20 MB。

The text is added in chunks, so the buffer is say, 20 MB. I've tried to reduce memory pressure by re-using buffers (as opposed to doing a new char[10000000] every time I need to add a chunk). What I'm noticing is that ADO.NET seems to be allocating a new buffer for every time my sproc is called. Every time the sproc is called, I see my process's working set go up by almost exactly 20 MB.

现在,你可能会认为是因为新的字符串(缓冲)(我一样),但我得到了相同的行为,当我做

Now, you might think that's because of the "new string(buffer)" (I did), but I get the same behavior when I do

param = command.Parameters.Add("@text", SqlDbType.Text, buffer.Length); param.Value = buffer;

有没有什么办法prevent ADO.NET从我每次添加一个文本块到数据库做额外的内存分配?

Is there any way to prevent ADO.NET from doing the extra memory allocation every time I add a chunk of text to the database?

谢谢!

推荐答案

如果您使用 SqlDbType.VarBinary 与字节[] 磁盘阵列,然后ADO.NET将使用您提供无复制的缓冲区。

If you use SqlDbType.VarBinary with a byte[] array, then ADO.NET will use the buffer you provide without copying.

更多推荐

有效地将大量的文本到数据库

本文发布于:2023-10-28 19:44:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1537596.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有效地   文本   数据库

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!