내 ASP.NET MVC 애플리케이션에서 한 번에 몇 개의 삽입을 실행하여 10000 개 이상의 행을 삽입하고 몇 가지 다른 것들을 업데이트 할 수 있습니다. 이 과정은 오랜 시간이 걸리지 만 삽입 물을 피할 수는 없기 때문에 그것이 정확하게 요구되었습니다. 지금 SQL Server 프로필러를 실행 중이며이 행을 삽입하는 데 약 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); –
나는 그것을 내 메인 포스트에 게시 할 것이다. – Hallaghan