SQL查询需要太长时间

时间:2012-03-27 15:42:27

标签: sql tsql sql-server-2008-r2

此查询大约需要52秒才能完成。在订单表的表扫描中花费了82%的时间。它说实际的行数超过800万,所以我唯一的猜测就是它循环遍历这个表,而且我不熟悉SQL以了解原因。

订单表目前有19700行。

SELECT a.DistID, a.BusCtrID, d.FName, d.LName, r.Description 
FROM funcGetDownline( 3 , 1) a
INNER JOIN Distributor d ON a.DistID = d.DistID
INNER JOIN Rank r ON d.RankID = r.RankID
INNER JOIN Orders o ON o.DistID = d.DistID
INNER JOIN orderlines ol ON ol.OrderID = o.OrderID
GROUP BY a.DistID, a.BusCtrID, d.FName, d.LName, r.Description'

funcGetDownline返回一个表,返回4,416条记录。如果我只对订单行执行查询,它将返回15,361行,这些行应该只返回不超过4,416行。

如果我对订单行执行查询,它将返回20811行,同样不应该返回超过4,416,少即可,但不是更多。它会在1秒内执行。

当我将其执行到组行时,执行此查询大约需要50秒,并返回4,313行,这似乎是正确的行数。

知道我在这里做错了什么吗?

再次编辑:

SELECT tmp.distid, tmp.busctrid, tmp.volume, r.description, d.fname, d.lname, d.email
FROM
(
SELECT o.Distid, o.busctrid, SUM (ol.volume * ol.quantity) as Volume
FROM Orders o
INNER JOIN Orderlines ol
 ON ol.orderid = o.orderid
GROUP BY o.distid, o.busctrid
HAVING SUM (ol.volume * ol.quantity) BETWEEN 0 AND 3011
)tmp
INNER JOIN Distributor d
 ON d.distid = tmp.distid
INNER JOIN Rank r
 ON r.rankid = d.rankid
INNER JOIN FuncGetDownline(3,1) a
 ON a.distid = tmp.distid
 AND a.busctrid = tmp.busctrid
ORDER BY tmp.DistID, tmp.BusCtrID 

上述查询在1秒或更短的时间内执行,我也不会为什么这么快。我的高级开发人员和我可能已经花了大约60分钟对其进行微调,以便快速进行调整。

EDITED LONG LIST:

