我的应用程序检查帐号字段中是否存在帐号。如果不存在,则为@AccountNumber参数输入'000000'。在SQLSMS中进行测试以检查执行时间时,我注意到使用合并检查值所花费的时间几乎是执行时间的3倍。由于两个查询都使用帐号,为什么下面的示例中的执行时间不完全相同?请注意,在第一个示例中,我结合使用了参数,在第二个示例中,我仅使用了帐号。
查询返回18,000行,每行: 加载37秒:
Declare @AccountNumber varchar(15) = '001234'
Declare @EmployeeNumber varchar (15) = ''
Declare @AccountType varchar (20) = 'Account Type'
select DISTINCT AccountTable.ACCOUNT_TYPE,
PayrollTable.EmployeeNumber,
AccountTable.ACCOUNT_NUMBER
from AccountTable
LEFT JOIN PayrollTable
ON AccountTable.ACCOUNT_NUMBER = PayrollTable.ACCOUNT_NUMBER
where (coalesce(@AccountNumber,'') = '000000' or AccountTable.ACCOUNT_NUMBER = @AccountNumber)
AND (coalesce(@EmployeeNumber,'') = '' or PayrollTable.EmployeeNumber = @EmployeeNumber)
AND (coalesce(@AccountType,'') = 'Account Type' or AccountTable.ACCOUNT_TYPE = @AccountType)
加载12秒:
Declare @AccountNumber varchar(15) = '001234'
Declare @EmployeeNumber varchar (15) = ''
Declare @AccountType varchar (20) = 'Account Type'
select DISTINCT AccountTable.ACCOUNT_TYPE,
PayrollTable.EmployeeNumber,
AccountTable.ACCOUNT_NUMBER
from AccountTable
LEFT JOIN PayrollTable
ON AccountTable.ACCOUNT_NUMBER = PayrollTable.ACCOUNT_NUMBER
where (AccountTable.ACCOUNT_NUMBER = @AccountNumber)
AND (coalesce(@EmployeeNumber,'') = '' or PayrollTable.EmployeeNumber = @EmployeeNumber)
AND (coalesce(@AccountType,'') = 'Account Type' or AccountTable.ACCOUNT_TYPE = @AccountType)