We've hit a performance nightmare with one of our data pulls in our application. Situation being is for a region based query where we have a massive list of customers and they should only see customers in their region when they view them. Current Tables (cut-down for ease of explanation):
Customer (Table):
CustomerID
FirstName
LastName
....
RegionCustomer (Table):
RegionCustomerID
RegionID
CustomerID
This is than retrieved through the .net entity framework using the following statement (edmx sets up the one to many relationship where one customer can have many RegionCustomer):
EntitiesManager.Customers.Where(function(x) x.RegionCustomers.Any(function(y) y.RegionID = CurrentUserRegionID)).ToList()
This runs ok when you are dealing with < 1000 records in either table but after that it is a performance nightmare the sql server spins up the CPU something shocking and takes to long. The SQL that the above statement creates is pretty nasty for what it needs to do (tracked through SQL Profiler).
Just wondering if anyone has any ideas on how this could be sped up?
Thanks in advance