I am having an issue using SQLiteParameters and the LIKE operator in a SQLite query. Here is a snippet of code, and I apologize if I don't have enough code here. If that is the case, I can easily post more.
Poor Performance:
using (OdysseyDataContext entities = new OdysseyDataContext())
{
var results = entities.SearchResults.SqlQuery(
"SELECT * FROM SearchResults WHERE ContactName LIKE @ContactName",
new SQLiteParameter("@ContactName", "test")
);
}
Great Performance:
using (OdysseyDataContext entities = new OdysseyDataContext())
{
var results = entities.SearchResults.SqlQuery(
string.Format(
"SELECT * FROM SearchResults WHERE ContactName LIKE '{0}'",
"test"
)
);
}
Other important code:
public class OdysseyDataContext : DbContext
{
public DbSet<SearchResult> SearchResults { get; set; }
}
public class SearchResult
{
[Key]
public Guid Id { get; set; }
public string ContactName { get; set; }
}
The first example takes 700 ms to execute, which my supervisor finds unacceptable. The second example takes 7 ms to execute. Why the difference? Is there something I am doing completely wrong to earn me newbie status?
Thanks in advance!
SqlQuerymethod source? Are you sure it's the query itself you are timing; or is it including the overhead of creating a parameter and adding it to the collection? (It should be almost no overhead, but we won't know till we see the method's source)