查询性能不佳,选择少数字段比选择多个字段慢10倍

时间:2013-02-12 11:24:45

标签: java sql sql-server performance hibernate

使用SQL Server数据库时,我的团队一直遇到性能问题。首先,应用程序是用Java编写的,并使用了Hibernate。我们有一些数据存储在数据库中,我们可以轻松顺利地检索,以防我们想要选择完整的对象(例如:给定表中的所有或大多数字段)。

这很好用,但是当我们执行一个较小的查询时,它的速度要慢10倍。最明显的部分可能是我们只检索一小部分字段(在这个例子中:3)并使用一个类似的WHERE子句(在检索84个字段时似乎不是问题,但是检索3个字段)。

Hibernate完成简单查询大约需要7秒钟,而大型查询大约需要3,4秒。在DbVisualizer中运行纯SQL时,复杂查询查询需要0.02秒,传输数据需要0.7秒。在DbVisualizer中运行的简单查询需要0.2秒(慢10倍),然后在大约0.36秒内传输(因此,传输的数据更少)。有人可能认为这仍然比大查询更快,但它似乎对Hibernate性能有某种影响。

我开玩笑说它是“只是添加额外的字段,直到该死的东西快速运行!”,但这并没有削减它,因为两者,要选择的字段和where子句将是用户配置的。

84字段查询如下所示:

select
          locateable0_.Un_ID as Un1_37_,
          locateable0_.active as active37_,
          locateable0_.code as code37_,
          locateable0_.name as name37_,
          locateable0_.ClientID as ClientID37_,
          locateable0_.equipmentGroup as equipmen7_37_,
          locateable0_.SupplierContractID as Supplier8_37_,
          locateable0_.orderingCode as ordering9_37_,
          locateable0_.expirationDate as expirat10_37_,
          locateable0_.purchaseDate as purchas11_37_,
          locateable0_.warrantyExpirationAlert as warrant12_37_,
          locateable0_.price as price37_,
          locateable0_.priceUnit as priceUnit37_,
          locateable0_.VatID as VatID37_,
          locateable0_.WEAlertJobID as WEAlert16_37_,
          locateable0_.barcode as barcode37_,
          locateable0_.erpCode as erpCode37_,
          locateable0_.description as descrip19_37_,
          locateable0_.innerWorksheet as innerWo20_37_,
          locateable0_.outerWorksheet as outerWo21_37_,
          locateable0_.ContractorCompanyID as Contrac22_37_,
          locateable0_.ContractorPersonID as Contrac23_37_,
          locateable0_.ManufacturerCompanyID as Manufac24_37_,
          locateable0_.ManufacturerPersonID as Manufac25_37_,
          locateable0_.ServicerCompanyID as Service26_37_,
          locateable0_.ServicerPersonID as Service27_37_,
          locateable0_.BudgetID as BudgetID37_,
          locateable0_.costObjectId as costObj29_37_,
          locateable0_.costKindId as costKindId37_,
          locateable0_.TemplateId as TemplateId37_,
          locateable0_.openingDate as opening32_37_,
          locateable0_.mainClassId as mainCla33_37_,
          locateable0_.pictureId as pictureId37_,
          locateable0_.productType as product35_37_,
          locateable0_.propertiesId as propert36_37_,
          locateable0_.ParentID as ParentID37_,
          locateable0_.accessDomainId as accessD38_37_,
          locateable0_.orderStateType as orderSt39_37_,
          locateable0_.orderEventId as orderEv40_37_,
          locateable0_.locationId as locationId37_,
          locateable0_.siteId as siteId37_,
          locateable0_.buildingId as buildingId37_,
          locateable0_.storeyId as storeyId37_,
          locateable0_.roomId as roomId37_,
          locateable0_.cadObjectId as cadObje46_37_,
          locateable0_.geoLattitude as geoLatt47_37_,
          locateable0_.geoLongitude as geoLong48_37_,
          locateable0_.ratingId as ratingId37_,
          locateable0_.grossArea as grossArea37_,
          locateable0_.grossVolume as grossVo51_37_,
          locateable0_1_.floor as floor99_,
          locateable0_2_.calculatedArea as calculat2_100_,
          locateable0_2_.nominalArea as nominalA3_100_,
          locateable0_2_.categoryId as categoryId100_,
          locateable0_2_.wingId as wingId100_,
          locateable0_2_.areaUnitId as areaUnitId100_,
          locateable0_2_.cleaningArea as cleaning7_100_,
          locateable0_2_.rentableArea as rentable8_100_,
          locateable0_2_.windowSurface as windowSu9_100_,
          locateable0_2_.bottomSurface as bottomS10_100_,
          locateable0_2_.topSurface as topSurface100_,
          locateable0_2_.wallSurface as wallSur12_100_,
          locateable0_2_.floorType as floorType100_,
          locateable0_3_.areaSize as areaSize101_,
          locateable0_3_.areaType as areaType101_,
          locateable0_3_.flooring as flooring101_,
          locateable0_4_.workplaceNumber as workplac2_102_,
          locateable0_4_.workplaceType as workplac3_102_,
          locateable0_4_.usingCompanyId as usingCom4_102_,
          locateable0_5_.quantity as quantity190_,
          locateable0_5_.quantityUnitId as quantity3_190_,
          locateable0_6_.inventoryNumber as inventor2_202_,
          locateable0_7_.area as area233_,
          locateable0_7_.meterValue as meterValue233_,
          locateable0_7_.calibrationFactor as calibrat4_233_,
          locateable0_7_.areaDomainId as areaDoma5_233_,
          locateable0_7_.degreeDayId as degreeDa6_233_,
          locateable0_7_.virtualType as virtualT7_233_,
          locateable0_7_.differenceFactor as differen8_233_,
          locateable0_7_.differenceMasterId as differen9_233_,
          locateable0_7_.virtual as virtual233_,
          locateable0_7_.startDate as startDate233_,
          locateable0_7_.scheduleId as scheduleId233_,
          locateable0_.discriminator as discrimi2_37_
      from
          MNT_Equipments locateable0_
      left outer join
          Storey locateable0_1_
              on locateable0_.Un_ID=locateable0_1_.id
      left outer join
          Room locateable0_2_
              on locateable0_.Un_ID=locateable0_2_.id
      left outer join
          RoomArea locateable0_3_
              on locateable0_.Un_ID=locateable0_3_.id
      left outer join
          Workplace locateable0_4_
              on locateable0_.Un_ID=locateable0_4_.id
      left outer join
          INV_Product locateable0_5_
              on locateable0_.Un_ID=locateable0_5_.id
      left outer join
          MNT_FacilityProduct locateable0_6_
              on locateable0_.Un_ID=locateable0_6_.id
      left outer join
          EN_ResourceMeter locateable0_7_
              on locateable0_.Un_ID=locateable0_7_.id
      left outer join
          DynamicProperties dynamicpro1_
              on locateable0_.propertiesId=dynamicpro1_.id
      left outer join
          KEYWORDSEARCH tags2_
              on dynamicpro1_.id=tags2_.dynamicPropertiesId
      left outer join
          MNT_Clients client3_
              on locateable0_.ClientID=client3_.Un_ID
      where
          (
              locateable0_.accessDomainId is null
              or locateable0_.accessDomainId in       (
                  select
                      uad.domainId
                  from
                      PERM_UserAccessDomain uad
                  join
                      Users u
                          on uad.userId = u.Un_ID
                  where
                      u.UserName = 'wsc'
                      and uad.functionId = 2000
              )
          )
          and locateable0_.discriminator in (
              '17000', '6010', '6020', '6030', '6035', '6040', '6060', '6070', '6080', '18000', '5010', '6000', '6100', '5000', '9000', '14000', '19000'
          )
          and (
              client3_.Un_ID in (
           1012, 1016, 1013, 1014
              )
          )
          and (
              locateable0_.productType not in  (
   18000, 19000
              )
          )
          and locateable0_.active=1
          and locateable0_.productType<>6080
          and (
              upper(tags2_.keyword) like 'B%'
          )

