I need to run a stored procedure for every item in a list (from an ASP .NET app). I have a framework of services which allow me to do this with minimal lines of code, but I’m afraid using that method will be to inefficient. Each time I call the service method, a database connection is opened, the stored procedure is run, and then the database connection is closed. It’s safe and all, but calling that for 20 items might be a bit slow, and I need this to be as fast as possible.
What would be a more efficient way of doing this?
Well, if you are using ADO.Net, then even though you are “closing” the connection The ADO.Net engine by default only puts the connection back into it’s cache (connection pool) to be reused, so it isn’t quite as expensive as you think…
But, if you want to avoid the round trip for each item, pass all the items in a pipe delimited list (or some other technique), and write a stored procedure that will process them all at once in a single set-based operation. That will get you the best performance.
Do you know that there is a performance issue here at all?