有效地向数据库添加大量文本(Efficiently adding large amounts of text to a database)
我有一个C#应用程序需要向SQL Server数据库添加大量文本(一行最多可以有1GB的文本)。 所以,我有一些看起来像的代码:
SqlParameter param = command.Parameters.Add("@text", SqlDbType.NVarChar); param.Value = new string(buffer);
文本以块的形式添加,因此缓冲区为20 MB。 我试图通过重新使用缓冲区来减少内存压力(而不是每次我需要添加块时执行新的char [10000000])。 我注意到的是ADO.NET似乎每次调用我的sproc时都会分配一个新的缓冲区。 每次调用sproc时,我都会看到我的进程的工作集几乎上升了20 MB。
现在,您可能认为这是因为“新字符串(缓冲区)”(我做过),但是当我这样做时,我会得到相同的行为
param = command.Parameters.Add("@text", SqlDbType.Text, buffer.Length); param.Value = buffer;
有没有办法阻止ADO.NET每次向数据库添加一大块文本时进行额外的内存分配?
谢谢!
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);
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;
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?
Thanks!
原文:https://stackoverflow.com/questions/1928600
最满意答案
函数签名应该取当前序列的最小编号,当前序列的最大编号和剩余序列。 如果剩余列表的头部大于当前序列的最大号码,则只需递增新的最大号码。 否则,返回当前差异的最大值和剩余当前头部的递归调用作为最小值和最大值。
The function signature should take the lowest number of the current sequence, the highest number of the current sequence and the remainding sequence. If the head of the remaining list is bigger than the highest number of the current sequence, just recurse with the new highest number. Otherwise, return the maximum of the current difference and the recursive call for the remainder with the current head as minimum and maximum.
相关问答
更多-
斯威夫特2 要按顺序创建Ints数组,可以使用“范围”: let a = Array(0...10) // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 这里0...10使Range和Array初始化器使Range成为Ints数组。 还有这个变种: let a = Array(0..<10) // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 要做同样的事情,但采用不同的步进,你可以使用“步幅”: let b = Array(0.stride(through: ...
-
d={} new_list = [d[i] for i in values if d.setdefault(i,len(d)+1)] d={} new_list = [d[i] for i in values if d.setdefault(i,len(d)+1)]
-
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
不,没有这样的内置功能。 这是一个天真的实现: let mode = function | [] -> None | xs -> let mostFrequentTwo = xs |> Seq.groupBy id |> Seq.map (fun (n, ns) -> n, Seq.length ns) |> Seq.sortByDescending snd ...
-
对整数列表进行分区以最小化其总和的差异(Partitioning a list of integers to minimize difference of their sums)[2022-04-07]
一个朴素的,平凡的和仍然是伪多项式的解决方案是使用现有解到子集求和,并且对sum(array)/2重复为0(并返回找到的第一个)。 该解决方案的复杂性将是O(W^2*n) ,其中W是阵列的总和。 伪代码: for cand from sum(array)/2 to 0 descending: subset <- subsetSumSolver(array,cand) if subset != null: return subset 以上将返回低于/等于sum(array)/2的 ... -
用户应该连续10天输入股票价格,该程序将告诉您最大的日常变化 您的代码允许用户输入超过10个价格。 当用户输入下一个价格时,您应该跟踪价格差异。 请参阅以下算法: import java.util.Scanner; public class LargestDiff { public static void main(String[] args) { final int days = 10; Scanner sc = new Scanner(System.in); ...
-
添加到列表中的整数(Add to integers in a list)[2023-05-05]
这是一个例子,其中添加的东西来自字典 >>> L = [0, 0, 0, 0] >>> things_to_add = ({'idx':1, 'amount': 1}, {'idx': 2, 'amount': 1}) >>> for item in things_to_add: ... L[item['idx']] += item['amount'] ... >>> L [0, 1, 1, 0] 以下是从另一个列表添加元素的示例 >>> L = [0, 0, 0, 0] >>> things_ ... -
找到满足特定条件的整数之间的最大差异总和(Finding greatest sum of difference between integers that meet a certain condition)[2022-02-07]
当前方法存在问题 您当前的算法不太正确,请考虑以下顺序: 1,99,1,100,99 您当前的算法将选择以下情况(不同的情况对应于不同的起始位置): 1,100,99 score 100 99,100,99 score 2 1,100,99 score 100 然而,最佳选择是1,99,1,得分为98 * 2 = 196 复杂性也是O(n ^ 2),这将太慢。 更好的算法 AO(n)的方法是计算一个数组A [n],它给出0,1,..,n-1范围内的最小值,以及一个给出该范围内最小值的数组B [n]。 N ... -
“压缩”整数列表(“Compressing” a list of integers)[2022-03-22]
像大多数涉及级联连续重复的问题一样,您仍然可以使用groupby()来实现此目的。 只需按照每个指数的值对指数进行分组。 values = [2,2,2,2,3,4,2,2,4,4,3] result = [] for key, group in itertools.groupby(range(len(values)), values.__getitem__): indices = list(group) if len(indices) > 1: result.appen ... -
函数签名应该取当前序列的最小编号,当前序列的最大编号和剩余序列。 如果剩余列表的头部大于当前序列的最大号码,则只需递增新的最大号码。 否则,返回当前差异的最大值和剩余当前头部的递归调用作为最小值和最大值。 The function signature should take the lowest number of the current sequence, the highest number of the current sequence and the remainding sequence. If ...