We have code like:
ms = New IO.MemoryStream
bin = New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
bin.Serialize(ms, largeGraphOfObjects)
dataToSaveToDatabase = ms.ToArray()
// put dataToSaveToDatabase in a Sql server BLOB
But the memory steam allocates a large buffer from the large memory heap that is giving us problems. So how can we stream the data without needing enough free memory to hold the serialized objects.
I am looking for a way to get a Stream from SQL server that can then be passed to bin.Serialize() so avoiding keeping all the data in my processes memory.
Likewise for reading the data back...
Some more background.
This is part of a complex numerical processing system that processes data in near real time looking for equipment problems etc, the serialization is done to allow a restart when there is a problem with data quality from a data feed etc. (We store the data feeds and can rerun them after the operator has edited out bad values.)
Therefore we serialize the object a lot more often then we de-serialize them.
The objects we are serializing include very large arrays mostly of doubles as well as a lot of small “more normal” objects. We are pushing the memory limit on 32 bit systems and make the garbage collector work very hard. (Effects are being made elsewhere in the system to improve this, e.g. reusing large arrays rather then create new arrays.)
Often the serialization of the state is the last straw that causes an out of memory exception; the peak of our memory usage is always during this serialization step.
I think we get large memory pool fragmentation when we de-serialize the object, I expect there are also other problems with large memory pool fragmentation given the size of the arrays. (This has not yet been investigated, as the person that first looked at this is a numerical processing expert, not a memory management expert.)
Our customers use a mix of SQL Server 2000, 2005 and 2008 and we would rather not have different code paths for each version of SQL Server if possible.
We can have many active models at a time (in different processes, across many machines), each model can have many saved states. Hence the saved state is stored in a database blob rather then a file.
As the spread of saving the state is important, I would rather not serialize the object to a file, and then put the file in a BLOB one block at a time.
Other related questions I have asked
7条答案
按热度按时间vwhgwdsa1#
There is no built-in ADO.Net functionality to handle this really gracefully for large data. The problem is two fold:
FileStream
) accept the stream to READ from it, which does not agree with the serialization semantics of write into a stream. No matter which way you turn this, you end up with a in memory copy of the entire serialized object, bad.So you really have to approach this from a different angle. Fortunately, there is a fairly easy solution. The trick is to use the highly efficient
UPDATE .WRITE
syntax and pass in the chunks of data one by one, in a series of T-SQL statements. This is the MSDN recommended way, see Modifying Large-Value (max) Data in ADO.NET . This looks complicated, but is actually trivial to do and plug into a Stream class.The BlobStream class
This is the bread and butter of the solution. A Stream derived class that implements the Write method as a call to the T-SQL BLOB WRITE syntax. Straight forward, the only thing interesting about it is that it has to keep track of the first update because the
UPDATE ... SET blob.WRITE(...)
syntax would fail on a NULL field:Using the BlobStream
To use this newly created blob stream class you plug into a
BufferedStream
. The class has a trivial design that handles only writing the stream into a column of a table. I'll reuse a table from another example:I'll add a dummy object to be serialized:
Finally, the actual serialization. We'll first insert a new record into the
Uploads
table, then create aBlobStream
on the newly inserted Id and call the serialization straight into this stream:If you monitor the execution of this simple sample you'll see that nowhere is a large serialization stream created. The sample will allocate the array of [1024*1024] but that is for demo purposes to have something to serialize. This code serializes in a buffered manner, chunk by chunk, using the SQL Server BLOB recommended update size of 8040 bytes at a time.
qmelpv7a2#
All you need is .NET Framework 4.5 and streaming. Let's assume we have a big file on HDD and we want to upload this file.
SQL code:
C# code:
Works good for me. I have successfully uploaded the file of 400 mb, while MemoryStream throwed an exception when I tried to load this file into memory.
UPD: This code works on Windows 7, but failed on Windows XP and 2003 Server.
beq87vna3#
You can always write to SQL Server at a lower level using the over the wire protocol TDS (tabular data stream) that Microsoft has used since day one. They are unlikely to change it any time soon as even SQLAzure uses it!
You can see source code of how this works from the Mono project and from the freetds project
Check out the
tds_blob
lf5gs5x24#
What does the graph look like?
One problem here is the stream; the SQL 2005 requirement is a pain, as otherwise you could write directly to
SqlFileStream
, however, I don't think it would be too hard to write your ownStream
implementation that buffers 8040 (or some multiple) bytes and writes it incrementally. However, I'm not sure that it is worth this extra complexity - I would be hugely tempted to just use a file as the scratch buffer and then (once serialized) loop over the file inserting/appending chunks. I don't think that the file system is going to hurt your overall performance here, and it will save you starting to write doomed data - i.e. you don't talk to the database until you already know what data you want to write. It will also help you minimise the time the connection is open.The next problem is the serialization itself. Personally I don't recommend using
BinaryFormatter
to write to persistent stores (only for transport), since it is implementation specific both in the encoder itself, and in your types (i.e. it is brittle if you make innocent-looking changes to your data types).If your data can be represented sufficiently as a tree (rather than a full graph), I would be very tempted to try protocol buffers / protobuf-net. This encoding (devised by Google) is smaller than the
BinaryFormatter
output, faster both for read and write, and is contract-based rather than field-based, so you can reliably rehydrate it again later (even if you switch platform entirely).The default options mean that it has to write the object-length before each object (which might be expensive in your case), but if you have nested lists of large (deep) objects you can use grouped encoding to avoid this need - allowing it to write the stream in a forwards-only, single-pass way; here's a brief simple example using grouped encoding, but if you want to throw a more complex scenario at me, just let me know...
Note: I do have some theories on how to hack Google's wire format to support full graphs, but it is going to need some time to try it. Oh, re the "very large arrays" - for primitive types (not objects) yuo can use "packed" encoding for this;
[DataMember(..., Options = MemberSerializationOptions.Packed)]
- might be useful, but hard to say without visibility of your model.pcrecxhr5#
Why not implement your own system::io:stream derived class? which would allow you to attach it to the SQL column directly via UpdateText for writing.
eg (pseudo-code)
Insert DB Record with blob column 'initialized' (see above UpdateText article)
Create Your Stream Type / Associate DB connection with the stream
Pass the stream to the serialize call
It could chunk up (Multiple of 8040 bytes at a time, i presume) the calls to it and on each full buffer pass that to the DB UpdateText call with the proper offset.
On close of the stream you'd flush whatever was left that didn't fill the buffer entirely via UpdateText.
Likewise you could use the same/similar derived stream to allow reading from a DB column, passing that to be deserialized.
Creating a derived Stream is not all that much work - i've done it in C++/CLI to provide interoperability with IStream's -- and if i can do it :)... (i can provide you the C++/CLI stream code i've done as a sample if that would be helpful)
If you put the entire operation (Insert of initial row, calls to update the blob via the stream) into a transaction you would avoid any potential db inconsistencies if the serialization step fails.
eulz3vhy6#
I would go with files. Basically use the file system as an intermediate between the SQL Server and your application.
INSERT INTO MyTable ( [MyColumn] ) SELECT b.BulkColumn, FROM OPENROWSET(BULK N'C:\Path To My File\File.ext', SINGLE_BLOB) as b
This procedure can be generalized into a helper class to do it, which will know when to delete those temporary files, as you can reuse them if you know for sure that the value of the sql data record hasn't changed.
b5buobof7#
Note that since SQL Server 2012 there's also FileTable what is similar to FILESTREAM except that allows non-transactional access too.
https://msdn.microsoft.com/en-us/library/hh403405.aspx#CompareFileTable