Sealer_05
Sealer_05

Reputation: 5566

Convert shown query/subquery to group by?

Update: I have got a sql query working but am still having trouble converting it to linq2sql. Please see the code below..

I have a linq query that is pulling a lot of data but sometimes it times out due to the load it puts on the sql server. I am going to work on getting more efficient data (adding indexes etc) but I have also heard that using group by would be more efficient than using the sub query. Would a group by be more efficient and if so what would my query below look like with a group by? I am not familiar with group by yet. Please dont use lambda

Edit: New query which is still slow and can time out:

  var query = (from s in db.ZipCodeServiceAvailabilities
                         join a in db.pdx_apart_views on s.ZipCode equals a.Zip_Code.Substring(0, 5)  into a_join
                         from a in a_join.DefaultIfEmpty()
                         join b in db.ZipCodeBoundaries on s.ZipCode equals b.ZipCode  into b_join
                         from b in b_join.DefaultIfEmpty()
                         where
                           (s.IsServiced == 1 &&
                            b.Ordering % 10 == 0 &&
                           s.State == "AL")
                         group new { s, b, a } by new
                         {
                             s.ZipCode,
                             s.IsServiced,
                             b.Longitude,
                             b.Latitude,
                             b.Ordering
                         } into g
                         orderby
                           g.Key.ZipCode,
                           g.Key.Ordering
                         select new
                         {
                             g.Key.ZipCode,
                             apartCount = g.Count(p => p.a.Apartment_complex != null),
                             Longitude = g.Key.Longitude,
                             Latitude = g.Key.Latitude
                         }).ToArray();

Edit: Query working in sql that I want in linq2sql (very fast):

select s.ZipCode, count(distinct ident) ApartCount, b.Longitude, b.Latitude from ZipCodeServiceAvailability s
                    left join pdx_apart_view
                          on s.ZipCode = left([Zip Code], 5)
                    left join ZipCodeBoundaries b
                          on s.ZipCode = b.ZipCode


  Where  IsServiced = 1 and and Ordering % 10 = 0 and State = 'AL'

  Group By s.ZipCode, IsServiced, b.Longitude, b.Latitude, b.Ordering
  Order by s.ZipCode, b.Ordering

Original query that is very slow:

var zips = (from s in db.ZipCodeServiceAvailabilities
    join b in db.ZipCodeBoundaries on s.ZipCode equals b.ZipCode
    where (s.IsServiced == service 
        && b.Ordering % 10 == 0 
        && s.State.Contains(state))
    orderby b.ZipCode
    select new
    {
        zipCode = b.ZipCode.Trim(),
        latitude = b.Latitude,
        longitude = b.Longitude,
        apartCount = (from a in db.pdx_apart_views
                      where a.Zip_Code.Remove(5) == b.ZipCode
                      select a.Apartment_complex).Count()

    }).ToArray();

Upvotes: 4

Views: 302

Answers (2)

Sealer_05
Sealer_05

Reputation: 5566

There was no way to duplicate the left[(zip code), 5] sql method in linq2sql without breaking the index. The answer was to go with straight ado.net so I get all sql functionality.

Upvotes: 1

Do you mean that you are not familiar with grouping in general, or only in the linq context?

I find it easier to write plain SQL than linq when I don't have the entity definitions, and below is what I think you are looking for in SQL - converting back to linq should be fairly straight-forward.

select
  b.ZipCode zipCode,
  b.Latitude latitude,
  b.Longitude longitude,
  count(a.Apartment_complex) apartCount
from 
  ZipCodeServiceAvailabilities s
join ZipCodeBoundaries b 
  on s.ZipCode = b.ZipCode
left join pdx_apart_views a
  on substring(a.Zip_Code, 1, 5) = b.ZipCode
group by
  ZipCode,
  Latitude,
  Longitude

Upvotes: 2

Related Questions