orders  PK_orders   Orderid
orders  PK_orders   Distid
orders  PK_orders   BusCtrID
orders  PK_orders   ShipAmt
orders  PK_orders   ShipDate
orders  PK_orders   ShipTrack
orders  PK_orders   TaxAmt
orders  PK_orders   invoicetype
orders  PK_orders   Notes
orders  PK_orders   PostAmount
orders  PK_orders   Status
orders  PK_orders   ShippingLine1
orders  PK_orders   ShippingLine2
orders  PK_orders   ShippingCity
orders  PK_orders   ShippingState
orders  PK_orders   ShippingCountry
orders  PK_orders   ShippingPostalCode
orders  PK_orders   EnteredBy
orders  PK_orders   PostFuturePeriod
orders  PK_orders   OrderDate
orders  PK_orders   MO_Ship
orders  PK_orders   ts
orders  PK_orders   ShippingPhone
orders  PK_orders   DatePaid
orders  PK_orders   PublicNotes
orders  PK_orders   LastUpdatedBy
orders  PK_orders   BonusDate
orders  PK_orders   TypeCode
orders  PK_orders   ShippingName
orders  PK_orders   GeoCode
orders  PK_orders   InOutCity
orders  PK_orders   TaxOption
orders  PK_orders   IsCredit
orders  PK_orders   TaxAmt2
orders  PK_orders   Aristo_Status
orders  PK_orders   WarehouseID
orders  PK_orders   ShipCost
orders  PK_orders   Weight
orders  PK_orders   Carrier
orders  PK_orders   Service
orders  PK_orders   OrderExported
orders  PK_orders   ShippingEmail
orders  PK_orders   CreditOrderID
orders  PK_orders   ShipMethod
orders  PK_orders   OrderID2
orders  PK_orders   PartyID
orders  PK_orders   ExportTimeStamp
orders  PK_orders   OrderIP
orders  PK_orders   PromoCode
orders  PK_orders   FirstOrder
Distributor Dist_PK DistID
Distributor Dist_PK Name
Distributor Dist_PK Status
Distributor Dist_PK Rank
Distributor Dist_PK OverRank
Distributor Dist_PK ORankDate
Distributor Dist_PK EnterDate
Distributor Dist_PK RenewalDate
Distributor Dist_PK Password
Distributor Dist_PK RankID
Distributor Dist_PK LName
Distributor Dist_PK FName
Distributor Dist_PK EnteredBy
Distributor Dist_PK MInitial
Distributor Dist_PK Email
Distributor Dist_PK RankDate
Distributor Dist_PK SSN
Distributor Dist_PK URL
Distributor Dist_PK HomePhone
Distributor Dist_PK WorkPhone
Distributor Dist_PK Fax
Distributor Dist_PK BillLine1
Distributor Dist_PK BillLine2
Distributor Dist_PK BillCity
Distributor Dist_PK BillState
Distributor Dist_PK BillPostalCode
Distributor Dist_PK BillCountry
Distributor Dist_PK ShipLine1
Distributor Dist_PK ShipLine2
Distributor Dist_PK ShipCity
Distributor Dist_PK ShipState
Distributor Dist_PK ShipPostalCode
Distributor Dist_PK ShipCountry
Distributor Dist_PK ts
Distributor Dist_PK FirstCycle
Distributor Dist_PK DistFlag1
Distributor Dist_PK DistFlag2
Distributor Dist_PK DistFlag3
Distributor Dist_PK DistFlag4
Distributor Dist_PK DistFlag5
Distributor Dist_PK DistFlag6
Distributor Dist_PK DistFlag7
Distributor Dist_PK HomePhoneExt
Distributor Dist_PK WorkPhoneExt
Distributor Dist_PK FaxExt
Distributor Dist_PK ExtraNumeric1
Distributor Dist_PK ExtraNumeric2
Distributor Dist_PK ExtraNumeric3
Distributor Dist_PK ExtraSring1
Distributor Dist_PK ExtraSring2
Distributor Dist_PK ExtraSring3
Distributor Dist_PK TaxExempt
Distributor Dist_PK txjr
Distributor Dist_PK GeoCode
Distributor Dist_PK InOutCity
Distributor Dist_PK DefaultWarehouse
Distributor Dist_PK TaxAsCustomer
Distributor Dist_PK CellPhone
Distributor Dist_PK PaidAsRank
Distributor Dist_PK TaxID
Distributor Dist_PK BankID
Distributor Dist_PK TrainerID
Distributor Dist_PK Statement
Distributor Dist_PK WebShowName
Distributor Dist_PK WebAddress
Distributor Dist_PK WebHomePhone
Distributor Dist_PK WebWorkPhone
Distributor Dist_PK WebFax
Distributor Dist_PK WebUrl
Distributor Dist_PK WebEmail
Distributor Dist_PK Photo
Distributor Dist_PK WebTemplate
Distributor Dist_PK HTMLEmail
Distributor Dist_PK UserDef1
Distributor Dist_PK UserDef2
Distributor Dist_PK UserDef3
Distributor Dist_PK UserDef4
Distributor Dist_PK UserDef5
Distributor Dist_PK UserDef6
Distributor Dist_PK UserDef7
Distributor Dist_PK UserDef8
Distributor Dist_PK UserDef9
Distributor Dist_PK UserDef10
Distributor Dist_PK UserDef11
Distributor Dist_PK UserDef12
Distributor Dist_PK UserDef13
Distributor Dist_PK UserDef14
Distributor Dist_PK UserDef15
Distributor Dist_PK EnableEcommerce
Distributor Dist_PK BirthDate
Distributor Dist_PK ModifiedBy
Distributor Dist_PK UserName
Distributor Dist_PK LastUpdateDate
Distributor Dist_PK ReceiveCompanyEmail
Distributor Dist_PK ReceiveUplineEmail
Distributor Dist_PK ReceiveUplineMessages
Distributor Dist_PK UserDef16
Distributor Dist_PK UserDef17
Distributor Dist_PK UserDef18
Distributor Dist_PK UserDef19
Distributor Dist_PK UserDef20
Distributor Dist_PK CreatedIP
Distributor Dist_PK UserDef21
Distributor Dist_PK UserDef22
Distributor Dist_PK UserDef23
Distributor Dist_PK OverrideMinChkAmt
Distributor Dist_PK ReceiveSponsorEmail
Distributor Dist_PK ReceiveDownlineActivityEmail
Distributor Dist_PK DefaultLanguage
Distributor Dist_PK DistID2
Distributor Dist_PK ImHandle
Distributor Dist_PK IMType
Distributor Dist_PK ReceivePersonallySponsoredActivityEmail
Distributor Dist_PK EnableWebsite
Distributor Dist_PK DistributorCenterExpireDate
Distributor Dist_PK WebsiteExpireDate
Distributor Dist_PK AutologinSessionID
Distributor Dist_PK RecoverPasswordKey
Distributor Dist_PK PCIUserID
Distributor Dist_PK TLPCountry
Distributor Dist_PK EncryptedSSN
Distributor Dist_PK EncryptedTaxID
Distributor Dist_PK AgreementAccepted
orderlines  PK_orderlines   OrderID
orderlines  PK_orderlines   OrderLineID
orderlines  PK_orderlines   ItemId
orderlines  PK_orderlines   Quantity
orderlines  PK_orderlines   Ship
orderlines  PK_orderlines   Amount
orderlines  PK_orderlines   Tax
orderlines  PK_orderlines   Weight
orderlines  PK_orderlines   StatusDate
orderlines  PK_orderlines   ShipDate
orderlines  PK_orderlines   Status
orderlines  PK_orderlines   Tracking1
orderlines  PK_orderlines   Tracking2
orderlines  PK_orderlines   RetailPrice
orderlines  PK_orderlines   WholesalePrice
orderlines  PK_orderlines   GroupItem
orderlines  PK_orderlines   Volume
orderlines  PK_orderlines   ts
orderlines  PK_orderlines   WarehouseID
orderlines  PK_orderlines   Notes
orderlines  PK_orderlines   ExtraCurrency1
orderlines  PK_orderlines   ExtraCurrency2
orderlines  PK_orderlines   ExtraCurrency3
orderlines  PK_orderlines   TaxRecID
orderlines  PK_orderlines   TrackingShipCo
orderlines  PK_orderlines   TaxRate
orderlines  PK_orderlines   MaxTaxAmt
orderlines  PK_orderlines   MaxOrderAmt
orderlines  PK_orderlines   CustomPrice
orderlines  PK_orderlines   DiscountPrice
orderlines  PK_orderlines   DiscountId
orderlines  PK_orderlines   DiscountDescription
orderlines  PK_orderlines   ShipCost
orderlines  PK_orderlines   Aristo_Status
orderlines  PK_orderlines   First_Time_Orderitem
orderlines  PK_orderlines   AttributeDetail
orderlines  PK_orderlines   Description
orderlines  PK_orderlines   BoxNumber

