I'm writing an application that uses SQLite as its database, and since I'm doing it with C#, I thought using Entity Framework to handle all my database access would be a good idea. It was working fine when I was using a small (around 10k rows in the Identifiers table) dataset to test things, but when I started using a real dataset(around 950k rows in the Identifiers table), it became way too slow. For instance, this statement
id = domain.Identifiers.FirstOrDefault(i => i.ExternalId.Equals(externalId));
takes about 55 seconds to run, while the equivalent SQL Query
//database is my dbContext
id = database.Identifiers.SqlQuery("SELECT * FROM Identifiers WHERE DomainId = @p0 AND ExternalId = @p1", domain.Id, externalId).FirstOrDefault();
takes miliseconds.
I'm assuming (and hoping) I'm somehow misusing LINQ, or Entity Framework, or both, because LINQ, Entity Framework and SQLite were all made by people much smarter than me, and I'd rather not have to re-write all my data access code to use SQL queries. Can anyone help me figure out why the LINQ statement takes so long to execute?
Edit: I tried
id = database.Identifiers.FirstOrDefault(i => i.DomainId.Equals(domain.Id) && i.ExternalId.Equals(externalId));
and it was almost as fast as the SQL query, which solves my problem. Still, is there some reason for the massive difference in performance between calling the FirstOrDefault method on an entity and calling it on a dbContext?