views:

233

answers:

2

Hi! I've to write a c++ application that receives a huge data stream( many thousands of messages per second) and insert it into a MS sql server2008 db (into one or more tables) as fast as possible. I'm new to windows, and amongst so many ways described in msdn, I'm not able to judge the best way to do this, for example: 1. should I use ODBC (using the sql native driver) or OLE db ? is odbc slow? 2. should I try to do a sqlprepare an insert stmt? should I try to bunch a few inserts together? OR should I try a stored procedure? will this be faster? 3. someone's mentioned to explore if windows exposes a tabular data stream API directly (but i couldn't find one, and it's probably a bit more advanced for me right now.)

I'd like to try something fast to run, fast to develop (and hopefully simple). please help, any ideas most appreciated. many thanks!

+1  A: 

In a similar application I've batched up the incoming data and inserted them using BULK INSERT, via the .Net 2 SqlBulkCopy class. It inserts on the order of 100,000 rows a second.

This is with Sql Server 2005; I'm not aware of any improved methods for such work in 2008.

Edit: As you can't use the .Net framework, an alternative would be use bcp_sendrow to insert the data, or write the incoming data to a file and use BULK INSERT commands or bcp.exe to do the heavy lifting.

Matt Howells
thank you!.. however I need to do this in 'normal' c++, and it looks like SqlBulkCopy is only available via .NET (as far as I've googled till now), is there something similar without using .NET? thanks!
You don't need .NET to perform bulk inserts. Actually I think Bulk Insert predates the .NET framework. It was in SQL Server 2000 and remains today.
onupdatecascade
+1  A: 

Look at SqlBulkCopy - this allows fast inserts of multiple rows of data. You could buffer a few thousand rows and insert them periodically.

ck
thank you! I'm however looking for something to use via 'normal' c++ (i.e without using .net or managed extensions if possible) , currently searching for a way to bulk copy without using .net...
You can do bulk copy using the bcp executable from SQL Server. Would this work for you?
ck
thanks for your answer! I'm currently investigating bcp_sendrow / bcp_bind etc. (sql server extensions) to check if it fits. still searching :-)