在我的ASP.NET MVC應用程序中,我正在運行一些插入,它可以插入10000行或更多行並更新其他幾行。這個過程需要很長時間,但我無法逃避插入,因爲這正是我被要求做的。 現在我正在運行Sql Server Profiler,它需要將近20分鐘才能插入這一堆行。我怎麼可能改善這一行動的表現?插入語句的性能改進
(我使用LINQ到SQL來將數據插入到數據庫。)
這是方法做的代碼插入數據:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult SaveEvent(int id)
{
int eventID= 0;
var query = from q in context.InventoryGoods
where q.ParentId == id && q.Action.HasValue && q.ActionOn.HasValue == false
select q;
var stockType = from q in context.Inventory
where q.Id == id
select q.StockType;
if (query.Count() > 0)
{
foreach (var i in query)
{
switch (i.Action.Value)
{
case (int)InventoryGoodsActionEnum.AdjustLocation:
Guid guid = Guid.NewGuid();
using (var scope = new TransactionScope())
{
GoodsEvent ge = new GoodsEvent()
{
Gid = i.Gid,
Guid = guid,
CreatedOn = DateTime.Now,
EventOn = DateTime.Now,
Type = "LO",
Lid = i.LidObtained,
Comments = "Inventário "+i.ParentId,
UserId = GetUserId(),
};
context.GoodsEvent.InsertOnSubmit(ge);
context.SubmitChanges();
eventID = ge.Id;
Repository.SetActionOn(i.Id, eventID);
scope.Complete();
}
break;
case (int)InventoryGoodsActionEnum.AdjustQuantity:
if (!i.QuantityObtained.HasValue)
{
guid = Guid.NewGuid();
using (var scope = new TransactionScope())
{
GoodsEvent ge = new GoodsEvent()
{
Gid = i.Gid,
Guid = guid,
CreatedOn = DateTime.Now,
EventOn = DateTime.Now,
Type = "AQ",
Quantity = (short)(i.QuantityExpected * -1),
Comments = "Inventário " + i.ParentId,
UserId = GetUserId(),
};
context.GoodsEvent.InsertOnSubmit(ge);
context.SubmitChanges();
eventID = ge.Id;
Repository.SetActionOn(i.Id, eventID);
scope.Complete();
}
}
else if ((i.QuantityObtained - (i.QuantityExpected.HasValue ? i.QuantityExpected : 0) != 0))
{
guid = Guid.NewGuid();
using (var scope = new TransactionScope())
{
GoodsEvent ge = new GoodsEvent()
{
Gid = i.Gid,
Guid = guid,
CreatedOn = DateTime.Now,
EventOn = DateTime.Now,
Type = "AQ",
Quantity = (short)(i.QuantityObtained.Value - (i.QuantityExpected.HasValue ? i.QuantityExpected : 0)),
Comments = "Inventário " + i.ParentId,
UserId = GetUserId(),
};
context.GoodsEvent.InsertOnSubmit(ge);
context.SubmitChanges();
eventID = ge.Id;
Repository.SetActionOn(i.Id, eventID);
scope.Complete();
}
}
break;
case (int)InventoryGoodsActionEnum.AdjustQuantityLocation:
guid = Guid.NewGuid();
using (var scope = new TransactionScope())
{
GoodsEvent ge = new GoodsEvent()
{
Gid = i.Gid,
Guid = guid,
CreatedOn = DateTime.Now,
EventOn = DateTime.Now,
Type = "LO",
Lid = i.LidExpected,
Comments = "Inventário " + i.ParentId,
UserId = GetUserId(),
};
context.GoodsEvent.InsertOnSubmit(ge);
context.SubmitChanges();
eventID = ge.Id;
Repository.SetActionOn(i.Id, eventID);
scope.Complete();
}
if (!i.QuantityObtained.HasValue)
{
guid = Guid.NewGuid();
using (var scope = new TransactionScope())
{
GoodsEvent ge = new GoodsEvent()
{
Gid = i.Gid,
Guid = guid,
CreatedOn = DateTime.Now.AddSeconds(1),
EventOn = DateTime.Now.AddSeconds(1),
Type = "AQ",
Quantity = (short)(i.QuantityExpected * -1),
Comments = "Inventário " + i.ParentId,
UserId = GetUserId(),
};
context.GoodsEvent.InsertOnSubmit(ge);
context.SubmitChanges();
eventID = ge.Id;
Repository.SetActionOn(i.Id, eventID);
scope.Complete();
}
}
else if ((i.QuantityObtained - (i.QuantityExpected.HasValue ? i.QuantityExpected : 0) != 0))
{
guid = Guid.NewGuid();
using (var scope = new TransactionScope())
{
GoodsEvent ge = new GoodsEvent()
{
Gid = i.Gid,
Guid = guid,
CreatedOn = DateTime.Now.AddSeconds(1),
EventOn = DateTime.Now.AddSeconds(1),
Type = "AQ",
Quantity = (short)(i.QuantityObtained.Value - (i.QuantityExpected.HasValue ? i.QuantityExpected : 0)),
Comments = "Inventário " + i.ParentId,
UserId = GetUserId(),
};
context.GoodsEvent.InsertOnSubmit(ge);
context.SubmitChanges();
eventID = ge.Id;
Repository.SetActionOn(i.Id, eventID);
scope.Complete();
}
}
break;
case (int)InventoryGoodsActionEnum.AdjustStockType:
guid = Guid.NewGuid();
using (var scope = new TransactionScope())
{
GoodsEvent ge = new GoodsEvent()
{
Gid = i.Gid,
Guid = guid,
CreatedOn = DateTime.Now,
EventOn = DateTime.Now,
Type = "ST",
StockType = stockType.First().Value,
Comments = "Inventário " + i.ParentId,
UserId = GetUserId(),
};
context.GoodsEvent.InsertOnSubmit(ge);
context.SubmitChanges();
eventID = ge.Id;
Repository.SetActionOn(i.Id, eventID);
scope.Complete();
}
break;
case (int)InventoryGoodsActionEnum.AdjustLocationStockType:
guid = Guid.NewGuid();
using (var scope = new TransactionScope())
{
GoodsEvent ge = new GoodsEvent()
{
Gid = i.Gid,
Guid = guid,
CreatedOn = DateTime.Now,
EventOn = DateTime.Now,
Type = "ST",
StockType = stockType.First().Value,
Comments = "Inventário " + i.ParentId,
UserId = GetUserId(),
};
context.GoodsEvent.InsertOnSubmit(ge);
context.SubmitChanges();
eventID = ge.Id;
Repository.SetActionOn(i.Id, eventID);
scope.Complete();
}
guid = Guid.NewGuid();
using (var scope = new TransactionScope())
{
GoodsEvent ge = new GoodsEvent()
{
Gid = i.Gid,
Guid = guid,
CreatedOn = DateTime.Now.AddSeconds(1),
EventOn = DateTime.Now.AddSeconds(1),
Type = "LO",
Lid = i.LidExpected,
Comments = "Inventário " + i.ParentId,
UserId = GetUserId(),
};
context.GoodsEvent.InsertOnSubmit(ge);
context.SubmitChanges();
eventID = ge.Id;
Repository.SetActionOn(i.Id, eventID);
scope.Complete();
}
break;
case (int)InventoryGoodsActionEnum.AdjustQuantityStockType:
guid = Guid.NewGuid();
using (var scope = new TransactionScope())
{
GoodsEvent ge = new GoodsEvent()
{
Gid = i.Gid,
Guid = guid,
CreatedOn = DateTime.Now,
EventOn = DateTime.Now,
Type = "ST",
StockType = stockType.First().Value,
Comments = "Inventário " + i.ParentId,
UserId = GetUserId(),
};
context.GoodsEvent.InsertOnSubmit(ge);
context.SubmitChanges();
eventID = ge.Id;
Repository.SetActionOn(i.Id, eventID);
scope.Complete();
}
if (!i.QuantityObtained.HasValue)
{
guid = Guid.NewGuid();
using (var scope = new TransactionScope())
{
GoodsEvent ge = new GoodsEvent()
{
Gid = i.Gid,
Guid = guid,
CreatedOn = DateTime.Now.AddSeconds(1),
EventOn = DateTime.Now.AddSeconds(1),
Type = "AQ",
Quantity = (short)(i.QuantityExpected * -1),
Comments = "Inventário " + i.ParentId,
UserId = GetUserId(),
};
context.GoodsEvent.InsertOnSubmit(ge);
context.SubmitChanges();
eventID = ge.Id;
Repository.SetActionOn(i.Id, eventID);
scope.Complete();
}
}
else if ((i.QuantityObtained - i.QuantityExpected != 0))
{
guid = Guid.NewGuid();
using (var scope = new TransactionScope())
{
GoodsEvent ge = new GoodsEvent()
{
Gid = i.Gid,
Guid = guid,
CreatedOn = DateTime.Now.AddSeconds(1),
EventOn = DateTime.Now.AddSeconds(1),
Type = "AQ",
Quantity = (short)(i.QuantityObtained.Value - i.QuantityExpected),
Comments = "Inventário " + i.ParentId,
UserId = GetUserId(),
};
context.GoodsEvent.InsertOnSubmit(ge);
context.SubmitChanges();
eventID = ge.Id;
Repository.SetActionOn(i.Id, eventID);
scope.Complete();
}
}
break;
case (int)InventoryGoodsActionEnum.AdjustQuantityLocationStockType:
guid = Guid.NewGuid();
using (var scope = new TransactionScope())
{
GoodsEvent ge = new GoodsEvent()
{
Gid = i.Gid,
Guid = guid,
CreatedOn = DateTime.Now,
EventOn = DateTime.Now,
Type = "ST",
StockType = stockType.First().Value,
Comments = "Inventário " + i.ParentId,
UserId = GetUserId(),
};
context.GoodsEvent.InsertOnSubmit(ge);
context.SubmitChanges();
eventID = ge.Id;
Repository.SetActionOn(i.Id, eventID);
scope.Complete();
}
if (!i.QuantityObtained.HasValue)
{
guid = Guid.NewGuid();
using (var scope = new TransactionScope())
{
GoodsEvent ge = new GoodsEvent()
{
Gid = i.Gid,
Guid = guid,
CreatedOn = DateTime.Now.AddSeconds(1),
EventOn = DateTime.Now.AddSeconds(1),
Type = "AQ",
Quantity = (short)(i.QuantityExpected * -1),
Comments = "Inventário " + i.ParentId,
UserId = GetUserId(),
};
context.GoodsEvent.InsertOnSubmit(ge);
context.SubmitChanges();
eventID = ge.Id;
Repository.SetActionOn(i.Id, eventID);
scope.Complete();
}
}
else if ((i.QuantityObtained - i.QuantityExpected != 0))
{
guid = Guid.NewGuid();
using (var scope = new TransactionScope())
{
GoodsEvent ge = new GoodsEvent()
{
Gid = i.Gid,
Guid = guid,
CreatedOn = DateTime.Now.AddSeconds(1),
EventOn = DateTime.Now.AddSeconds(1),
Type = "AQ",
Quantity = (short)(i.QuantityObtained.Value - i.QuantityExpected),
Comments = "Inventário " + i.ParentId,
UserId = GetUserId(),
};
context.GoodsEvent.InsertOnSubmit(ge);
context.SubmitChanges();
eventID = ge.Id;
Repository.SetActionOn(i.Id, eventID);
scope.Complete();
}
}
guid = Guid.NewGuid();
using (var scope = new TransactionScope())
{
GoodsEvent ge = new GoodsEvent()
{
Gid = i.Gid,
Guid = guid,
CreatedOn = DateTime.Now.AddSeconds(2),
EventOn = DateTime.Now.AddSeconds(2),
Type = "LO",
Lid = i.LidExpected,
Comments = "Inventário " + i.ParentId,
UserId = GetUserId(),
};
context.GoodsEvent.InsertOnSubmit(ge);
context.SubmitChanges();
eventID = ge.Id;
Repository.SetActionOn(i.Id, eventID);
scope.Complete();
}
break;
}
}
}
else
{
var lista = from q in context.InventoryGoods
where q.ParentId == id
select q;
Repository.EvaluateActions(lista.ToList());
SaveEvent(id);
}
using (var scope = new TransactionScope())
{
var thisInventory = from i in context.Inventory
where i.Id == id
select i;
thisInventory.First().State = (int)InventoryStateEnum.Verified;
context.SubmitChanges();
scope.Complete();
}
Status.Info(string.Format("Acções aplicadas com sucesso."));
return RedirectToAction("Details", new { id });
}
public void SetActionOn(int id, int eventID)
{
var InventoryGoods = from i in context.InventoryGoods
where i.Id == id
select i;
using (var scope = new TransactionScope())
{
InventoryGoods.First().ActionOn = DateTime.Now;
InventoryGoodsEvents ige = new InventoryGoodsEvents
{
EventId = eventID,
InventoryGood = InventoryGoods.First().Id,
};
context.InventoryGoodsEvents.InsertOnSubmit(ige);
scope.Complete();
}
}
這種方法是怎麼回事:Repository.SetActionOn(i.Id,eventID); – 2010-07-21 11:42:07
我會在我的主要帖子上張貼它。 – Hallaghan 2010-07-21 11:45:34