简体   繁体   English

标准SQL查询比Linq 2 SQL快得多

[英]Standard SQL Query much faster than Linq 2 SQL

Hey there I got a problem with my code, I compared three ways of implementing a query on a database 嘿,我的代码有问题,我比较了在数据库上实现查询的三种方式

  1. Standard SQL Query (From * Select ...) 标准SQL查询(从*选择...)
  2. LinQ Query LinQ查询
  3. Precompiled LinqQuery (CompiledQuery) 预编译的LinqQuery(CompiledQuery)

Now the result I got is that the Precompiled LingQuery is 5 times faster than the LinQ Query but much more interesting ist that the Standard SQL Query is approximately ten times fast than the Precompiled LinqQuery. 现在我得到的结果是,预编译的LingQuery比LinQ查询快5倍,但更有趣的是,标准SQL查询比预编译的LinqQuery快大约10倍。 The table that my Queries run against is one simple table with no associations to other tables. 我的查询所针对的表是一个简单的表,与其他表没有关联。

The fact that makes the table interesting is that it consists of 5 columns which represent all together the primary key I mean. 使表格变得有趣的事实是它由5列组成,这些列共同代表了我所指的主键。 all columns together is unique so there is no row in the table that looks like the other. 所有列在一起都是唯一的,因此表中没有其他行。 All columns are indexed. 所有列均已建立索引。

Does anyone have an idea about what goes wrong with LinQ? 有没有人知道LinQ出了什么问题?

With kind regards 亲切的问候

Sebastian 塞巴斯蒂安

Have you had a look at the actual query that linq generates? 您是否看过linq生成的实际查询? Comparing those with your SQL query might give you some answers. 将它们与SQL查询进行比较可能会给您一些答案。

You can do this either using SQL Profiler or listening to the linq datacontext's Out property? 您可以使用SQL Profiler或侦听linq datacontext的Out属性来执行此操作? You can do it like this in a console app: 您可以在控制台应用程序中这样做:

DataContext ctx = new ...;
ctx.Log = Console.Out;
//Now execute the linq query, and the query will be output to the console.

只要确保您的查询没有被缓存,就可能使结果不正确。

How are you measuring? 你如何测量? Are you using all of the query results, or just executing the query? 您是使用所有查询结果,还是仅执行查询? (LINQ may well be fetching the results for you, for example.) (例如,LINQ可能会为您获取结果。)

Does this happen the second time you execute the query too, or are you just measuring the time taken for the first execution? 这是否也会在第二次执行查询时发生,还是只是在测量第一次执行的时间?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM