2

.netアプリケーションを介してSQLサーバーテーブルにレコードを挿入/更新するために使用する区切りファイルがあります。このファイルには約80000レコードがあり、毎日処理されます。私の質問:80000行のそれぞれをスピンしている間、データベースへの接続を開いたままにしておくのは安全ですか、それとも賢明ですか?それとも、ループを繰り返すたびに接続を閉じて再度開く必要がありますか?それ自体は面倒に聞こえます。しかし、接続を長時間開いたままにしたり、ロックをかけたり、メモリを不必要に使い果たしたりすることが心配です。これを行うためのよりスケーラブルで安全かつ賢明な方法は何でしょうか?

4

2 に答える 2

4

First, no you should not open/close the connection every row. For 80,000 rows, that will take forever and will just add to the overhead. You could consider batching the rows (reset the connection say every 10-500 rows). Fortunately, there is a better option:

Secondly, the proper way to insert/update that many rows into a database from a .Net application, is to use the SQLBulkCopy methods, and not the INSERT or UPDATE commands. You should use SQLBulkCopy to load the data rows into a holding/staging table, and then use a SQL Stored Procedure to do the Insert/Update to the actual table(s), en-mass.

If you are concerned about the sustained load of the SQLBulkCopy, it has batching options built-in.

Using this technique, the initial upload of data should be at least 5x faster, and the actual table Insert/Updates should only be a matter of seconds.

于 2013-02-11T20:28:48.840 に答える
2

私はかつてデータをインポートする必要がありました。しかし、私はそれにいくつかのミニビジネスルールを実行する必要がありました。また、私の要件では、できるだけ多くの行をインポートすることでしたが、何かが失敗した場合は、それをログに記録します(ただし、インポート全体を失敗させないでください)。

以下にサンプルを書きました。

http://granadacoder.wordpress.com/2009/01/27/bulk-insert-example-using-an-idatareader-to-strong-dataset-to-sql-server-xml/

N個のレコード(たとえば、N = 1000)を〜xmlとしてストアドプロシージャに渡します。

「スイートスポット」を見つけるには、Nを構成可能にする必要があります。しかし、一度に1つは遅すぎて、一度に80,000が多くのように見えます。1,000(行)x80「実行」....良い出発点です、私見。

したがって、インポートが「ダム」の場合は、以前に提案された「SQLBulkCopy」が最善の方法である可能性があります。しかし、チェックや検証がある場合は、私のサンプルが良い提案かもしれません。

.....。

別のオプション:

http://msdn.microsoft.com/en-us/library/ms162802.aspx bcp.exe

しかし、それは実際には「ドットネットコード」ではありません。

于 2013-02-11T20:40:22.473 に答える