Reputation: 18387
I have 3 stored procedures AddProduct
, AddProductProperties
and AddOffer
.
I need to import data from various datasources in database using this procedures. Structure of datasources is variable, so it may contains only products or offers or both.
My algorithm is to read record from data source and then call necessary procedures.
To call procedures I use MySqlCommand
with parameters.
Questions:
MySqlCommand
objects and change only parameters
or it doesn't matters? Upvotes: 0
Views: 386
Reputation: 6280
Speed and individual calls to inserts are like oil and water. (oh, not a good statement to use in these times).
Delete from temp2 where temp2.id in (select id from temp1)or simliar before import.
Once I even swung something like this where conflicting items later on in the file had to overwrite earlier items, with builk update/insert/delete queries and temp tables, from an external flatfile. Took 5+ huge ugly sql statements. Not fun but about 100x faster than code on the same box doing inserts. Which is the analoge of a single SP doing inserts (ok maybe not that bad, it was vb4 and access jet2.0:-). But, a networked client would do WORSE because of roundtrips, etc.
You can also cure cancer with Multi-record SQL updates.
Upvotes: 0
Reputation: 10280
Two suggestions:
Use the Prepare method on MySqlCommand
The MySqlCommand class has a Prepare method on it. If you have to call these stored procedures repeatedly, then I would use that in order to try to minimize the amount of work that has to be done per execution. Below is roughly the steps that I think would be involved:
Set up the command objects (one per stored procedure) including any necessary parameters.
Open the connection to the database
Call the Prepare method for each command once
Iterate over your data
4.1. Update the values of the command's parameters according to your data
4.2. Call the appropriate Execute method on the command's
I'm not sure how much performance benefit you will really get from this, though.
If you were executing ad-hoc statements instead of procedures (or perhaps even with procedures), then I think an import thing to note is to use parameters instead of simply concatenating your values into the text of the command. If you were to use the string concatenation approach, which I think is generally a bad idea in most situations anyway, then you might not be able to take advantage of the Prepare method, as you will have to modify the command for each execution.
Is there a BulkInsert/BulkCopy option available?
I'm not as familiar with MySQL as with SQL Server; ADO.Net has bulk copy functionality that you can use to insert a large amount of data into SQL Server. I don't know if similar functionality is available with MySQL, but if it is and you have a lot of data to insert, then consider using that instead of repeatedly calling stored procedures.
Upvotes: 2