日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

EF的性能瓶颈

發布時間:2024/2/2 综合教程 38 生活家
生活随笔 收集整理的這篇文章主要介紹了 EF的性能瓶颈 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

通過EF以面向對象的方式操作數據庫帶來了一定的便利性,但是某些情況下不宜采用EF否則會遇到性能瓶頸。目前遇到的問題主要包括兩個方面:批量的DB操作、從DB帶出大數量計算再持久化。

1、批量的DB操作

EF對批量插入、更新等操作會通過構造多條SQL的方式傳輸給DB執行,當量大的時候會帶來傳輸及執行(執行方式甚至語句可優化)上的時間浪費。

(1)示例

foreach (var photo in photos)
{
	//添加樣本照片記錄
	var samplePhoto = new SamplePhoto()
	{
		SamplePhotoId = CommonBll.PR_GetSysKey(efContext, "SamplePhoto", "SamplePhotoId", venderId),
		PhotoId = photo.PhotoId,
		SourceId = sampleSourceInfo.SourceId,
		IsAllot = 0,
		JoinTime = now,
		VenderId = venderId
	};
	samplePhotoRepository.Add(samplePhoto);

(2)執行情況

foreach里的add產生多條執行的SQL

INSERT [dbo].[SamplePhoto]([SamplePhotoId], [PhotoId], [SourceId], [IsAllot], [JoinTime], [Height], [VenderId], [Width])
VALUES (@0, @1, @2, @3, @4, NULL, @5, NULL)
-- @0: '10001303' (Type = Int32)
-- @1: '54A7075D-1797-438A-B068-981D89B78AEB' (Type = String, Size = -1)
-- @2: '10001044' (Type = Int32)
-- @3: '0' (Type = Int32)
-- @4: '2018/10/9 7:19:10' (Type = DateTime2)
-- @5: '1000' (Type = Int32)
-- 正在 2018/10/9 7:21:10 +08:00
 執行
-- 已在 46 毫秒內完成,結果為: 1

INSERT [dbo].[SamplePhoto]([SamplePhotoId], [PhotoId], [SourceId], [IsAllot], [JoinTime], [Height], [VenderId], [Width])
VALUES (@0, @1, @2, @3, @4, NULL, @5, NULL)
-- @0: '10001304' (Type = Int32)
-- @1: '63B78093-A0F1-4E2B-8973-BED22164EAF1' (Type = String, Size = -1)
-- @2: '10001044' (Type = Int32)
-- @3: '0' (Type = Int32)
-- @4: '2018/10/9 7:19:10' (Type = DateTime2)
-- @5: '1000' (Type = Int32)
-- 正在 2018/10/9 7:21:10 +08:00
 執行
-- 已在 34 毫秒內完成,結果為: 1

INSERT [dbo].[SamplePhoto]([SamplePhotoId], [PhotoId], [SourceId], [IsAllot], [JoinTime], [Height], [VenderId], [Width])
VALUES (@0, @1, @2, @3, @4, NULL, @5, NULL)
-- @0: '10001305' (Type = Int32)
-- @1: '8F40A68C-0207-4000-BAB0-654ACCCDEA30' (Type = String, Size = -1)
-- @2: '10001044' (Type = Int32)
-- @3: '0' (Type = Int32)
-- @4: '2018/10/9 7:19:10' (Type = DateTime2)
-- @5: '1000' (Type = Int32)
-- 正在 2018/10/9 7:21:10 +08:00
 執行
-- 已在 38 毫秒內完成,結果為: 1

2、從DB帶出大數量計算再持久化

大數量量從DB傳遞到應用,計算后再從應用傳遞到DB,需要耗費大量的網絡資源,時間會消耗在傳輸上。

var sampleboxs = boxs.Where(T => T.SamplePhotoId == photo.SamplePhotoId).ToList();
if (sampleboxs != null)
{
	foreach (var box in sampleboxs)
	{
		var samplePhotoBox = new SamplePhotoBox()
		{
			//BoxId = CommonBll.PR_GetSysKey(efContext, "SamplePhotoBox", "BoxId", venderId),
			SamplePhotoId = samplePhoto.SamplePhotoId,
			XMax = box.XMax,
			XMin = box.XMin,
			YMax = box.YMax,
			YMin = box.YMin,
			SkuCode = box.SkuCode
		};
		samplePhotoBoxRepository.Add(samplePhotoBox);
	}
}

從DB帶出sampleboxs再逐個遍歷計算后Add到另一張表,如果sampleboxs的量足夠大的時候,這個邏輯的執行時間會花費幾十秒、幾分鐘...

 

3、替代方式

對于EF有可能出現性能瓶頸的地方可通過執行“存儲過程”或“參數化執行原生SQL”解決。

SqlParameter[] paras = new SqlParameter[3];
paras[0] = new SqlParameter("@inspectId", inspectId);
paras[1] = new SqlParameter("@userId", userId);
paras[2] = new SqlParameter("@venderId", venderId);

var result = efContext.Database.SqlQuery<string>(
	  "EXEC dbo.PR_NewSkuSetSample @inspectId,@userId,@venderId", paras).First();

  

總結

以上是生活随笔為你收集整理的EF的性能瓶颈的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。