zoukankan      html  css  js  c++  java
  • EF初次启动慢

    EF第一次查询很慢,大约在2s左右,第二次及之后就变快了。

    EFCore第一次查询大约也有1s左右。

    而用ado.net第一次查询也就只有100ms。

    测试结果(EF和ado.net):

    测试代码如下:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data;
    using System.Data.Entity;
    using System.Data.SqlClient;
    using System.Diagnostics;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                testado(); testef();
                Console.ReadLine();
    
            }
    
            private static void testado()
            {
    
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Restart();
    
                String connString = "data source=.\sqlexpress;initial catalog=test;integrated security=True;";
    
                using (SqlConnection conn = new SqlConnection(connString))
                {
                    try
                    {
    
                        conn.Open();
    
                        using (SqlCommand cmd = new SqlCommand())
                        {
    
                            cmd.CommandText = "select top 1 * from [Member]";
                            cmd.CommandType = CommandType.Text;
                            cmd.Connection = conn;
                            var data = cmd.ExecuteScalar();
                        }
                        conn.Close();
    
                    }
                    catch (Exception err)
                    {
                        throw err;
                    }
                }
    
    
                Console.WriteLine("the ado.net query1: " + stopwatch.ElapsedMilliseconds + " millisecond.");
                stopwatch.Restart();
    
                using (SqlConnection conn = new SqlConnection(connString))
                {
                    try
                    {
    
                        conn.Open();
                        using (SqlCommand cmd = new SqlCommand())
                        {
                            cmd.CommandText = "select top 1 * from [MemberProject]";
                            cmd.CommandType = CommandType.Text;
                            cmd.Connection = conn;
                            var data = cmd.ExecuteScalar();
                        }
                        conn.Close();
    
                    }
                    catch (Exception err)
                    {
                        throw err;
                    }
                }
    
                stopwatch.Stop();
                Console.WriteLine("the ado.net query1: " + stopwatch.ElapsedMilliseconds + " millisecond.");
            }
    
            private static void testef()
            {
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Restart();
    
                DataContext dataContext = new DataContext();
    
                var data1 = dataContext.Members.First();
                Console.WriteLine("the ef query1: " + stopwatch.ElapsedMilliseconds + " millisecond.");
                stopwatch.Restart();
    
                var data2 = dataContext.MemberProjects.First();
                stopwatch.Stop();
                Console.WriteLine("the ef query2:  " + stopwatch.ElapsedMilliseconds + " millisecond.");
            }
        }
        public class DataContext : DbContext
        {
            public DataContext() : base("data source=.\sqlexpress;initial catalog=test;integrated security=True;")
            {
            }
            public DbSet<Member> Members { get; set; }
            public DbSet<MemberProject> MemberProjects { get; set; }
    
        }
    
        [Table("Member")]
        public class Member
        {
            [Key]
            public int MemberID { get; set; }
        }
        [Table("MemberProject")]
        public class MemberProject
        {
            [Key]
            public int MemberProjectID { get; set; }
        }
    }

    百度出来优化的方法,使用NGen优化。

    亲测可用,优化后第一次查询大概200ms

    操作步骤:

    1:以管理员身份启动控制台cmd程序

    2:切换到本机.NET 工具目录下:

       对于32位机器,通常在%WINDIR%Microsoft.NETFrameworkv4.0.30319下

       对于64位机器,通常在 %WINDIR%Microsoft.NETFramework64v4.0.30319下

    3:然后执行 ngen install 加上程序集的路径和名称,即可。

    cd C:WindowsMicrosoft.NETFrameworkv4.0.30319 

    ngen install d:ConsoleApp1inReleaseEntityFramework.dll
    ngen install d:ConsoleApp1inReleaseEntityFramework.SqlServer.dll

    但是该方案对EFCore无效,搜索很久,找不到关于EFCore的优化方案。

    参考资料:https://www.cnblogs.com/yangecnu/p/Speed-First-Startup-of-the-Entity-Framework.html

    https://github.com/dotnet/efcore/issues/4372

  • 相关阅读:
    [51nod1474]宝藏图
    web h5常用代码总结
    ionic app 热更新
    ionic3——ion-scroll无法使用scrollTo的问题
    git操作
    uniapp开发
    uniapp 之navigateTo:fail page 跳转路径不对
    微信小程序之登录用户不是该小程序的开发者
    ionic slide组件使用
    ionic使用自定义icon
  • 原文地址:https://www.cnblogs.com/xbzhu/p/12309661.html
Copyright © 2011-2022 走看看