Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework EF真的很慢,还是我做错了什么?_Entity Framework - Fatal编程技术网

Entity framework EF真的很慢,还是我做错了什么?

Entity framework EF真的很慢,还是我做错了什么?,entity-framework,Entity Framework,我是EF的新手,我正在尝试一些非常基本的东西。在一个全新的控制台应用程序项目中,我刚刚在db(我的机器的本地)上使用了“逆向工程代码优先”(来自Entity Framework Power Tools) 然后我修改了Main以进行一些基本测试…从一个表中读取相同的三行两次…一次使用EF,一次使用传统系统(SqlConnection、DataReader等)。我对两个系统进行计时,并以微秒为单位打印输出时间 当它运行时,我会得到以下结果(当然,略有变化): 我做错什么了吗?我的LINQ查询在某种程

我是EF的新手,我正在尝试一些非常基本的东西。在一个全新的控制台应用程序项目中,我刚刚在db(我的机器的本地)上使用了“逆向工程代码优先”(来自Entity Framework Power Tools)

然后我修改了Main以进行一些基本测试…从一个表中读取相同的三行两次…一次使用EF,一次使用传统系统(SqlConnection、DataReader等)。我对两个系统进行计时,并以微秒为单位打印输出时间

当它运行时,我会得到以下结果(当然,略有变化):

我做错什么了吗?我的LINQ查询在某种程度上是一团糟,效率低得令人绝望,还是EF真的因为某种原因而如此缓慢

代码:

    static void Main(string[] args)
    {
        Stopwatch stopwatch = new Stopwatch();
        stopwatch.Start();

        // EF Path
        // Selecting three rows, one at a time
        using (var context = new myCustomContext())
        {
            var query = from b in context.myTable
                        where b.KEY == "Key1"
                        select b;

            stopwatch.Stop();

            foreach (var returnedVal in query)
            {
                Console.WriteLine("EF query 1: " + stopwatch.ElapsedMilliseconds);
            }

            stopwatch.Restart();

            var query2 = from b in context.myTable
                         where b.KEY == "Key2"
                         select b;

            foreach (var returnedVal in query2)
            {
                Console.WriteLine("EF query 2: " + stopwatch.ElapsedMilliseconds);
            }

            stopwatch.Restart();

            var query3 = from b in context.myTable
                         where b.KEY == "Key3"
                         select b;

            foreach (var returnedVal in query3)
            {
                Console.WriteLine("EF query 3: " + stopwatch.ElapsedMilliseconds);
            }
        }

        // Traditional path
        // Selecting same three rows, one at a time.  I recreate a connection
        // each time, in a futile attempt to even the odds.
        string connectionString = "<myConnectionString>";

        stopwatch.Restart();

        using (SqlConnection con = new SqlConnection(connectionString))
        {
            con.Open();

            using (SqlCommand command = new SqlCommand("select * from myTable where KEY = 'Key1'", con))
            using (SqlDataReader reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    Console.WriteLine("Trad query 1: " + stopwatch.ElapsedMilliseconds);
                }
            }
        }

        using (SqlConnection con = new SqlConnection(connectionString))
        {
            con.Open();
            stopwatch.Restart();

            using (SqlCommand command = new SqlCommand("select * from myTable where KEY = 'Key2'", con))
            using (SqlDataReader reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    Console.WriteLine("Trad query 2: " + stopwatch.ElapsedMilliseconds);
                }
            }

            stopwatch.Restart();
        }

        using (SqlConnection con = new SqlConnection(connectionString))
        {
            con.Open();
            using (SqlCommand command = new SqlCommand("select * from myTable where KEY = 'Key3'", con))
            using (SqlDataReader reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    Console.WriteLine("Trad query 3: " + stopwatch.ElapsedMilliseconds);
                }
            }
        }

        Console.ReadLine();
    }
