这儿存在一个问题。我每天都要更新和下载大量数据。约 140 mb。这些数据取自文件。这些加载时间很长的 9000 条记录加载了 10 分钟。尽管当我使用自定义 sql server 时数据加载速度更快。
下面是我加载数据的代码:
var InBase = FrmMain.allRecords.ToList();
var allcats = FrmMain.allCats;
int curCategory = 0;
for (int jrCnt = rCnt; jrCnt <= arrayTable.GetUpperBound(0); jrCnt++)
{
while (operations.Count(x => x.IsAlive) >= 100) ;
var prcI = new Price();
if (arrayTable[jrCnt, nametov] != null)
prcI.name = arrayTable[jrCnt, nametov].ToString();
if (productsInBase.FirstOrDefault(x => x.name == prcI.name) != null)
{
var finded = productsInBase.FirstOrDefault(x => x.name == prcI.name && x.company==company);
prcI.ID = finded.ID;
}
if (arrayTable[jrCnt, pricetov] != null)
{
decimal parsdec;
if (decimal.TryParse(arrayTable[jrCnt, pricetov].ToString(), out parsdec))
prcI.prc = parsdec;
}
prcI.category = curCategory;
if (!string.IsNullOrEmpty(prcI.name) && prcI.prc == 0)
{
var cat =
allcats.FirstOrDefault(
x =>
x.findname != "NaN" &&
x.findname.ToUpper().Split(';').Any(prcI.name.ToUpper().Contains));
curCategory = cat == null ? 0 : cat.id;
}
if ((string.IsNullOrEmpty(prcI.name)) || (prcI.prc == 0)) continue;
Products.Add(prcI);
if (count == 0 || count % 200 != 0 && jrCnt != arrayTable.GetUpperBound(0)) continue;
int start = count >= prodInTh ? count % prodInTh != 0 ? (count - count % prodInTh) : (count - prodInTh) : 0;
int end = count % prodInTh != 0 ? (count % prodInTh) : prodInTh;
var productsForThreadUpd = Products.GetRange(start, end).Where(x => x.ID != 0).ToList();
var addprod = Products.GetRange(start, end).Where(x => x.ID == 0).ToList();
if (productsForThreadUpd.Count > 0)
{
var newTh = new Thread(() => _mainClass.AddProductsUpdateProduct(productsForThreadUpd))
{
Name = company + start + " - " + (start + end) + " UPDATE"
};
newTh.Start();
operations.Add(newTh);
}
if (addprod.Count > 0)
{
var newTh = new Thread(() => _mainClass.AddProductsUpdateProduct(addprod))
{
Name = company + start + " - " + (start + end) + " ADD"
};
newTh.Start();
operations.Add(newTh);
}
}
我分担了线程的负载。在 obnom 流中,我的 200 个条目。启动代码数据:
public void AddProductsUpdateProduct(List<Price> price)
{
using (var dcupdateoradd = new PriceDataContext())
{
if (price.Any(x => x.ID != 0))
{
var upds = price.Where(x => x.ID != 0).ToList();
dcupdateoradd.Price.AttachAll(upds);
dcupdateoradd.Refresh(RefreshMode.KeepCurrentValues, upds);
}
dcupdateoradd.Price.InsertAllOnSubmit(price.Where(x => x.ID == 0));
dcupdateoradd.SubmitChanges();
}
}
还不知道我不喜欢该程序连接数据库的程序多少次,但在您想出之前什么都没有。
内存负载:
谢谢!