在SQL Server的Where子句中使用变量

时间:2017-12-21 12:04:29

标签: sql-server

当我在Where子句中使用Delcared变量时查询需要时间,但是当我使用常量值时,它没有。

随着变量 - 15秒

DECLARE @V_EXTERNALCONTRACTNBR varchar(100),
        @V_REGIONALID int

SET @V_EXTERNALCONTRACTNBR = '310046'
SET @V_REGIONALID = 10

SELECT
  C.CONTRACT_ID AS CONTRACTID,
  C.CONTRACT_STS AS CONTRACTSTATUS,
  C.LIVE_STS AS LIVESTS,
  CC.BUSINESS_PARTNER_ID AS BUSINESSPARTNERID
FROM CONT C (NOLOCK)
INNER JOIN CUST_CONT CC (NOLOCK)
  ON C.REGIONAL_ID = CC.REGIONAL_ID
  AND C.CONTRACT_ID = CC.CONTRACT_ID
WHERE C.REGIONAL_ID = @V_REGIONALID
AND CC.REL_ROLE_CDE = 'B'
AND C.EXTERNAL_CONTRACT_NBR = @V_EXTERNALCONTRACTNBR

使用Conatant值-1秒

SELECT
  C.CONTRACT_ID AS CONTRACTID,
  C.CONTRACT_STS AS CONTRACTSTATUS,
  C.LIVE_STS AS LIVESTS,
  CC.BUSINESS_PARTNER_ID AS BUSINESSPARTNERID
FROM CONT C (NOLOCK)
INNER JOIN CUST_CONT CC (NOLOCK)
  ON C.REGIONAL_ID = CC.REGIONAL_ID
  AND C.CONTRACT_ID = CC.CONTRACT_ID
WHERE C.REGIONAL_ID = 10
AND CC.REL_ROLE_CDE = 'B'
AND C.EXTERNAL_CONTRACT_NBR = '310046'

0 个答案:

没有答案