c# - Linq to SQL Query Slow -
i'm trying following sql query linq sql.
select course_sections.sec_subject, course_sections.sec_course_no, course_sections.sec_no, sectionenrollments.sectionenrollment course_sections inner join (select stc_course_name, stc_section_no, count(*) sectionenrollment student_acad_cred stc_term = '2012fl' group stc_course_name, stc_section_no) sectionenrollments on course_sections.sec_subject + '-' + course_sections.sec_course_no = sectionenrollments.stc_course_name , course_sections.sec_no = sectionenrollments.stc_section_no sec_term = '2012fl'
my first attempt this
var query = sections in db.course_sections sections.sec_term == "2012fl" orderby sections.sec_subject, sections.sec_course_no, sections.sec_no select new { sections.sec_subject, sections.sec_course_no, sections.sec_no, sectionenrollmentcount = (from enrollments in db.student_acad_cred enrollments.stc_term == "2012fl" group enrollments new { stc_course_name = enrollments.stc_course_name, stc_section_no = enrollments.stc_section_no } grouping grouping.key.stc_course_name == sections.sec_subject + "-" + sections.sec_course_no && grouping.key.stc_section_no == sections.sec_no select grouping.count()).firstordefault() };
this actual works takes 30 return results (about 900 records). sql takes under second.
this next attempt try run faster.
var query2 = sections in db.course_sections join groupingquery in (from enrollments in db.student_acad_cred enrollments.stc_term == "2012fl" group enrollments new { stc_course_name = enrollments.stc_course_name, stc_section_no = enrollments.stc_section_no } grouping select new { grouping.key, enrollmentcount = grouping.count() } ) on sections.sec_subject + "-" + sections.sec_course_no + "-" + sections.sec_no equals groupingquery.key.stc_course_name + "-" + groupingquery.key.stc_section_no sectionenrollmentcount sections.sec_term == "2012fl" orderby sections.sec_subject, sections.sec_course_no, sections.sec_no select new { sections.sec_subject, sections.sec_course_no, sections.sec_no, sectionenrollmentcount };
however, sectionenrollmentcount variable type system.collections.generic.list1[<>f__anonymoustype2
2[<>f__anonymoustype0`2[system.string,system.string],system.int32]] , can't figure out how become number.
can me work?
thanks,
edit
this sql first attempt generating.
select [project1].[c1] [c1], [project1].[sec_subject] [sec_subject], [project1].[sec_course_no] [sec_course_no], [project1].[sec_no] [sec_no], [project1].[c2] [c2] ( select [filter1].[sec_subject] [sec_subject], [filter1].[sec_course_no] [sec_course_no], [filter1].[sec_no] [sec_no], 1 [c1], case when ([limit1].[a1] null) 0 else [limit2].[a1] end [c2] (select [extent1].[sec_subject] [sec_subject], [extent1].[sec_course_no] [sec_course_no], [extent1].[sec_no] [sec_no] [dbo].[course_sections] [extent1] '2012fl' = [extent1].[sec_term] ) [filter1] outer apply (select top (1) [groupby1].[a1] [a1] ( select [extent2].[stc_course_name] [k1], [extent2].[stc_section_no] [k2], count(1) [a1] [dbo].[student_acad_cred] [extent2] '2012fl' = [extent2].[stc_term] group [extent2].[stc_course_name], [extent2].[stc_section_no] ) [groupby1] ([groupby1].[k1] = ([filter1].[sec_subject] + n'-' + [filter1].[sec_course_no])) , ([groupby1].[k2] = [filter1].[sec_no]) ) [limit1] outer apply (select top (1) [groupby2].[a1] [a1] ( select [extent3].[stc_course_name] [k1], [extent3].[stc_section_no] [k2], count(1) [a1] [dbo].[student_acad_cred] [extent3] '2012fl' = [extent3].[stc_term] group [extent3].[stc_course_name], [extent3].[stc_section_no] ) [groupby2] ([groupby2].[k1] = ([filter1].[sec_subject] + n'-' + [filter1].[sec_course_no])) , ([groupby2].[k2] = [filter1].[sec_no]) ) [limit2] ) [project1] order [project1].[sec_subject] asc, [project1].[sec_course_no] asc, [project1].[sec_no] asc
this linq-to-sql command works
from section in db.course_sections join sectionenrollments in ( (from student_acad_cred in db.student_acad_cred student_acad_cred.stc_term == "2012fl" group student_acad_cred new { student_acad_cred.stc_course_name, student_acad_cred.stc_section_no } g select new { g.key.stc_course_name, g.key.stc_section_no, sectionenrollment = (int64?)g.count() })) on section.sec_subject + "-" + section.sec_course_no + "-" + section.sec_no equals sectionenrollments.stc_course_name + "-" + sectionenrollments.stc_section_no section.sec_term == "2012fl" orderby section.sec_subject, section.sec_course_no, section.sec_no select new { section.sec_subject, section.sec_course_no, section.sec_no, sectionenrollments.sectionenrollment };
Comments
Post a Comment