使用nhibernate进行慢查询

时间:2017-09-06 10:02:54

标签: sql-server nhibernate nhibernate-mapping

我的查询非常慢。我们使用sqlserver-database。 我使用nHibernate如下:

Session.Flush();
            Session.FlushMode = FlushMode.Never;

            IQuery query =
                this.Session.GetNamedQuery("FindGroupsByHcwRizivNr")
                    .SetParameter("rizivNr", rizivNr)
                    .SetParameter("GroupName", "%%")
                    .SetParameter("hciId", 0)
                    .SetParameter("hcwId", 0)
                    .SetParameter("hcwSitId", 0)
                    .SetParameter("inss", "")
                    .SetParameterList("typeCodes", codes)
                    .SetResultTransformer(new AliasToBeanResultTransformer(typeof(Group)))
                    .SetFirstResult(0);

            IList<Group> result = query.List<Group>();

            Session.FlushMode = FlushMode.Auto; 

            watch.Stop();
            var elapsedMs = watch.ElapsedMilliseconds;
            Debug.WriteLine(" => FindByHcwRizinNr : " + TimeSpan.FromMilliseconds(elapsedMs).TotalSeconds);

时间告诉我:=&gt; FindByHcwRizinNr:12,789

查询如下:

select DISTINCT  a.id as 'Id', 
         a.RIZIVNr as 'RizivNr', 
         a.Denomination as 'Denomination', 
         a.FirstInsDate as 'FirstInsDate',
         c.ID as 'HcwId',
         e.BCEKBO as 'CbeEstablishmentNr', 
         d.ID as 'TypeCodeId', 
         d.Code AS 'TypeCodeCode', 
         d.DescriptionNL AS 'DescriptionNL', 
         d.DescriptionFR AS 'DescriptionFR',
         g.Id as 'TypeSitCodeId',
         g.Code as 'SitCode',
         g.DescriptionFR as 'SitDescriptionFR',
         g.DescriptionNL as 'SitDescriptionNL',
         f.Id as 'SitCodeId',
         f.SitBegDate as 'SitBegDate',
         F.SitEndDate as 'SitEndDate',
         c.rizivnr as 'LidRizivNr',
         a.LastSitID as 'LastSitID',
         c.LastSitID as 'MemberLastSitID',
         n.Code as 'MemberSitCode',
         n.DescriptionFR as 'MemberSitDescriptionFR',
         n.DescriptionNL as 'MemberSitDescriptionNL',
         j.FirstName1 as 'FirstName1',
         j.FirstName2 as 'FirstName2',
         j.LastName as 'LastName',
         j.INSS as 'INSS',
         h.FromDate as 'StartDatum',
         k.Code as 'RolCode',
         k.DescriptionFR as 'RolDescriptionFR',
         k.DescriptionNL as 'RolDescriptionNL',
         h.ToDate as 'EindDatum',
         m.Id as 'AddressId',
         m.Street as 'Street',
         m.StreetNr as 'StreetNr',
         m.Box as 'Box',
         m.Locality as 'Locality',
         m.ZipCode as 'ZipCode',
         p.AttributeValueID as 'AttributValueId',
         p.AttributeID as 'AttributeId',
         r.Code as 'AttributeCode',
         q.AttributeValue,
         s.Code as 'ProfCodeCode',
         s.DescriptionNL as 'ProfDescriptionNL',
         s.DescriptionFR as 'ProfDescriptionFR',
               h.Id as 'HcwSitId',
               u.Id as 'RequestId',
               u.HCWID as 'RequestForId',
                 x.rizivnr as 'RequestedForRizivNr',
               u.ToDate as 'RequestToDate',
               u.FromDate as 'RequestFromDate',
               u.RequestedBy as 'RequestedBy',
               v.Code as 'RequestRolCode',
         v.DescriptionFR as 'RequestRolDescriptionFR',
         v.DescriptionNL as 'RequestRolDescriptionNL',
               w.FirstName1 + ' ' + w.LastName as 'RequestedByName',
               y.FirstName1 as 'RequestedForFirstName1',
         y.FirstName2 as 'RequestedForFirstName2',
         y.LastName as 'RequestedForLastName',
               y.INSS as 'RequestedForINSS',
         z.ID as 'HciAttributeRequestId',
         z.RequestedBy as 'HciAttributeRequestedBy'
    from [RefSZV].[SZV].[hci] a
   inner join [RefSZV].[SZV].[HCWHCI] b on (a.id = b.HCIID)
   inner join [RefSZV].[SZV].[HCW] c on (b.HCWID = c.ID)
   inner join [RefSZV].[Param].[TypeCode] d on (d.ID = a.TypeCodeID)
   inner join [RefSZV].[SZV].[Org] e on (e.ID = a.OrgID)
   inner join [RefSZV].[SZV].[HCISit] f on F.ID in (select max(ID) from [RefSZV].[SZV].[HCISit] where HCIID = a.ID)
   inner join [RefSZV].[Param].[TypeSitCode] g on (g.ID = f.SitCodeID)
   inner join [RefSZV].[SZV].[HCWHCI] h on (h.HCIID = a.ID and h.HCWID = c.Id)
     inner join [RefSZV].[SZV].[HCWSit] o on (o.HCWID = c.id)
   inner join [RefSZV].[Param].[TypeSitCode] n on (n.ID = o.SitCodeID)
     inner join [RefSZV].[SZV].[Person] j on (j.id = c.PersonID)
   inner join [RefSZV].[Param].[HCWRole] k on (h.RoleID = k.ID)
     inner join [RefSZV].[SZV].[HCIAddress] l on (l.HCIID = a.id)
     inner join [RefSZV].[Param].[ProfCode] s on (s.ID = c.ProfCodeID)
     inner join [RefSZV].[SZV].[Address] m on (m.id = l.AddressID)
      left join [RefSZV].[SZV].[HCIAttribute] p on (p.HCIID = a.id)
        left join [RefSZV].[Param].[AttributeValue] q on (p.AttributeValueID = q.id)
        left join [RefSZV].[Param].[Attribute] r on (r.ID = p.AttributeID and r.Code = 'TherapeuticLink')
        left join [RefSZV].[SZV].[HCWHCIRequest] u on (u.hciid = a.id and u.hcwid = c.id and validated = 0 and canceled = 0)
    left join [RefSZV].[Param].[HCWRole] v on (u.RoleID = v.ID)
        left join [RefSZV].[SZV].[Person] w on (w.INSS = u.RequestedBy)
        left join [RefSZV].[SZV].[HCW] x on (u.HCWID = x.ID)
        left join [RefSZV].[SZV].[Person] y on (x.PersonId = y.ID)
          left join [RefSZV].[SZV].[HCIAttributeRequest] z on (z.HCIID = a.id And z.validated = 0 and z.canceled = 0)
   where (d.Code in (:typeCodes))
     and (c.RIZIVNr = :rizivNr or :rizivNr =  '')
     and (c.id = :hcwId or :hcwId = 0)
     and (a.id = :hciId or :hciId = 0)
     and (a.Denomination like :GroupName or a.Denomination = null)
     and (h.Id = :hcwSitId or :hcwSitId = 0)
     and (j.Inss = :inss or :inss = '')

我检查了数据库并添加了必要的索引。但问题仍然很慢。

有谁知道我能做什么?

0 个答案:

没有答案