I'm wondering about the performance of the SQL queries executed in SQL Server and used in ADO.NET and LINQ-to-SQL.
I use the AdventureWorks
database with extended Sales.SalesOrderDetailEnlarged
table which has almost 5 million rows. Executing a query
select *
from Sales.SalesOrderDetailEnlarged
lasts about 37 seconds in SQL Server, but execution of the same query in ADO.NET is about 21 seconds. To measure execution time I use Stopwatch and SQL Server Profiler.
// this is how I perform command execution in ADO.NET
using (SqlConnection sqlConnection = new SqlConnection(GetConnectionString()))
{
sqlConnection.Open();
DataSet table = new DataSet();
using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(commandQuery, sqlConnection))
{
//stopwatch start
sqlDataAdapter.Fill(table);
//stopwatch stop
}
}
In case of Linq-to-SQL, I have such a query which is equivalent to query used in ADO.NET. Execution time of query using Linq-to-SQL lasts about 12-13 seconds
var query = from salesOrderDetail in dataContext.SalesOrderDetailEnlargeds
select salesOrderDetail;
//stopwatch start
query.ToList();
//stopwatch stop
Could someone explain:
Shouldn't query execution in SQL Server be faster than ADO.NET?
How to compare time execution of this query for ADO.NET and Linq-to-SQL? Linq-to-SQL is actually a layer on top of ADO.NET, so why is it faster than ADO.NET (or maybe I try to measure it in the wrong way)?