C# DataTable數(shù)據(jù)遍歷優(yōu)化詳解
我們在進行開發(fā)時,會經(jīng)常使用DataTable來存儲和操作數(shù)據(jù),我發(fā)現(xiàn)在遍歷DataTable并對數(shù)據(jù)進行刪除和添加操作時速度非常慢,查閱相關(guān)資料并測試在添加主鍵后可以使遍歷和操作速度提高很多:
測試代碼,測試的是我們向取出來數(shù)據(jù)滿足Flag!=1條件的所有數(shù)據(jù)的后面添加一條數(shù)據(jù)(因為這條數(shù)據(jù)的一些字段值是根據(jù)前面的幾條滿足條件[“AccID='” + accID + “' AND Y='” + year + “' AND AbsID <= ” + absID;]數(shù)據(jù)的值累加得到的)所以需要進行整個DataTable的遍歷來計算添加:
public static void Test2() { Stopwatch watch = new Stopwatch(); using (DbConnection conn = SqlHelper.GetConnection("ConnectionString")) { using (SqlCommand cmd = new SqlCommand()) { watch.Start(); cmd.CommandText = string.Format(@" select ROW_NUMBER() OVER (Order by S.AccID,S.CurrID,S.AbsID,S.Flag)AS RowNum,S.* from Test S "); cmd.Connection = conn as SqlConnection; cmd.CommandTimeout = 60000; conn.Open(); DataTable table = ExecuteDataTable(cmd); watch.Stop(); Console.WriteLine("從數(shù)據(jù)庫取出數(shù)據(jù){0}條", table.Rows.Count); Stopwatch watch2 = new Stopwatch(); watch2.Start(); DataTable newTable = HandleAccYear(table,true); watch2.Stop(); Console.WriteLine("數(shù)據(jù){0},遍歷操作時間:毫秒:{1},秒:{2}", newTable.Rows.Count, watch2.ElapsedMilliseconds, watch2.ElapsedMilliseconds / 1000); } conn.Close(); } }
填充數(shù)據(jù)到DataTable的方法
public static DataTable ExecuteDataTable(SqlCommand cmd) { DataTable table = new DataTable(); SqlDataAdapter adaper = new SqlDataAdapter(cmd); adaper.Fill(table); return table; }
private static DataTable HandleAccYear(DataTable dt, bool isCurrency) { DataTable newdt = dt.Clone(); //不使用主鍵 //dt.PrimaryKey = new DataColumn[] { // dt.Columns["AccID"], // dt.Columns["Flag"], // dt.Columns["AbsID"], // dt.Columns["RowNum"], //}; if (dt.Rows.Count > 0) { object flag = null; foreach (DataRow row in dt.Rows) { flag = row["Flag"]; if (flag != null && !Helper.AreEqual(flag.ToString(), "1")) { DataRow newRow = newdt.NewRow(); DataRow sourceRow = newdt.NewRow(); sourceRow.ItemArray = row.ItemArray; newRow.ItemArray = row.ItemArray; string accID = row["AccID"].ToString(), year = row["Y"].ToString(), absID = row["AbsID"].ToString(); newRow["Flag"] = "5"; newRow["SumInfo"] = "測試數(shù)據(jù)"; string filter = "AccID='" + accID + "' AND Y='" + year + "' AND AbsID <= " + absID; if (!isCurrency) { filter = "AccID='" + accID + "'AND CurrID='" + row["CurrID"] + "' AND Y='" + year + "' AND AbsID <= " + absID; } DataRow[] selectRow = dt.Select(filter); double debitLC = 0, debitQty = 0, creditLC = 0, creditQty = 0, debitFC = 0, creditFC = 0; foreach (DataRow item in selectRow) { debitLC += ToDouble(item["YearDebitLC"]); debitQty += ToDouble(item["YearDebitQty"]); creditLC +=ToDouble(item["YearCreditLC"]); creditQty += ToDouble(item["YearCreditQty"]); if (!isCurrency) { debitFC += ToDouble(item["YearDebitFC"]); creditFC += ToDouble(item["YearCreditFC"]); } } newRow["CurDebitLC"] = debitLC; newRow["CurDebitQty"] = debitQty; newRow["CurCreditLC"] = creditLC; newRow["CurCreditQty"] = creditQty; //newRow["CurDebitLC"] = dt.Compute("Sum(YearDebitLC)", filter); //newRow["CurDebitQty"] = dt.Compute("Sum(YearDebitQty)", filter); //newRow["CurCreditLC"] = dt.Compute("Sum(YearCreditLC)", filter); //newRow["CurCreditQty"] = dt.Compute("Sum(YearCreditQty)", filter); if (!isCurrency) { //newRow["CurCreditFC"] = dt.Compute("Sum(YearCreditFC)", filter); //newRow["CurDebitFC"] = dt.Compute("Sum(YearDebitFC)", filter); newRow["CurCreditFC"] = creditFC; newRow["CurDebitFC"] = debitFC; } newdt.Rows.Add(sourceRow); newdt.Rows.Add(newRow); } else { DataRow sourceRow = newdt.NewRow(); sourceRow.ItemArray = row.ItemArray; newdt.Rows.Add(sourceRow); } } } return newdt; }
當(dāng)不使用主鍵進行遍歷計算插入相應(yīng)的值時所用時間竟然是這么多:
當(dāng)我使用同樣的方法,同樣的數(shù)據(jù)添加主鍵(即把HandleAccYear方法中不使用主鍵下面的注釋去掉后).進行遍歷計算等操作,得出的結(jié)果竟然有這么大的差別:
補充:C# DataTable數(shù)據(jù)量大,循環(huán)處理數(shù)據(jù)的時候優(yōu)化速度
相信大家用for循環(huán)datatable數(shù)據(jù)的不會太少,這個在數(shù)據(jù)量比較小的時候可以接受,但是數(shù)據(jù)量大的時候卻會造成CPU占用過高,甚至把電腦資源耗盡卡死至無限等待,
其實一些循環(huán)耗時的操作可以用線程池分塊來處理,這樣會減輕CPU很多壓力,好比食堂打飯,當(dāng)只有一個窗口的時候勢必等待的時間會非常的長,但是多開幾個窗口的時候卻大大提高效率,
C#中用線程池就可以做到,本來一開始的時候我用的是為每個區(qū)塊開一個線程,但是有一個問題就是開了那么多的線程沒辦法結(jié)束他們,后來我想到了線程池,
具體代碼如下:
int sid = dt.Rows.Count % 100 == 0 ? (dt.Rows.Count / 100) : (dt.Rows.Count / 100 + 1); for (int a = 1; a <= sid; a++) { object aa=a.ToString() + "," + sid.ToString(); ThreadPool.QueueUserWorkItem(todo , aa); } public void todo(object aa) { string sql = ""; int startindex = Convert.ToInt32(aa.ToString().Split(',')[0]); int limitstep = Convert.ToInt32(aa.ToString().Split(',')[1]); for (int i = (startindex > 1 ? ((startindex - 1) * 100) : 0); i < (startindex == limitstep ? (dt.Rows.Count) : startindex*100); i++) { //todo數(shù)據(jù)操作 } Thread.Sleep(2000); }
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。