I am having trouble with the performance of a query I am trying to write. I have a table with about 6000 records within it.
Currently its taking about 15 seconds to run on my development machine. A win8 machine with 32gb ram quadcore running vs2012 & sql2012. So its not my machine its my bad code.
public IEnumerable<Customer> GetByStoreIdAndContainingName(Guid storeId, string containing)
{
using (var context = new Entities())
{
var store = context.Stores.FirstOrDefault(b => b.StoreId == storeId);
if (store == null) return null;
var business = store.Business;
var consumers = new List<Consumer>();
consumers =
business.ConsumerIdentities.Select(ci => ci.Consumer)
.Distinct()
.Where(x => x.FirstName.ToLower().Contains(containing.ToLower()))
.ToList();
The layout of the database tables are
Business
BusinessId
Name
etc
StoreId
StoreId
StoreName
BusinessId
Consumer
ConsumerId
FirstName
LastName
ConsumerIdentities
BusinessId
ConsumerIdentityType
ConsumerIdentityValue
Can anyone see any obvious things I am doing wrong that would be taking so long to return the query results?
Turning on SQL Profiler was scary. The first query made was to select everything from ConsumerIdentity table where the business Id matched. Great that's perfect then gets the business table table.
However then seems to make a call for every single record like
exec sp_executesql N'SELECT
[Extent1].[ConsumerId] AS [ConsumerId],
[Extent1].[UserID] AS [UserID],
[Extent1].[FirstName] AS [FirstName],
[Extent1].[LastName] AS [LastName],
[Extent1].[IsMale] AS [IsMale],
[Extent1].[DateOfBirth] AS [DateOfBirth],
FROM [dbo].[Consumer] AS [Extent1]
WHERE [Extent1].[ConsumerId] = @EntityKeyValue1',N'@EntityKeyValue1 uniqueidentifier',@EntityKeyValue1='952ED7B8-2123-49E2-BAE3-69FBD713BACB'
So it looks like my where statement isn't getting applied