4 个答案:

答案 0 :(得分:3)

一些建议:

  • GROUP BY子句中的列是否已编入索引?如果没有,那么这将减慢查询速度。
  • “ID”列是否标记为主键?如果不是那么他们应该。在许多现代RDBMS中,标记为主键的列将自动索引
  • 您是否在外键上指定了索引?那是a.DistID,d.rankID等。如果没有,那么索引你的FK列将加快查询速度
  • 使用返回表的函数可能不是一个好主意。如果在SQL Server中执行此操作,则查询优化器无法优化该部分查询。

希望这有帮助。

答案 1 :(得分:3)

记录数量增加的原因是内部联接返回每个匹配的记录。

IE。

如果订单中有1个订单,订单行中有5个匹配的订单ID,则会返回5行。

所以你可能在一个点上有4416行,由于连接而很容易爆炸到更高的数字,然后由于你得到了你想要的数字

修改

Try this query.  
  SELECT 
    T.Name as TableName, I.name as IndexName,s.name as IndexColumn
FROM 
    sys.indexes I 
INNER JOIN 
    Sys.tables t 
    ON 
        t.object_id = I.Object_ID 
inner join
    Sys.columns s
    on
        t.object_id = s.object_id
WHERE 
    index_id >= 1
    and
    t.type = 'U'

这应该让我们知道您对索引的位置。

您也可以发布生成的执行计划吗?如果从SQL Server Management Studio运行查询,请右键单击查询编写区域并选择包含实际执行计划。

答案 2 :(得分:1)

要检查的第一件事是你在join子句中的所有列都有索引。

第二,你的SQL表示分组,因此在分组之前20K的数字可能是正确的。

但是,group by子句通常表示组中的某种类型的数学,例如sum()函数,我在您的sql中没有看到。您可以尝试使用distinct而不是group:

 SELECT DISTINCT a.DistID, a.BusCtrID, d.FName, d.LName, r.Description 
 FROM funcGetDownline( 3 , 1) a INNER JOIN Distributor d ON a.DistID =
 d.DistID INNER JOIN Rank r ON d.RankID = r.RankID INNER JOIN Orders o
 ON o.DistID = d.DistID INNER JOIN orderlines ol ON ol.OrderID =
 o.OrderID

答案 3 :(得分:1)

我不确定这一点,但看起来您不需要来自订单或订单行的数据,只是知道它们存在于此查询中,所以......

SELECT a.DistID, a.BusCtrID, d.FName, d.LName, r.Description  
FROM funcGetDownline( 3 , 1) a 
INNER JOIN Distributor d ON a.DistID = d.DistID 
INNER JOIN Rank r ON d.RankID = r.RankID 
where exists (select o.distid from  Orders o 
INNER JOIN orderlines ol ON ol.OrderID = o.OrderID 
where o.DistID = d.DistID )