約 1'000'000 行を生成して 2 つのテーブルに挿入するタスクがあります。難しいのは、既存の外部キーを挿入する必要があるため、最初に他のテーブルからデータを取得することです...さらに、1000000 行の新しい DataTable を生成して SqlBulk で貼り付けようとするより...しかし、この世代は時間がかかりすぎます (~毎分 4000 行!) これがコードです。最適化を手伝ってくれる人がいるかもしれません...
public void GenerateData(DateTime fromDate, DateTime toDate, int Size)
{
DataTable theDatetime = new DataTable();
theDatetime.Columns.Add("Datetime", new DateTime().GetType());
theDatetime.Columns.Add("Date", new DateTime().GetType());
theDatetime.Columns.Add("Hour", new Int32().GetType());
DataTable theOverspeed = new DataTable();
theOverspeed.Columns.Add("CarNumber", new Int32().ToString().GetType());
theOverspeed.Columns.Add("Datetime", new DateTime().GetType());
theOverspeed.Columns.Add("DistrictCode", new Int32().GetType());
theOverspeed.Columns.Add("MarkCode", new Int32().GetType());
theOverspeed.Columns.Add("OwnerCode", new Int32().GetType());
theOverspeed.Columns.Add("RecognitorNumber", new Int32().GetType());
theOverspeed.Columns.Add("Region", new Int32().GetType());
theOverspeed.Columns.Add("RouteCode", new Int32().GetType());
theOverspeed.Columns.Add("Overspeed", new Int32().GetType());
theOverspeed.Columns.Add("Speed", new Int32().GetType());
Dictionary<DateTime, DateTime> Dates = new Dictionary<DateTime, DateTime>();
Random TheGenerator = new Random();
DataTable theCars = getCars();
DataTable theRecognitors = getRecognitors();
int[] sizes = new int[5];
for(int step = 0; step < 5; ++step)
{
DateTime Current1 = GetFifthDate(fromDate, toDate)[step];
DateTime Current2 = GetFifthDate(fromDate, toDate)[step+1];
int CurrentCount = GetFifthCount(Size)[step];
for (int i = 0; i < CurrentCount; ++i)
{
var aDate = GetRandomDate(Current1, Current2);
if (!Dates.ContainsKey(aDate))
{
InsertFact(theCars, aDate, theRecognitors,
theDatetime, theOverspeed);
Dates.Add(aDate, aDate);
}
else
{
i--;
}
}
}
そして InsertFact メソッド
private void InsertFact(DataTable theCars,
DateTime theDate,
DataTable theRecognitor, DataTable theDatetime, DataTable theOverspeed)
{
Random theGenerator = new Random(DateTime.Now.Millisecond);
DataRow rowDate = theDatetime.NewRow();
rowDate["DateTime"] = theDate;
rowDate["Date"] = theDate.Date;
rowDate["Hour"] = theDate.Hour;
theDatetime.Rows.Add(rowDate);
int car = theGenerator.Next(theCars.Rows.Count);
int recognitor = theGenerator.Next(theRecognitor.Rows.Count);
DataRow rowOverspeed = theOverspeed.NewRow();
rowOverspeed["CarNumber"] = theCars.Rows[car][4];
rowOverspeed["Datetime"] = theDate;
rowOverspeed["DistrictCode"] = theRecognitor.Rows[recognitor][3];
rowOverspeed["MarkCode"] = theCars.Rows[car][3];
rowOverspeed["OwnerCode"] = theCars.Rows[car][2];
rowOverspeed["RecognitorNumber"] = theRecognitor.Rows[recognitor][2];
rowOverspeed["Region"] = theCars.Rows[car][1];
rowOverspeed["RouteCode"] = theRecognitor.Rows[recognitor][1];
rowOverspeed["Overspeed"] = theGenerator.Next(10, 40);
rowOverspeed["Speed"] = theGenerator.Next(100) > 40 ? 70 : 90;
theOverspeed.Rows.Add(rowOverspeed);}
GetRecognitors
private DataTable getRecognitors()
{
DataTable Result = new DataTable("Recognitors");
using (SqlConnection theConnection = new SqlConnection(str))
{
theConnection.Open();
SqlDataAdapter theAdapter = new SqlDataAdapter();
SqlCommand theCommand = new SqlCommand("Select * from Recognitor", theConnection);
theCommand.CommandType = System.Data.CommandType.Text;
theCommand.CommandTimeout = 0;
theAdapter.SelectCommand = theCommand;
theAdapter.Fill(Result);
}
return Result;
}
GetCars
private DataTable getCars()
{
DataTable Result = new DataTable("Cars");
using (SqlConnection theConnection = new SqlConnection(str))
{
theConnection.Open();
SqlDataAdapter theAdapter = new SqlDataAdapter();
SqlCommand theCommand = new SqlCommand("Select * from Car", theConnection);
theCommand.CommandType = System.Data.CommandType.Text;
theCommand.CommandTimeout = 0;
theAdapter.SelectCommand = theCommand;
theAdapter.Fill(Result);
}
return Result;
}