How bad is opening and closing a SQL connection for several times? What is the exact effect?

后端 未结 3 1089
独厮守ぢ
独厮守ぢ 2021-01-14 14:51

For example, I need to fill lots of DataTables with SQLDataAdapter\'s Fill() method:

DataAdapter1.Fill(DataTable1);
DataAdapter2.Fill(DataTable2);
DataAdapte         


        
3条回答
  •  感情败类
    2021-01-14 15:29

    Key Point:

    • Why open, keep and reuse connections?

      Performance

    There are many reasons that you may want to open and close connections. You have to decide where the best trade-off is for your use. You can do both: use an open connection for a period of time and/or a set number of transactions, then close it and open a new one.

    Opening and closing SQL connections is expensive when compared to other simple tasks in the database. But, if your actual task is already time consuming, the extra overhead may not be noticed (if you are already hiding the wait period of the actual task - so the user does not start randomly clicking things - like retry).

    Test Case:

    You can measure your difference by writing two versions of a test query. Select any simple SQL task (needs to be the same in each version).

    In version one, do it with a single constant open connection outside the loop, looping thru your simple task X number of times.

    In the second, do it with the opening and closing of the connection inside the loop.

    Change the X number of times to match your usage and expectations. That should give you a real good feel for the impact on your system.

    Hope that helps you understand the basics... Jack.

提交回复
热议问题