zoukankan      html  css  js  c++  java
  • EF Core批量插入与删除

    using EFPlus.Models;
    using Microsoft.EntityFrameworkCore;
    using System;
    using System.Diagnostics;
    using System.Linq;
    using System.Threading.Tasks;
    
    namespace EFPlus
    {
        class Program
        {
            static async Task Main(string[] args)
            {
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();
                //GetCount();
                await MultiDeleteWithSql();
    
                stopwatch.Stop();  //停止Stopwatch
                Console.WriteLine("Using Elapsed output runTime:{0}", stopwatch.Elapsed.ToString());//这里使用时间差来输出,如:时:分:秒
    
                Console.WriteLine("Hello World!");
            }
    
            //static void Main(string[] args)
            //{
            //    Stopwatch stopwatch = new Stopwatch();
            //    stopwatch.Start();
    
            //    MultiDelete();
    
            //    stopwatch.Stop();  //停止Stopwatch
            //    Console.WriteLine("Using Elapsed output runTime:{0}", stopwatch.Elapsed.ToString());//这里使用时间差来输出,如:时:分:秒
    
            //    Console.WriteLine("Hello World!");
            //}
    
    
            static void GetCount()
            {
                using (var db = new BlogDbContext())
                {
                    var allblogs = db.Blogs.Count();
                    Console.WriteLine(allblogs);
                }
    
            }
    
            static void MultiDelete()
            {
                using (var db = new BlogDbContext())
                {
                    var allblogs = db.Blogs.ToArrayAsync();
                    db.Blogs.RemoveRange(allblogs.Result);
                    db.SaveChanges();
                }
                
            }
    
            static async Task MultiDeleteAsync()
            {
                using (var db = new BlogDbContext())
                {
                    var allblogs = db.Blogs.ToArrayAsync();
                    db.Blogs.RemoveRange(allblogs.Result);
                    await db.SaveChangesAsync();
                }
            }
    
            /// <summary>
            /// 批量插入
            /// </summary>
            static void MultiInsert()
            {
                using (var db = new BlogDbContext())
                {
                    for (int i = 0; i < 1000000; i++)
                    {
                        var blog = new Blog { Url = "https://www.cnblog.com/p/" + i.ToString() };
                        db.Blogs.Add(blog);
                    }
                    db.SaveChanges();
                }
    
            }
    
            static async Task MultiInsertAsync()
            {
                using (var db = new BlogDbContext())
                {
    
                    for (int i = 0; i < 1000000; i++)
                    {
                        var blog = new Blog { Url = "https://www.cnblog.com/p/" + i.ToString() };
                        db.Blogs.Add(blog);
                    }
                    await db.SaveChangesAsync();
                }
            }
    
            static async Task MultiDeleteWithSql()
            {
                var db = new BlogDbContext();
                var conn = db.Database.GetDbConnection();
                try
                {
                    await conn.OpenAsync();
                    using (var command = conn.CreateCommand())
                    {
                        string query = "delete from blogs where Id<1000001";
                        command.CommandText = query;
                        var rows = await command.ExecuteNonQueryAsync();
                    }
                }
                finally
                {
                    conn.Close();
                }
            }
    
        }
    }
    

    在release模式下,localdb批量插入一百万数据,用时: 

     

    localdb批量删除一百万,用时(等了3分钟...不等了):

     

    异步方法下批量插入一百万数据:用时:

    异步方法下批量删除一百万数据:用时:

     

    EF执行原生sql删除一百万:

     

    在sql server 2008 R2数据库批量插入一百万数据:

     

     在sql server 2008 R2数据库批量删除一百万数据(3分钟不等了...):

     

    用EF执行原生sql批量删除:

  • 相关阅读:
    Monkey学习笔记(一)
    10.18 nslookup:域名查询工具
    10.22 tcpdump:监听网络流量
    10.22 tcpdump:监听网络流量
    Linux运维常见笔试题(选择题)
    Linux运维40道精华题
    LeetCode Isomorphic Strings 对称字符串
    LeetCode 3Sum Closest 最近似的3sum(2sum方法)
    博弈的图论模型——必败态与核
    威佐夫博弈(证明)
  • 原文地址:https://www.cnblogs.com/AlexanderZhao/p/12878873.html
Copyright © 2011-2022 走看看