views:

215

answers:

3

The application layer (ColdFusion) needs to insert multiple rows into SQL Server 2005.

I was thinking of using loop in app layer to construct multiple input statements and send to SQL Server over JDBC in a single connection.

My colleague however suggests to construct an XML and bulk insert the XML instead.

Which one is the better method?

+1  A: 

One INSERT at a time will be unnecessarily slow due to all that network latency.

A better performing way would be to send several transactions as a single batch in one network round trip and commit them as a single unit of work.

If you have a very large number of records you might want to consider a hybrid approach: loop over a number of batches and send them to be committed as a unit of work. This will perform, even for large transactions, and you won't force your database to maintain a large rollback log before committing the whole transaction.

I'm not a fan if your XML solution means inserting the raw XML stream as a CLOB. How will you query it once it's in the database? You lose everything that SQL gives you: the ability to query. All you can do is XPath for particular values if you store the raw XML. And updates mean replacing the entire CLOB.

duffymo
how about the XML approach, is it any good? thx
Henry
You can use OPENXML to create a rowset from an XML source (a rowset is the result of a table, view, etc..) I've not used OPENXML, so I have no idea about performnace. For the problem at hand, it sounds like adding a lot of complexity for little or probably negative gain.
mdma
+4  A: 

For inserts of millions of rows, I've used BULK INSERT, writing data to a CSV file that the SQL Server instance has access to. This outperforms any type of insert via JDBC, but at the cost of reduced flexibility. For smaller numbers of rows, using JDBC's Statement.addBatch() and Statement.executeBatch() can be used to avoid the overhead of sending many small commands.

Depending upon your requirements, you may have to put all this in one transaction, or you may be able to split up into several transactions, if full ACID guarantees for the entire data set are not required.

Here is an article discussing bulk insert of XML. I have no data to base any conclusion on, but at a guess I would imagine BULK INSERT the raw row data will be faster since there is no OPENXML transformation required. Of course, if your data is already in XML then this makes sense, but if not, then staying with tabular data is probably simplest, and possibly most performant.

mdma
Thx, I'd say the dataset is in the hundreds, not millions. So I'll not be considering BULK INSERT
Henry
Is the XML Bulk Load component in the article accessible from JDBC?
Henry
Not from JDBC. You can access COM objects frm java, but this seems like overkill for your situation. If we are only talking a few hundred rows, and your data is not in XML already, then simply use batched JDBC inserts. It's not the absolute fastest method, but it's easy to do, performance should be more than adequate.
mdma
+1  A: 
iKnowKungFoo
what are the reasons for thinking XML is the better approach? thx
Henry
One insert statement vs. more than one?
iKnowKungFoo
but I still need to loop through something to construct the XML (or input statements)... Is single statement in SQL level much better?
Henry
converting your data to XML will take time on the application side. Running a single insert will take time on the database side.Converting the data to XML should take considerably less time than it would take to run all of the individual inserts on the database.This is something you'll have to benchmark to see which approach works best for your situation.
iKnowKungFoo