EF初次启动慢

Posted xbzhu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

以上是关于EF初次启动慢的主要内容,如果未能解决你的问题,请参考以下文章

为啥很多ASP.NET网页初次访问很慢,以后几次访问很快

git项目初次push提示error: failed to push some refs to https://gitee.com/xxxx/gittest.git’解决方案 --九五小庞(代码片段

Entity Framework EF 代码首先使用 ExecuteStoreCommand 执行 sproc 非常慢

EF添加关联的提示问题:映射从第 260 行开始的片段时有问题:

Blazor WebAssembly的初次访问慢的优化

Blazor WebAssembly的初次访问慢的优化