static void Main(字符串[]args)
{
秒表秒表=新秒表();
秒表。开始();
//EF路径
//选择三行,一次一行
使用(var context=new myCustomContext())
{
var query=来自context.myTable中的b
其中b.KEY==“Key1”
选择b;
秒表;
foreach(查询中的var returnedVal)
{
Console.WriteLine(“EF查询1:+stopwatch.ElapsedMilliseconds”);
}
stopwatch.Restart();
var query2=来自context.myTable中的b
其中b.KEY==“Key2”
选择b;
foreach(查询2中返回的var VAL)
{
Console.WriteLine(“EF查询2:+stopwatch.ElapsedMilliseconds”);
}
stopwatch.Restart();
var query3=来自context.myTable中的b
其中b.KEY==“Key3”
选择b;
foreach(查询3中返回的var VAL)
{
Console.WriteLine(“EF查询3:+stopwatch.ElapsedMilliseconds”);
}
}
//传统路径
//选择相同的三行,一次一行。我重新创建连接
//每一次,都是徒劳地试图让胜算均等。
字符串连接字符串=”;
stopwatch.Restart();
使用(SqlConnection con=newsqlconnection(connectionString))
{
con.Open();
使用(SqlCommand=newsqlcommand(“从myTable中选择*,其中KEY='Key1',con))
使用(SqlDataReader=command.ExecuteReader())
{
while(reader.Read())
{
Console.WriteLine(“交易查询1:+stopwatch.ElapsedMilliseconds”);
}
}
}
使用(SqlConnection con=newsqlconnection(connectionString))
{
con.Open();
stopwatch.Restart();
使用(SqlCommand命令=新的SqlCommand(“从myTable中选择*,其中键='Key2',con))
使用(SqlDataReader=command.ExecuteReader())
{
while(reader.Read())
{
Console.WriteLine(“交易查询2:+stopwatch.ElapsedMilliseconds”);
}
}
stopwatch.Restart();
}
使用(SqlConnection con=newsqlconnection(connectionString))
{
con.Open();
使用(SqlCommand=newsqlcommand(“从myTable中选择*,其中KEY='Key3',con))
使用(SqlDataReader=command.ExecuteReader())
{
while(reader.Read())
{
Console.WriteLine(“交易查询3:+stopwatch.ElapsedMilliseconds”);
}
}
}
Console.ReadLine();
}

EF因这种开销而臭名昭著。如果需要更好的性能,您可以研究Dapper。要在没有任何对象/图形构造的情况下测试LINQ到EF的生成性能,请使用
if(query.any()){..}
——因为标准的非EF查询需要“0ms”,所以可以推断,即使使用
的SQL转换也不能更快/更慢。此外,由于连接池,您不必创建新连接。
    static void Main(string[] args)
    {
        Stopwatch stopwatch = new Stopwatch();
        stopwatch.Start();

        // EF Path
        // Selecting three rows, one at a time
        using (var context = new myCustomContext())
        {
            var query = from b in context.myTable
                        where b.KEY == "Key1"
                        select b;

            stopwatch.Stop();

            foreach (var returnedVal in query)
            {
                Console.WriteLine("EF query 1: " + stopwatch.ElapsedMilliseconds);
            }

            stopwatch.Restart();

            var query2 = from b in context.myTable
                         where b.KEY == "Key2"
                         select b;

            foreach (var returnedVal in query2)
            {
                Console.WriteLine("EF query 2: " + stopwatch.ElapsedMilliseconds);
            }

            stopwatch.Restart();

            var query3 = from b in context.myTable
                         where b.KEY == "Key3"
                         select b;

            foreach (var returnedVal in query3)
            {
                Console.WriteLine("EF query 3: " + stopwatch.ElapsedMilliseconds);
            }
        }

        // Traditional path
        // Selecting same three rows, one at a time.  I recreate a connection
        // each time, in a futile attempt to even the odds.
        string connectionString = "<myConnectionString>";

        stopwatch.Restart();

        using (SqlConnection con = new SqlConnection(connectionString))
        {
            con.Open();

            using (SqlCommand command = new SqlCommand("select * from myTable where KEY = 'Key1'", con))
            using (SqlDataReader reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    Console.WriteLine("Trad query 1: " + stopwatch.ElapsedMilliseconds);
                }
            }
        }

        using (SqlConnection con = new SqlConnection(connectionString))
        {
            con.Open();
            stopwatch.Restart();

            using (SqlCommand command = new SqlCommand("select * from myTable where KEY = 'Key2'", con))
            using (SqlDataReader reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    Console.WriteLine("Trad query 2: " + stopwatch.ElapsedMilliseconds);
                }
            }

            stopwatch.Restart();
        }

        using (SqlConnection con = new SqlConnection(connectionString))
        {
            con.Open();
            using (SqlCommand command = new SqlCommand("select * from myTable where KEY = 'Key3'", con))
            using (SqlDataReader reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    Console.WriteLine("Trad query 3: " + stopwatch.ElapsedMilliseconds);
                }
            }
        }

        Console.ReadLine();
    }