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.NET\Framework\v4.0.30319\下

   对于64位机器,通常在 %WINDIR%\Microsoft.NET\Framework64\v4.0.30319\下

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

cd C:\Windows\Microsoft.NET\Framework\v4.0.30319 

ngen install d:\ConsoleApp1\bin\Release\EntityFramework.dll
ngen install d:\ConsoleApp1\bin\Release\EntityFramework.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

posted @ 2020-02-14 21:26  小水皮  阅读(1025)  评论(0编辑  收藏  举报