慢速3字段查询如下所示:

select
       locateable0_.Un_ID as col_0_0_,
       locateable0_.code as col_1_0_,
       locateable0_.name as col_2_0_
   from
       MNT_Equipments locateable0_
   left outer join
       DynamicProperties dynamicpro1_
           on locateable0_.propertiesId=dynamicpro1_.id
   left outer join
       KEYWORDSEARCH tags2_
           on dynamicpro1_.id=tags2_.dynamicPropertiesId
   left outer join
       MNT_Clients client3_
           on locateable0_.ClientID=client3_.Un_ID
   where
       (
           locateable0_.accessDomainId is null
           or locateable0_.accessDomainId in       (
               select
                   uad.domainId
               from
                   PERM_UserAccessDomain uad
               join
                   Users u
                       on uad.userId = u.Un_ID
               where
                   u.UserName = 'wsc'
                   and uad.functionId = 2000
           )
       )
       and locateable0_.discriminator in (
           '17000', '6010', '6020', '6030', '6035', '6040', '6060', '6070', '6080', '18000', '5010', '6000', '6100', '5000', '9000', '14000', '19000'
       )
       and (
           client3_.Un_ID in (
   1012, 1016, 1013, 1014
           )
       )
       and (
           locateable0_.productType not in  (
   18000, 19000
           )
       )
       and locateable0_.active=1
       and (
           upper(tags2_.keyword) like 'B%'
       )

Microsoft SQL Server Management Studio建议为我们请求的字段添加索引,但这样做根本没有帮助。特别要记住,要查询的字段将是用户可配置的,并且休眠将在查询和获取时间之外添加巨大的奖励。

那么,我们可以做些什么来将性能提升到可接受的水平?

1 个答案:

答案 0 :(得分:2)

我会注意到你的where子句有所不同,第一个是:

and locateable0_.productType<>6080

并在第二次失踪。

您的数据库中是否有更多此类产品的记录?即你的第二个查询返回的行多于第一个?