如何在sql server中获取记录之前计算总记录数

时间:2015-03-02 15:49:10

标签: c# sql sql-server datagridview

我正在使用Windows窗体应用程序并希望在DataGridView中执行分页 所以我写了一个查询

Select  DISTINCT  TOP(1000)
        TblStudentBioData.RegNo
    , Coalesce(TblStudentBioData.First_NameUr + SPACE(1) + 
      TblStudentBioData.Middle_NameUr + SPACE(1),TblStudentBioData.First_NameUr)
    + TblStudentBioData.Last_NameUr AS Name
    , TblStudentBioData.Father_NameUr
    , Coalesce(Ay.AcademicYearName,N'+@InCaseNull+') As AcademicYearName
    , Coalesce(Smst.SemName,N'+@InCaseNull+') As SemName
    , Coalesce(Colg.CollegeName,N'+@InCaseNull+') As CollegeName
    , Coalesce(CID.ClassName,N'+@InCaseNull+') As ClassName
    , TblImages.Images
    , TblStudentBioData.Student_ID
    , TblImages.ImageId
    , Ay.AcademicYearId
    , Smst.SemesterId
    , TblClassSchedule.ClassSchId


FROM TblStudentBioData
        INNER JOIN TBLCFGSEX AS sex
                    ON TblStudentBioData.CfgSexId = sex.CfgSexId
        LEFT JOIN TBLMARITALSTATUS ms
                    ON TblStudentBioData.MaritalStatusId = ms.MaritalStatusId
        INNER JOIN TblImages
                    ON TblStudentBioData.ImageId = TblImages.ImageId
        LEFT JOIN TBLBLOODGROUP BG
                    ON TblStudentBioData.BloodID = BG.BloodId

        LEFT JOIN TblStudentDetail
                    ON (TblStudentBioData.Student_ID = TblStudentDetail.Student_ID)
        LEFT JOIN TblStudentSubAss
                    ON TblStudentDetail.StudentDetailID = TblStudentSubAss.StudentDetailID
        LEFT JOIN TblClassSchedule
                    ON TblStudentDetail.CLassSchID = TblClassSchedule.ClassSchID

        LEFT JOIN TblSubAss
                    ON TblSubAss.SubAssId = TblStudentSubAss.SubAssId
        LEFT JOIN TblClass AS CID
                    ON TblClassSchedule.ClassID = CID.ClassID
        LEFT JOIN TBLCOLLEGE Colg
                    ON CID.CollegeId = Colg.CollegeID
        LEFT JOIN TblSemAssigning SA
                    ON TblClassSchedule.SemAssId = SA.SemAssId
        LEFT JOIN TblAcademicYear AY
                    ON SA.AcademicYearId = AY.AcademicYearId
        LEFT JOIN TblSemester Smst
                    ON Smst.SemesterId = SA.SemesterId

此查询返回的行超过28K行,但对于分页,我只需要1000行,总记录为28K,我将明智地获取记录1000。 任何想法我该怎么做?

注意:

  

这个查询需要20多秒才能执行,我想要一个不会增加执行时间的解决方案。

1 个答案:

答案 0 :(得分:-2)

如果您只在查询中提供1:1详细信息表,则可以先在主表的主键字段上执行selec count()。这应该只持续几毫秒

相关问题