Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Linq To Sql databinding to gridview much slower than pass-through SQL?

I have compared two queries which fetch some fairly large data from a database table. For query one I used Linq To Sql and for the other I use passthrough SQL via ADO.NET.

I know that Linq To Sql has to do a lot of work behind the scenes, but what is it actually doing? The two queries fetches the same amount of data but the Linq To Sql query is more than 5 seconds slower and uses 150mb more RAM!

Here is my test code:

Using Linq To Sql :

public void MakeList()
    {
        int start = Environment.TickCount;
        var document = from d in _dm.tDokuments select d;

        List<tDokument> documentList = document.ToList();
        int end = Environment.TickCount;

        GridView1.DataSource = documentList;
        GridView1.DataBind();

        Label1.Text = (end - start).ToString();
    }

Passthrough SQL + ADO.NET:

public void MakeList()
    {

        int start = Environment.TickCount;
        SqlCommand sqlCommand = new SqlCommand("SELECT * FROM tDokument", _connection);
        SqlDataAdapter da = new SqlDataAdapter(sqlCommand);

        DataSet ds = new DataSet();
        da.Fill(ds);
        int end = Environment.TickCount;

        GridView1.DataSource = ds;
        GridView1.DataBind();

        Label1.Text = (end - start).ToString();
    }
like image 552
Poku Avatar asked Nov 22 '09 16:11

Poku


People also ask

Which is faster LINQ or SQL?

Stored procedures are faster as compared to LINQ query since they have a predictable execution plan and can take the full advantage of SQL features. Hence, when a stored procedure is being executed next time, the database used the cached execution plan to execute that stored procedure.

Which is faster LINQ or SQL and why?

Sql is faster than Linq. Its simple: if I m executing a sql query directly its a one way process whereas if I m using linq, first its been converted to sql query and then its executed.

Why do you prefer LINQ over SQL query?

Compared to SQL, LINQ is simpler, tidier, and higher-level. It's rather like comparing C# to C++. Sure, there are times when it's still best to use C++ (as is the case with SQL), but in most situations, working in a modern tidy language and not having to worry about lower-level details is a big win.

What are the factors to be noted while handling LINQ to SQL queries?

LINQ to SQL needs a Data Context object. The Data Context object is the bridge between LINQ and the database. LINQ to Objects doesn't need any intermediate LINQ provider or API. LINQ to SQL returns data of type IQueryable<T> while LINQ to Objects returns data of type IEnumerable<T> .


3 Answers

Linq2Sql is returning strongly typed objects where as the dataset is getting populated with what essentially amounts to a hash table.

In Linq, the population of the data and the binding of that data to a GridView uses a lot of reflection to generate the desired results.

In the second piece of code, the data is being loaded into a dataset and binded to a GridView. This is essentially loading a hashtable with data and lookups to bind.

Hashtable operations are always going to be faster than reflection. With a small amount of data, there is not going to be a noticeable difference but for lots of data, you will see the impact of reflection in Linq.

like image 113
Jason Avatar answered Sep 23 '22 02:09

Jason


Have you looked at the actual SQL being sent to SQL Server with Profiler?

In this case I suspect it's how the client is handling it (DataSet vs List) that is causing the difference, but I'm nowhere near a c# expert.

like image 41
gbn Avatar answered Sep 22 '22 02:09

gbn


Capture and analyze the SQL statement(s) that are being sent over the wire in your Linq To Sql example. SQL Profiler will do the trick.

Run both those statements from Example 1 and 2 directly against your SQL Server using Management Studio. Likely you won't see ANY substantial difference in the query plan.

I think the majority of time is spent in constructing the C# objects (Jason's answer nails it, I think).

like image 25
p.campbell Avatar answered Sep 23 '22 02:09

p.campbell