倧éã®ã¬ã³ãŒãïŒ10³以äžïŒãè¿œå /å€æŽããå ŽåãEntity Frameworkã®ããã©ãŒãã³ã¹ã«ã¯å€ãã®èŠæãæ®ããŸãã ãã®çç±ã¯ããã¬ãŒã ã¯ãŒã¯èªäœã®ã¢ãŒããã¯ãã£äžã®æ©èœãšæé©ã§ãªãçæãããSQLã®äž¡æ¹ã§ãã å
ãèŠæ®ããŠ-ã³ã³ããã¹ãããã€ãã¹ããŠããŒã¿ãä¿åãããšãå®è¡æéã倧å¹
ã«ççž®ãããŸãã
èšäºã®å
容ïŒ
1.æšæºã®Entity FrameworkããŒã«ã«ããæ¿å
¥/æŽæ°
2.åé¡ã®è§£æ±ºçãèŠã€ãã
3. Entity FrameworkãšSqlBulkCopyã®çµ±å
4. MERGEã䜿çšããé«åºŠãªæ¿å
¥
5.ããã©ãŒãã³ã¹ã®æ¯èŒ
6.çµè«
1.æšæºã®Entity FrameworkããŒã«ã«ããæ¿å
¥/æŽæ°
æ¿å
¥ããå§ããŸãããã ããŒã¿ããŒã¹ã«æ°ããã¬ã³ãŒããè¿œå ããæšæºçãªæ¹æ³ã¯ããããã³ã³ããã¹ãã«è¿œå ããŠä¿åããããšã§ãã
context.Orders.Add(order); context.SaveChanges();
Addã¡ãœãããåŒã³åºã
ãã³ã«ãå
éš
DetectChangesã¢ã«ãŽãªãºã ã®å®è¡ã³ã¹ããé«ã
ãªããŸãã ãã®ã¢ã«ãŽãªãºã ã¯ãã³ã³ããã¹ãå
ã®ãã¹ãŠã®ãšã³ãã£ãã£ãã¹ãã£ã³ããåããããã£ã®çŸåšã®å€ãã³ã³ããã¹ãã«ä¿åãããŠããå
ã®å€ãšæ¯èŒãããšã³ãã£ãã£éã®é¢ä¿ãªã©ãæŽæ°ããŸãã EF 6ã®ãªãªãŒã¹ãŸã§é¢é£ããããã©ãŒãã³ã¹ãæ¹åããããã®æ¢ç¥ã®æ¹æ³ã¯ãã³ã³ããã¹ãã«ãšã³ãã£ãã£ãè¿œå ããéã
DetectChangesã
ç¡å¹ã«ããããšã§ãã
context.Configuration.AutoDetectChangesEnabled = false; orders.ForEach(order => context.Orders.Add(order)); context.Configuration.AutoDetectChangesEnabled = true; context.SaveChanges();
ãŸããã³ã³ããã¹ããç¶æãã
ããã«ç€ºãããã«Nåããšã«æ°ãããªããžã§ã¯ããäœæããªãããäœäžãã®ãªããžã§ã¯ããã³ã³ããã¹ãã«ä¿æããã«ããŒã¿ããããã¯ã«ä¿åããããšããå§ãã
ãŸã ã æåŸã«ãEF 6ã§ã¯ã
Add + AutoDetectChangesEnabledãã³ãã«ã¬ãã«ã«ããã©ãŒãã³ã¹ãåäžãããæé©åããã
AddRangeã¡ãœããã
ç»å ŽããŸããã
context.Orders.AddRange(orders); context.SaveChanges();
æ®å¿µãªãããäžèšã®ã¢ãããŒãã¯äž»ãªåé¡ã解決ããŸãããã€ãŸããããŒã¿ããŒã¹ã«ããŒã¿ãä¿åãããšãã
æ°ããã¬ã³ãŒãããšã«åå¥ã®INSERTèŠæ±ãçæãããŸãïŒSQL INSERT [dbo].[Order]([Date], [Number], [Text]) VALUES (@0, @1, NULL)
æŽæ°ã§ã¯ãç¶æ³ã¯äŒŒãŠããŸãã 次ã®ã³ãŒãïŒ
var orders = context.Orders.ToList();
å€æŽããããªããžã§ã¯ãããšã«åå¥ã®SQLã¯ãšãªãå®è¡ãããŸãã
SQL UPDATE [dbo].[Order] SET [Text] = @0 WHERE ([Id] = @1)
æãåçŽãªã±ãŒã¹ã§ã¯ã
EntityFramework.Extendedã圹ç«ã€å ŽåããããŸãã
ãã®ã³ãŒãã¯ã³ã³ããã¹ãããã€ãã¹ãã1ã€ã®SQLã¯ãšãªãçæããŸãã EFã®é床ãšãã®ã©ã€ãã©ãªã®æäœã®
詳现ã«ã€ããŠã¯ã tp7ã åç
§ããŠãã ãã ã æããã«ããœãªã¥ãŒã·ã§ã³ã¯æ®éçã§ã¯ãªããåãå€ã®ãã¹ãŠã®ã¿ãŒã²ããè¡ãžã®æžã蟌ã¿ã«ã®ã¿é©ããŠããŸãã
2.åé¡ã®è§£æ±ºçãèŠã€ãã
ããã€ã¯ããæžãããšã«å¯Ÿããå·persistentãªå«æªæãæããŠãç§ã¯æåã«EFã䜿çšããè€æ°æ¿å
¥ã®ãã¹ããã©ã¯ãã£ã¹ãæ¢ããŸããã å
žåçãªã¿ã¹ã¯ã®ããã«æããŸããããç®±ããåºããŠãé©åãªãœãªã¥ãŒã·ã§ã³ãèŠã€ãããŸããã§ããã åæã«ãSQL Serverã¯ã
bcpãŠãŒãã£ãªãã£ã
SqlBulkCopyã¯ã©ã¹ãªã©ãããŒã¿ããã°ããæ¿å
¥ããããã®å€ãã®ææ³ãæäŸããŸãã åŸè
ã«ã€ããŠã¯ä»¥äžã§èª¬æããŸãã
System.Data.SqlClient.SqlBulkCopyã¯ã倧éã®ããŒã¿ãSQL ServerããŒãã«ã«æžã蟌ãããã«èšèšãããADO.NETã®ã¯ã©ã¹ã§ãã ããŒã¿ãœãŒã¹ãšããŠã
DataRow [] ã
DataTable ããŸãã¯
IDataReaderå®è£
ã䜿çšã§ããŸãã
ã§ããããšïŒ
- ãã©ã³ã¶ã¯ã·ã§ã³ãµããŒãã䜿çšããŠããŒã¿ããµãŒããŒãããã¯ã«éä¿¡ããŸãã
- DataTableããããŒã¿ããŒã¹ããŒãã«ãžã®åãããã³ã°ãå®è¡ããŸãã
- å¶çŽãæ¿å
¥æã®å€éšããŒãç¡èŠããŸãïŒãªãã·ã§ã³ïŒã
çæïŒ
- ååæ¿å
¥ïŒãªãã·ã§ã³ïŒ;
- äŸå€ãçºçããåŸã«äœæ¥ãç¶ç¶ã§ããªãã
- 匱ããšã©ãŒåŠçæ©èœã
JeanLouisã® èšäºã§ã¯ã©ã¹ã®è©³çŽ°ãèªãããšãã§ããŸãããããã§ã¯ç·æ¥ã®åé¡ãã€ãŸã
SqlBulkCopyãšEFã®çµ±åã®æ¬ åŠã«ã€ããŠæ€èšããŸãã ãã®ãããªåé¡ã解決ãã確ç«ãããã¢ãããŒãã¯ãããŸãããã次ã®ãããªããŸããŸãªçšåºŠã®é©åæ§ã®ãããžã§ã¯ããããã€ããããŸãã
EntityFramework.BulkInsertå®éãããã¯åäœäžèœã§ããããšãå€æããŸããã 課é¡ãå匷ããŠ
ãããšãæªåé«ããžã¥ãªãŒã»ã©ãŒãã³ãšã®
è°è«ã«åºäŒããŸãããç§ã®åé¡ãšäŒŒãŠããããããžã§ã¯ãã®äœè
ãçããŠããªãåé¡ã«ã€ããŠèª¬æããŠããŸãã
EntityFramework.Utilitiesã©ã€ããããžã§ã¯ããã¢ã¯ãã£ããªã³ãã¥ããã£ã Database FirstãµããŒãã¯ãããŸããããè¿œå ããããšãçŽæããŸãã
Entity Frameworkã®æ¡åŒµ300ãã«ã
3. Entity FrameworkãšSqlBulkCopyã®çµ±å
ãã¹ãŠèªåã§ããããšããŸãããã æãåçŽãªã±ãŒã¹ã§ã¯ã
SqlBulkCopyã䜿çšããŠãªããžã§ã¯ãã®ã³ã¬ã¯ã·ã§ã³ããããŒã¿ãæ¿å
¥ãããšã次ã®ããã«ãªããŸãã
ãªããžã§ã¯ãã®ã³ã¬ã¯ã·ã§ã³ã«åºã¥ããŠ
IDataReaderãå®è£
ããã¿ã¹ã¯èªäœã¯ç°¡åãªã®ã§ã
ãªã³ã¯ã«éå®ããŠã
SqlBulkCopyã䜿çšããŠæ¿å
¥äžã«ãšã©ãŒãåŠçããæ¹æ³ã説æããŸãã ããã©ã«ãã§ã¯ãããŒã¿ã¯ç¬èªã®ãã©ã³ã¶ã¯ã·ã§ã³ã«æ¿å
¥ãããŸãã äŸå€ãçºçãããšã
SqlExceptionãã¹ããŒãããããŒã«ããã¯ãçºçããŸãã ããŒã¿ããŒã¹å
ã®ããŒã¿ã¯ãŸã£ããæžã蟌ãŸããŸããã ãŸãããã®ã¯ã©ã¹ã®ããã€ãã£ãããšã©ãŒã¡ãã»ãŒãžã¯æ
å ±äŸ¡å€ããªããšã¯èšããŸããã ããšãã°ã
SqlException.AdditionalInformationãå«ãå¯èœæ§ããããã®ã¯
次ã®ãšããã§ãã
ããŒã¿ãœãŒã¹ã®Stringåã®æå®ãããå€ã¯ãæå®ãããã¿ãŒã²ããåã®nvarcharåã«å€æã§ããŸããã
ãŸãã¯ïŒ
SqlDateTimeãªãŒããŒãããŒã 1/1/1753 12:00:00 AMãã12/31/9999 11:59:59 PMã®éã§ãªããã°ãªããŸããã
æ®å¿µãªããã
SqlBulkCopyã¯å€ãã®å Žåããšã©ãŒã®åå ãšãªã£ãè¡/ãšã³ãã£ãã£ãäžæã«èå¥ããæ
å ±ãæäŸããŸããã ãã1ã€ã®äžå¿«ãªæ©èœã¯ããã©ã€ããªããŒã«éè€ã¬ã³ãŒããæ¿å
¥ããããšãããšã
SqlBulkCopyãäŸå€
ãã¹ããŒããŠçµäºããç¶æ³ãåŠçããŠå®è¡ãç¶ç¶ããæ©äŒãæäŸããªãããšã§ãã
ãããã³ã°æ£ããçæããããšã³ãã£ãã£ãšããŒã¿ããŒã¹ã®å Žåãããã«ããããã«ãåã®ã³ã³ãã©ã€ã¢ã³ã¹ãŸãã¯ããŒãã«å
ã®ãã£ãŒã«ãã®é·ãã®ãã§ãã¯ã¯ç¡é¢ä¿ã«ãªã
ãŸã ã
SqlBulkCopy.ColumnMappingsããããã£ãä»ããŠå®è¡ãããåãããã³ã°ãåŠçããæ¹ã䟿å©ã§ãã
ããŒã¿ãœãŒã¹ãšå®å
ããŒãã«ã®åæ°ãåãã§ãããŒã¿ãœãŒã¹ã®åãœãŒã¹åã®éå§äœçœ®ã察å¿ããå®å
åã®éå§äœçœ®ã«å¯Ÿå¿ããå ŽåãColumnMappingsã³ã¬ã¯ã·ã§ã³ã¯å¿
èŠãããŸããã ãã ããåã®æ°ãŸãã¯é åºãç°ãªãå ŽåãColumnMappingsã䜿çšããŠãåéã®ããŒã¿ãæ£ããã³ããŒãããããã«ããå¿
èŠããããŸãã
EFã®å Žå99ïŒ
ã®å Žåã
ColumnMappingsãæ瀺çã«èšå®ããå¿
èŠããããŸãïŒããã²ãŒã·ã§ã³ããããã£ãšè¿œå ã®ããããã£ã®ããïŒã ããã²ãŒã·ã§ã³ããããã£ã¯ãReflectionã§é€å€ã§ããŸãã
ãããã³ã°çšã®ããããã£åãååŸãã var columns = typeof(Order).GetProperties() .Where(property => property.PropertyType.IsValueType || property.PropertyType.Name.ToLower() == "string") .Select(property => property.Name) .ToList();
ãã®ãããªã³ãŒãã¯ãè¿œå ã®ããããã£ãªãã§POCOã¯ã©ã¹ã«é©åããŸãããã以å€ã®å Žåã¯ããæåå¶åŸ¡ãã«åãæ¿ããå¿
èŠããããŸãã ããŒãã«ã¹ããŒãã®ååŸãéåžžã«ç°¡åã§ãã
ããŒãã«ã¹ããŒããèªã private static List<string> GetColumns(SqlConnection connection) { string[] restrictions = { null, null, "<TableName>", null }; var columns = connection.GetSchema("Columns", restrictions) .AsEnumerable() .Select(s => s.Field<String>("Column_Name")) .ToList(); return columns; }
ããã«ããããœãŒã¹ã¯ã©ã¹ãšã¿ãŒã²ããããŒãã«ãæåã§ãããã³ã°ã§ããŸãã
SqlBulkCopy.BatchSizeããããã£ãšSqlBulkCopyOptionsã¯ã©ã¹ã®äœ¿çšSqlBulkCopy.BatchSize ïŒ
ããããµã€ãº | åããã±ãŒãžã®è¡æ°ã åãã±ããã®çµããã«ãããã«å«ãŸããè¡æ°ããµãŒããŒã«éä¿¡ãããŸãã |
SqlBulkCopyOptions-åæïŒ
ã¡ã³ããŒå | 説æ |
---|
ãã§ãã¯å¶çŽ | ããŒã¿ãæ¿å
¥ãããšãã®å¶éã確èªããŠãã ããã ããã©ã«ãã§ã¯ãå¶éã¯ãã§ãã¯ãããŠããŸããã |
ããã©ã«ã | ãã¹ãŠã®ãã©ã¡ãŒã¿ãŒã«ããã©ã«ãå€ã䜿çšããŸãã |
ãã¡ã€ã¢ããªã¬ãŒ | ãã®èšå®ãæå®ãããšããµãŒããŒã¯ããŒã¿ããŒã¹ã«æ¿å
¥ãããè¡ã®æ¿å
¥ããªã¬ãŒãåŒã³åºããŸãã |
KeepIdentity | ãœãŒã¹èå¥å€ãä¿åããŸãã ãã®èšå®ãæå®ãããŠããªãå Žåãèå¥å€ã¯ã¿ãŒã²ããããŒãã«ã«ãã£ãŠå²ãåœãŠãããŸãã |
Keepnulls | ããã©ã«ãå€ã®èšå®ã«é¢ä¿ãªããã¿ãŒã²ããããŒãã«ã«NULLå€ãæ ŒçŽããŸãã ãã®èšå®ãæå®ãããŠããªãå Žåãå¯èœãªå Žåã¯ãã«å€ãããã©ã«ãå€ã«çœ®ãæããããŸãã |
ããŒãã«ãã㯠| äžæ¬ã³ããŒæäœäžã«äžæ¬æŽæ°ããã¯ãååŸããŸãã ãã®èšå®ãæå®ãããŠããªãå Žåãè¡ããã¯ã䜿çšãããŸãã |
UseInternalTransaction | ãã®èšå®ãæå®ãããšãåãã«ã¯ããŒã¿ã³ããŒæäœããã©ã³ã¶ã¯ã·ã§ã³ã§å®è¡ãããŸãã ãã®èšå®ãèšå®ããã³ã³ã¹ãã©ã¯ã¿ã«SqlTransactionãªããžã§ã¯ããæäŸãããšãArgumentExceptionãã¹ããŒãããŸãã |
ãªãã·ã§ã³ã§ãããŒã¿ããŒã¹åŽã§ããªã¬ãŒãšå¶éã®æ€èšŒãæå¹ã«ã§ããŸãïŒããã©ã«ãã§ã¯ç¡å¹ïŒã
BatchSizeãš
UseInternalTransactionãæå®ãããšãããŒã¿ã¯åå¥ã®ãã©ã³ã¶ã¯ã·ã§ã³ã§ãããã¯åäœã§ãµãŒããŒã«éä¿¡ãããŸãã ãããã£ãŠãæåã®ãšã©ãŒãçºçãããŸã§æåãããã¹ãŠã®ãããã¯ã¯ããŒã¿ããŒã¹ã«ä¿åãããŸãã
4. MERGEã䜿çšããé«åºŠãªæ¿å
¥
SqlBulkCopyã¯ããŒãã«ã«ã¬ã³ãŒããè¿œå ã§ããã ãã§ãæ¢åã®ã¬ã³ãŒããå€æŽããæ©èœã¯æäŸããŸããã ããã§ããæŽæ°æäœã®å®è¡ãé«éåã§ããŸãïŒ ã©ããã£ãŠïŒ ããŒã¿ãäžæçãªç©ºã®ããŒãã«ã«è²Œãä»ããSQL Server 2008ã§å°å
¥ããã
MERGEã¹ããŒãã¡ã³ãã䜿çšããŠããŒãã«ãåæããŸãã
MERGEïŒTransact-SQLïŒ
ãœãŒã¹è¡šãžã®çµåã®çµæã«åºã¥ããŠãã¿ãŒã²ããè¡šã®æ¿å
¥ãæŽæ°ããŸãã¯åé€æäœãå®è¡ããŸãã ããšãã°ãå¥ã®ããŒãã«ã§èŠã€ãã£ãéãã«åºã¥ããŠ1ã€ã®ããŒãã«ã®è¡ãæ¿å
¥ãæŽæ°ããŸãã¯åé€ããããšã«ããã2ã€ã®ããŒãã«ãåæã§ããŸãã
MERGEã䜿çšãããšãè€è£œãåŠçããããã®ããŸããŸãªããžãã¯ãç°¡åãã€ç°¡åã«å®è£
ã§ããŸããã¿ãŒã²ããããŒãã«ã®ããŒã¿ãæŽæ°ããããäžèŽããã¬ã³ãŒããç¡èŠãŸãã¯åé€ããããšããã§ããŸãã ãããã£ãŠã次ã®ã¢ã«ãŽãªãºã ã䜿çšããŠãããŒã¿ããŒã¹å
ã®EFãªããžã§ã¯ãã®ã³ã¬ã¯ã·ã§ã³ããããŒã¿ãä¿åã§ããŸãã
- ã¿ãŒã²ããè¡šãšå®å
šã«åäžã®äžæè¡šãäœæ/æ¶å»ããŸãã
- SqlBulkCopyã䜿çšããŠäžæããŒãã«ã«ããŒã¿ãæ¿å
¥ããŸãã
- MERGEã䜿çšããŠãäžæããŒãã«ã®ãšã³ããªãã¿ãŒã²ããã«è¿œå ããŸãã
æé 1ãš3ã«ã€ããŠè©³ãã説æããŸãã
äžæããŒãã«ããŒã¿ãæ¿å
¥ããããã«ãããŒãã«ã¬ã€ã¢ãŠããå®å
šã«ç¹°ãè¿ãããŒãã«ãããŒã¿ããŒã¹ã«äœæããå¿
èŠããããŸãã æåã§ã³ããŒãäœæããããšã¯å¯èœãªéãææªã®ãªãã·ã§ã³ã§ããããŒãã«ã¹ããŒãã®æ¯èŒãšåæã«é¢ãããã以éã®ãã¹ãŠã®äœæ¥ãè² æ
ã«ãªããŸãã åè·¯ãããã°ã©ã ã§æ¿å
¥ããçŽåã«ã³ããŒããæ¹ãå®å
šã§ãã ããšãã°ã
SQL Server管çãªããžã§ã¯ãïŒSMOïŒã䜿çšããå Žå ïŒ
Server server = new Server();
çæãããSQLã埮調æŽããããã®æ°ååã®ãã©ã¡ãŒã¿ãŒãå«ã
ScriptingOptionsã¯ã©ã¹ã«æ³šæãã䟡å€ããããŸãã çµæã®
StringCollectionã
Stringã«å±éããŸãã æ®å¿µãªãããã¹ã¯ãªããã§ãœãŒã¹ããŒãã«ã®ååãäžæçãª
String.ReplaceïŒ "Order"ã "Order_TEMP"ïŒã®ååã«çœ®ãæãããããè¯ã解決çã¯èŠã€ãããŸããã§ããã åãããŒã¿ããŒã¹å
ã«ããŒãã«ã®ã³ããŒãäœæãããšããçŸãã決å®ã®ãã³ãã«æè¬ããŸãã å®æããã¹ã¯ãªããã䟿å©ãªæ¹æ³ã§å®è¡ããŸãããã ããŒãã«ã®ã³ããŒãäœæãããŸããïŒ
.NET 4+ã§SMOã䜿çšãããã¥ã¢ã³ã¹.NET 4+ã§
Database.ExecuteNonQueryãåŒã³åºããšã次ã®åœ¢åŒã®äŸå€ãã¹ããŒãããããšã«æ³šæããŠãã ããã
æ··åã¢ãŒãã¢ã»ã³ããªã¯ãã©ã³ã¿ã€ã ã®ããŒãžã§ã³ 'v2.0.50727'ã«å¯ŸããŠãã«ããããè¿œå ã®æ§ææ
å ±ãªãã§ã¯4.0ã©ã³ã¿ã€ã ã«ããŒãã§ããŸããã
çŽ æŽãããSMOã©ã€ãã©ãªã¯.NET 2 Runtimeå°çšã§ãããšããäºå®ã®ããã§ãã 幞ããªããšã«ã
åé¿çããããŸãã
<startup useLegacyV2RuntimeActivationPolicy="true"> ... </startup>
å¥ã®ãªãã·ã§ã³ã¯ã
Database.ExecuteWithResultsã䜿çšããããšã§ãã
äžæããŒãã«ããã¿ãŒã²ããã«ããŒã¿ãã³ããŒããŸãSQL ServeråŽã§MERGEã¹ããŒãã¡ã³ããå®è¡ããäžæããŒãã«ãšã¿ãŒã²ããããŒãã«ã®å
容ãæ¯èŒããå¿
èŠã«å¿ããŠæŽæ°ãŸãã¯æ¿å
¥ãå®è¡ããŸãã ããšãã°ã[Order]ããŒãã«ã®å Žåãã³ãŒãã¯æ¬¡ã®ããã«ãªããŸãã
MERGE INTO [Order] AS [Target] USING [Order_TEMP] AS [Source] ON Target.Id = Source.Id WHEN MATCHED THEN UPDATE SET Target.Date = Source.Date, Target.Number = Source.Number, Target.Text = Source.Text WHEN NOT MATCHED THEN INSERT (Date, Number, Text) VALUES (Source.Date, Source.Number, Source.Text);
ãã®SQLã¯ãšãªã¯ãäžæããŒãã«[Order_TEMP]ã®ã¬ã³ãŒããã¿ãŒã²ããããŒãã«[Order]ã®ã¬ã³ãŒããšæ¯èŒããIdãã£ãŒã«ãã«åãå€ãæã€ã¬ã³ãŒããèŠã€ãã£ãå Žåã¯æŽæ°ãå®è¡ãããã®ãããªã¬ã³ãŒããèŠã€ãããªãå Žåã¯æ¿å
¥ãå®è¡ããŸãã 䟿å©ãªæ¹æ³ã§ã³ãŒããå®è¡ããŸããããããã§å®äºã§ãïŒ äžæããŒãã«ãåé€ããããšãå¿ããªãã§ãã ããã
5.ããã©ãŒãã³ã¹ã®æ¯èŒ
ã©ã³ã¿ã€ã ç°å¢ïŒVisual Studio 2013ãEntity Framework 6.1.1ïŒããŒã¿ããŒã¹åªå
ïŒãSQL Server2012ããã¹ãã«ã¯ã[Order]ããŒãã«ã䜿çšããŸããïŒããŒãã«å³ã¯äžèšã®ãšããã§ãïŒã èšäºã§æ€èšããããŒã¿ããŒã¹ã«ããŒã¿ãä¿åããã¢ãããŒãã®ã©ã³ã¿ã€ã ã枬å®ããçµæã以äžã«ç€ºããŸãïŒæéã¯ç§åäœã§ç€ºãããŠããŸãïŒã
æ¿å
¥
ããŒã¿ããŒã¹ãžã®å€æŽãã³ãããããæ¹æ³ | ã¬ã³ãŒãæ° |
---|
1000 | 10,000 | 100,000 |
è¿œå +å€æŽãä¿å | 7.3 | 101 | 6344 |
è¿œå +ïŒAutoDetectChangesEnabled = falseïŒ+ SaveChanges | 6.5 | 64 | 801 |
è¿œå +åå¥ã®ã³ã³ããã¹ã+ SaveChanges | 8.4 | 77 | 953 |
AddRange + SaveChanges | 7.2 | 64 | 711 |
SqlBulkCopy | 0.01 | 0,07 | 0.42 |
ããïŒ
Addã¡ãœããã䜿çšããŠã³ã³ããã¹ãã«è¿œå ãã
SaveChangesãä¿åããå Žåã100,000ã¬ã³ãŒããããŒã¿ããŒã¹ã«ä¿åããã®ã«çŽ2æéããããŸãã
SqlBulkCopyã¯åãã¿ã¹ã¯ã«1ç§ã
ããããŸããïŒ
æŽæ°ãã
ããŒã¿ããŒã¹ãžã®å€æŽãã³ãããããæ¹æ³ | ã¬ã³ãŒãæ° |
---|
1000 | 10,000 | 100,000 |
å€æŽãä¿å | 6.2 | 60 | 590 |
SqlBulkCopy + MERGE | 0.04 | 0.2 | 1,5 |
åã³
SqlBulkCopyã競äºãã
æé€ããŸãã ãã¹ãã¢ããªã±ãŒã·ã§ã³ã®ãœãŒã¹ã³ãŒãã¯
GitHubã§å
¥æã§ã
ãŸã ã
çµè«
å€æ°ã®ãªããžã§ã¯ãïŒ10³以äžïŒãå«ãã³ã³ããã¹ããæäœããå ŽåãEntity Frameworkã€ã³ãã©ã¹ãã©ã¯ãã£ãæŸæ£ïŒã³ã³ããã¹ãã«è¿œå +ã³ã³ããã¹ããä¿åïŒããããŒã¿ããŒã¹ãžã®æžã蟌ã¿ã®ããã«SqlBulkCopyã«åãæ¿ãããšãããã©ãŒãã³ã¹ãæ°ååãŸãã¯æ°çŸåã«ãªããŸãã ããããç§ã®æèŠã§ã¯ãEF + SqlBulkCopyãã³ãã«ãã©ãã§ã䜿çšããããšã¯ãã¢ããªã±ãŒã·ã§ã³ã®ã¢ãŒããã¯ãã£ã«äœãåé¡ãããããšãæ確ã«ç€ºããŠããŸãã ãã®èšäºã§æ€èšãããŠããã¢ãããŒãã¯ãäœããã®çç±ã§ã¢ãŒããã¯ãã£/ãã¯ãããžãŒãå€æŽããããšãå°é£ãªå Žåããã§ã«èšè¿°ãããã·ã¹ãã ã®ããã«ããã¯ã®ããã©ãŒãã³ã¹ãå éããããã®ç°¡åãªæ段ãšèããã¹ãã§ãã Entity Frameworkã䜿çšããéçºè
ã¯ããã®ããŒã«ã®é·æãšçæãç¥ã£ãŠããå¿
èŠããããŸãã é 匵ã£ãŠïŒ