我在使用sp_executesql执行查询时在SQL中看到它需要MINUTES来处理它,而我以正常方式运行相同的查询,声明参数在2秒内运行。
sp_executesql
EXEC Sp_executesql
N'SELECT TOP (27) [ARSalesPrice].[RecordID], [ARSalesPrice].[PriceType], [ARSalesPrice].[CustPriceClassID], [ARSalesPrice].[CustomerID], [ARSalesPrice].[InventoryID], [ARSalesPrice].[CuryID], [ARSalesPrice].[UOM], [ARSalesPrice].[IsPromotionalPrice], [ARSalesPrice].[EffectiveDate], [ARSalesPrice].[SalesPrice], [ARSalesPrice].[TaxID], [ARSalesPrice].[BreakQty], [ARSalesPrice].[ExpirationDate], [ARSalesPrice].[tstamp], [ARSalesPrice].[CreatedByID], [ARSalesPrice].[CreatedByScreenID], [ARSalesPrice].[CreatedDateTime], [ARSalesPrice].[LastModifiedByID], [ARSalesPrice].[LastModifiedByScreenID], [ARSalesPrice].[LastModifiedDateTime], [InventoryItem].[InventoryID], [InventoryItem].[InventoryCD], [InventoryItem].[Descr], [InventoryItem].[ItemClassID], [InventoryItem].[ItemStatus], [InventoryItem].[ItemType], [InventoryItem].[ValMethod], [InventoryItem].[TaxCategoryID], [InventoryItem].[BaseUnit], [InventoryItem].[SalesUnit], [InventoryItem].[PurchaseUnit], [InventoryItem].[Commisionable], [InventoryItem].[ReasonCodeSubID], [InventoryItem].[SalesAcctID], [InventoryItem].[SalesSubID], [InventoryItem].[InvtAcctID], [InventoryItem].[InvtSubID], [InventoryItem].[COGSAcctID], [InventoryItem].[COGSSubID], [InventoryItem].[DiscAcctID], [InventoryItem].[DiscSubID], [InventoryItem].[StdCstRevAcctID], [InventoryItem].[StdCstRevSubID], [InventoryItem].[StdCstVarAcctID], [InventoryItem].[StdCstVarSubID], [InventoryItem].[PPVAcctID], [InventoryItem].[PPVSubID], [InventoryItem].[POAccrualAcctID], [InventoryItem].[POAccrualSubID], [InventoryItem].[LCVarianceAcctID], [InventoryItem].[LCVarianceSubID], [InventoryItem].[DeferralAcctID], [InventoryItem].[DeferralSubID], [InventoryItem].[LastSiteID], [InventoryItem].[LastStdCost], [InventoryItem].[PendingStdCost], [InventoryItem].[PendingStdCostDate], [InventoryItem].[StdCost], [InventoryItem].[StdCostDate], [InventoryItem].[BasePrice], [InventoryItem].[BaseWeight], [InventoryItem].[BaseVolume], [InventoryItem].[BaseItemWeight], [InventoryItem].[BaseItemVolume], [InventoryItem].[WeightUOM], [InventoryItem].[VolumeUOM], [InventoryItem].[PackSeparately], [InventoryItem].[PackageOption], [InventoryItem].[PreferredVendorID], [InventoryItem].[PreferredVendorLocationID], [InventoryItem].[DefaultSubItemID], [InventoryItem].[DefaultSubItemOnEntry], [InventoryItem].[DfltSiteID], [InventoryItem].[DfltShipLocationID], [InventoryItem].[DfltReceiptLocationID], [InventoryItem].[ProductWorkgroupID], [InventoryItem].[ProductManagerID], [InventoryItem].[PriceWorkgroupID], [InventoryItem].[PriceManagerID], [InventoryItem].[StkItem], (SELECT TOP (1) ([INItemClass].[NegQty]) FROM INItemClass INItemClass WHERE (INItemClass.CompanyID = 3) AND INItemClass.DeletedDatabaseRecord = 0 AND ( [INItemClass].[ItemClassID] = [InventoryItem].[ItemClassID]) ORDER BY [INItemClass].[NegQty]), [InventoryItem].[LotSerClassID], [InventoryItem].[LotSerNumVal], [InventoryItem].[PostClassID], [InventoryItem].[DeferredCode], [InventoryItem].[DefaultTerm], [InventoryItem].[DefaultTermUOM], [InventoryItem].[PriceClassID], [InventoryItem].[IsSplitted], [InventoryItem].[UseParentSubID], [InventoryItem].[KitItem], [InventoryItem].[MinGrossProfitPct], [InventoryItem].[NonStockReceipt], [InventoryItem].[NonStockShip], [InventoryItem].[CompletePOLine], [InventoryItem].[ABCCodeID], [InventoryItem].[ABCCodeIsFixed], [InventoryItem].[MovementClassID], [InventoryItem].[MovementClassIsFixed], [InventoryItem].[MarkupPct], [InventoryItem].[RecPrice], [InventoryItem].[ImageUrl], [InventoryItem].[NoteID], NULL, NULL, NULL, [InventoryItem].[CreatedByID], [InventoryItem].[CreatedByScreenID], [InventoryItem].[CreatedDateTime], [InventoryItem].[LastModifiedByID], [InventoryItem].[LastModifiedByScreenID], [InventoryItem].[LastModifiedDateTime], [InventoryItem].[GroupMask], [InventoryItem].[CycleID], [InventoryItem].[tstamp], [InventoryItem].[IsRUTROTDeductible], [InventoryItem].[Body], [InventoryItem].[RUTROTType], [InventoryItem].[RUTROTItemType], [InventoryItem].[RUTROTWorkType], ( ( ( [InventoryItem].[UsrPartNumber] + N'' : '') + [InventoryItem].[UsrBrand])), [InventoryItem].[UsrSISupersededBy], [InventoryItem].[UsrPartNumber], [InventoryItem].[UsrBrand], [InventoryItem].[UsrFrgnName], [InventoryItem].[UsrSIPackSize], [InventoryItem].[UsrSICountryOfOrigin], [InventoryItem].[UsrSILength], [InventoryItem].[UsrSIWidth], [InventoryItem].[UsrSIHeight], [InventoryItem].[UsrSIAssocPartNumber], [InventoryItem].[UsrSISpecs], [InventoryItem].[UsrSIFactoryPartnumber], [InventoryItem].[UsrSIItemMovementClass], (SELECT TOP (1) ([INItemClass].[UsrSIMainGroup]) FROM INItemClass INItemClass WHERE (INItemClass.CompanyID = 3) AND INItemClass.DeletedDatabaseRecord = 0 AND ( [INItemClass].[ItemClassID] = [InventoryItem].[ItemClassID]) ORDER BY [INItemClass].[UsrSIMainGroup]), [InventoryItem].[UsrSIPrintDescr] FROM ARSalesPrice ARSalesPrice LEFT JOIN InventoryItem InventoryItem ON (InventoryItem.CompanyID = 3) AND InventoryItem.DeletedDatabaseRecord = 0 AND ( [InventoryItem].[InventoryID] = [ARSalesPrice].[InventoryID]) WHERE (ARSalesPrice.CompanyID = 3) AND [InventoryItem].[ItemStatus] <> N''IN'' AND [InventoryItem].[ItemStatus] <> N''DE'' AND ( @P0 = N''A'' OR [ARSalesPrice].[PriceType] = @P1) AND ( [ARSalesPrice].[CustomerID] = @P2 OR [ARSalesPrice].[CustPriceClassID] = @P3 OR @P4 IS NULL) AND ( [ARSalesPrice].[InventoryID] = @P5 OR @P6 IS NULL) AND ( ( ( [ARSalesPrice].[EffectiveDate] <= @P7 OR [ARSalesPrice].[EffectiveDate] IS NULL) AND ( [ARSalesPrice].[ExpirationDate] >= @P8 OR [ARSalesPrice].[ExpirationDate] IS NULL)) OR @P9 IS NULL) AND ( @P10 IS NULL OR @P11 = [InventoryItem].[ItemClassID]) AND ( @P12 IS NULL OR @P13 = [InventoryItem].[PriceClassID]) AND ( @P14 IS NULL OR @P15 = [InventoryItem].[PriceManagerID]) AND ( @P16 = CONVERT(BIT, 0) OR [InventoryItem].[PriceWorkgroupID] IN (SELECT EPCompanyTreeMember.workGroupID FROM EPCompanyTreeMember EPCompanyTreeMember WHERE (EPCompanyTreeMember.CompanyID = 3) AND EPCompanyTreeMember.userID= CONVERT(UNIQUEIDENTIFIER, ''b5344897-037e-4d58-b5c3-1bdfd0f47bf9'') AND EPCompanyTreeMember.active=1)) AND ( @P17 IS NULL OR @P18 = [InventoryItem].[PriceWorkgroupID]) ORDER BY (SELECT TOP (1) inventoryCD FROM InventoryItem InventoryItemExt WHERE (InventoryItemExt.CompanyID = 3) AND InventoryItemExt.DeletedDatabaseRecord = 0 AND [InventoryItemExt].[inventoryID] = [ARSalesPrice].[InventoryID]), CASE WHEN [ARSalesPrice].[PriceType] = ''B'' THEN ''Base'' WHEN [ARSalesPrice].[PriceType] = ''C'' THEN ''Customer'' WHEN [ARSalesPrice].[PriceType] = ''P'' THEN ''Customer Price Class'' END, [ARSalesPrice].[UOM], [ARSalesPrice].[BreakQty], [ARSalesPrice].[EffectiveDate], [ARSalesPrice].[RecordID] OPTION(OPTIMIZE FOR UNKNOWN) /* AR.20.20.00 */'
,
N'@P0 char(1),@P1 char(1),@P2 varchar(30),@P3 varchar(30),@P4 varchar(30),@P5 int,@P6 int,@P7 smalldatetime,@P8 smalldatetime,@P9 smalldatetime,@P10 varchar(10),@P11 varchar(10),@P12 varchar(10),@P13 varchar(10),@P14 uniqueidentifier,@P15 uniqueidentifier,@P16 bit,@P17 int,@P18 int'
,
@P0='A',
@P1='A',
@P2=NULL,
@P3=NULL,
@P4=NULL,
@P5=NULL,
@P6=NULL,
@P7=NULL,
@P8=NULL,
@P9=NULL,
@P10=NULL,
@P11=NULL,
@P12=NULL,
@P13=NULL,
@P14=NULL,
@P15=NULL,
@P16=0,
@P17=NULL,
@P18=NULL
需要几分钟才能完成。
在普通SQL中相同
DECLARE @P0 CHAR(1),
@P1 CHAR(1),
@P2 VARCHAR(30),
@P3 VARCHAR(30),
@P4 VARCHAR(30),
@P5 INT,
@P6 INT,
@P7 SMALLDATETIME,
@P8 SMALLDATETIME,
@P9 SMALLDATETIME,
@P10 VARCHAR(10),
@P11 VARCHAR(10),
@P12 VARCHAR(10),
@P13 VARCHAR(10),
@P14 UNIQUEIDENTIFIER,
@P15 UNIQUEIDENTIFIER,
@P16 BIT,
@P17 INT,
@P18 INT
SET @P0='A'
SET @P1='A'
SET @P2=NULL
SET @P3=NULL
SET @P4=NULL
SET @P5=NULL
SET @P6=NULL
SET @P7=NULL
SET @P8=NULL
SET @P9=NULL
SET @P10=NULL
SET @P11=NULL
SET @P12=NULL
SET @P13=NULL
SET @P14=NULL
SET @P15=NULL
SET @P16=0
SET @P17=NULL
SET @P18=NULL
SELECT TOP (27) [arsalesprice].[recordid],
[arsalesprice].[pricetype],
[arsalesprice].[custpriceclassid],
[arsalesprice].[customerid],
[arsalesprice].[inventoryid],
[arsalesprice].[curyid],
[arsalesprice].[uom],
[arsalesprice].[ispromotionalprice],
[arsalesprice].[effectivedate],
[arsalesprice].[salesprice],
[arsalesprice].[taxid],
[arsalesprice].[breakqty],
[arsalesprice].[expirationdate],
[arsalesprice].[tstamp],
[arsalesprice].[createdbyid],
[arsalesprice].[createdbyscreenid],
[arsalesprice].[createddatetime],
[arsalesprice].[lastmodifiedbyid],
[arsalesprice].[lastmodifiedbyscreenid],
[arsalesprice].[lastmodifieddatetime],
[inventoryitem].[inventoryid],
[inventoryitem].[inventorycd],
[inventoryitem].[descr],
[inventoryitem].[itemclassid],
[inventoryitem].[itemstatus],
[inventoryitem].[itemtype],
[inventoryitem].[valmethod],
[inventoryitem].[taxcategoryid],
[inventoryitem].[baseunit],
[inventoryitem].[salesunit],
[inventoryitem].[purchaseunit],
[inventoryitem].[commisionable],
[inventoryitem].[reasoncodesubid],
[inventoryitem].[salesacctid],
[inventoryitem].[salessubid],
[inventoryitem].[invtacctid],
[inventoryitem].[invtsubid],
[inventoryitem].[cogsacctid],
[inventoryitem].[cogssubid],
[inventoryitem].[discacctid],
[inventoryitem].[discsubid],
[inventoryitem].[stdcstrevacctid],
[inventoryitem].[stdcstrevsubid],
[inventoryitem].[stdcstvaracctid],
[inventoryitem].[stdcstvarsubid],
[inventoryitem].[ppvacctid],
[inventoryitem].[ppvsubid],
[inventoryitem].[poaccrualacctid],
[inventoryitem].[poaccrualsubid],
[inventoryitem].[lcvarianceacctid],
[inventoryitem].[lcvariancesubid],
[inventoryitem].[deferralacctid],
[inventoryitem].[deferralsubid],
[inventoryitem].[lastsiteid],
[inventoryitem].[laststdcost],
[inventoryitem].[pendingstdcost],
[inventoryitem].[pendingstdcostdate],
[inventoryitem].[stdcost],
[inventoryitem].[stdcostdate],
[inventoryitem].[baseprice],
[inventoryitem].[baseweight],
[inventoryitem].[basevolume],
[inventoryitem].[baseitemweight],
[inventoryitem].[baseitemvolume],
[inventoryitem].[weightuom],
[inventoryitem].[volumeuom],
[inventoryitem].[packseparately],
[inventoryitem].[packageoption],
[inventoryitem].[preferredvendorid],
[inventoryitem].[preferredvendorlocationid],
[inventoryitem].[defaultsubitemid],
[inventoryitem].[defaultsubitemonentry],
[inventoryitem].[dfltsiteid],
[inventoryitem].[dfltshiplocationid],
[inventoryitem].[dfltreceiptlocationid],
[inventoryitem].[productworkgroupid],
[inventoryitem].[productmanagerid],
[inventoryitem].[priceworkgroupid],
[inventoryitem].[pricemanagerid],
[inventoryitem].[stkitem],
(SELECT TOP (1) ( [initemclass].[negqty] )
FROM initemclass INItemClass
WHERE ( initemclass.companyid = 3 )
AND initemclass.deleteddatabaserecord = 0
AND ( [initemclass].[itemclassid] =
[inventoryitem].[itemclassid] )
ORDER BY [initemclass].[negqty]),
[inventoryitem].[lotserclassid],
[inventoryitem].[lotsernumval],
[inventoryitem].[postclassid],
[inventoryitem].[deferredcode],
[inventoryitem].[defaultterm],
[inventoryitem].[defaulttermuom],
[inventoryitem].[priceclassid],
[inventoryitem].[issplitted],
[inventoryitem].[useparentsubid],
[inventoryitem].[kititem],
[inventoryitem].[mingrossprofitpct],
[inventoryitem].[nonstockreceipt],
[inventoryitem].[nonstockship],
[inventoryitem].[completepoline],
[inventoryitem].[abccodeid],
[inventoryitem].[abccodeisfixed],
[inventoryitem].[movementclassid],
[inventoryitem].[movementclassisfixed],
[inventoryitem].[markuppct],
[inventoryitem].[recprice],
[inventoryitem].[imageurl],
[inventoryitem].[noteid],
NULL,
NULL,
NULL,
[inventoryitem].[createdbyid],
[inventoryitem].[createdbyscreenid],
[inventoryitem].[createddatetime],
[inventoryitem].[lastmodifiedbyid],
[inventoryitem].[lastmodifiedbyscreenid],
[inventoryitem].[lastmodifieddatetime],
[inventoryitem].[groupmask],
[inventoryitem].[cycleid],
[inventoryitem].[tstamp],
[inventoryitem].[isrutrotdeductible],
[inventoryitem].[body],
[inventoryitem].[rutrottype],
[inventoryitem].[rutrotitemtype],
[inventoryitem].[rutrotworktype],
(( ( [inventoryitem].[usrpartnumber] + ' : ' ) +
[inventoryitem].[usrbrand] )),
[inventoryitem].[usrsisupersededby],
[inventoryitem].[usrpartnumber],
[inventoryitem].[usrbrand],
[inventoryitem].[usrfrgnname],
[inventoryitem].[usrsipacksize],
[inventoryitem].[usrsicountryoforigin],
[inventoryitem].[usrsilength],
[inventoryitem].[usrsiwidth],
[inventoryitem].[usrsiheight],
[inventoryitem].[usrsiassocpartnumber],
[inventoryitem].[usrsispecs],
[inventoryitem].[usrsifactorypartnumber],
[inventoryitem].[usrsiitemmovementclass],
(SELECT TOP (1) ( [initemclass].[usrsimaingroup] )
FROM initemclass INItemClass
WHERE ( initemclass.companyid = 3 )
AND initemclass.deleteddatabaserecord = 0
AND ( [initemclass].[itemclassid] =
[inventoryitem].[itemclassid] )
ORDER BY [initemclass].[usrsimaingroup]),
[inventoryitem].[usrsiprintdescr]
FROM arsalesprice ARSalesPrice
LEFT JOIN inventoryitem InventoryItem
ON ( inventoryitem.companyid = 3 )
AND inventoryitem.deleteddatabaserecord = 0
AND ( [inventoryitem].[inventoryid] =
[arsalesprice].[inventoryid] )
WHERE ( arsalesprice.companyid = 3 )
AND [inventoryitem].[itemstatus] <> 'IN'
AND [inventoryitem].[itemstatus] <> 'DE'
AND ( @P0 = N'A'
OR [arsalesprice].[pricetype] = @P1 )
AND ( [arsalesprice].[customerid] = @P2
OR [arsalesprice].[custpriceclassid] = @P3
OR @P4 IS NULL )
AND ( [arsalesprice].[inventoryid] = @P5
OR @P6 IS NULL )
AND ( ( ( [arsalesprice].[effectivedate] <= @P7
OR [arsalesprice].[effectivedate] IS NULL )
AND ( [arsalesprice].[expirationdate] >= @P8
OR [arsalesprice].[expirationdate] IS NULL ) )
OR @P9 IS NULL )
AND ( @P10 IS NULL
OR @P11 = [inventoryitem].[itemclassid] )
AND ( @P12 IS NULL
OR @P13 = [inventoryitem].[priceclassid] )
AND ( @P14 IS NULL
OR @P15 = [inventoryitem].[pricemanagerid] )
AND ( @P16 = CONVERT(BIT, 0)
OR [inventoryitem].[priceworkgroupid] IN
(SELECT epcompanytreemember.workgroupid
FROM
epcompanytreemember EPCompanyTreeMember
WHERE (
epcompanytreemember.companyid = 3 )
AND
epcompanytreemember.userid =
CONVERT(UNIQUEIDENTIFIER,
'b5344897-037e-4d58-b5c3-1bdfd0f47bf9')
AND
epcompanytreemember.active = 1) )
AND ( @P17 IS NULL
OR @P18 = [inventoryitem].[priceworkgroupid] )
ORDER BY (SELECT TOP (1) inventorycd
FROM inventoryitem InventoryItemExt
WHERE ( InventoryItemExt.companyid = 3 )
AND InventoryItemExt.deleteddatabaserecord = 0
AND [InventoryItemExt].[inventoryid] =
[arsalesprice].[inventoryid]),
CASE
WHEN [arsalesprice].[pricetype] = 'B' THEN 'Base'
WHEN [arsalesprice].[pricetype] = 'C' THEN 'Customer'
WHEN [arsalesprice].[pricetype] = 'P' THEN 'Customer Price Class'
END,
[arsalesprice].[uom],
[arsalesprice].[breakqty],
[arsalesprice].[effectivedate],
[arsalesprice].[recordid]
OPTION(optimize FOR unknown) /* AR.20.20.00 */
运行只需2-3秒。
我在添加索引后尝试清除查询计划。仍然存在问题。 sp_executesql是由框架生成的,我正在尝试优化该查询结果。那么我能做些什么才能让它跑得更快? 有什么帮助吗?