这里只是我想知道临时表中的记录数并传递给另一个变量,以便它在执行计划表中显示扫描大约100%
请找到我正在做的以下查询
DECLARE @tmpANHdr TABLE (
hdrId INT IDENTITY,
CBFId NVARCHAR(32),
ACPT_REJ_FLAG NVARCHAR(8),
PROC_FILE_NAME NVARCHAR(50))
INSERT INTO @tmpANHdr
SELECT TOP 100 AHR_CB_FTS_FILE_ID,
AHR_ACCT_REJ_FLAG,
AHR_PROC_FILE_NAME
FROM FTS_ACK_NAK_HEADER WITH (NOLOCK)
WHERE AHR_FLAG IS NULL
OR AHR_FLAG = 0
DECLARE @varRecordCount INT
SELECT @varRecordCount = Count(1)
FROM @tmpANHdr
SET @varIndex = 1
IF( @varIndex <= @varRecordCount )
BEGIN
PRINT 'hi'
END
答案 0 :(得分:2)
对不超过100行的表变量进行扫描可能相当便宜。表扫描操作的成本可能是该语句计划的100%,但这是一个非常小的数字的100%。
您可以完全避免这笔费用,只需在插入后查看@@rowcount
,如下所示。
DECLARE @varRecordCount INT;
INSERT INTO @tmpANHdr
SELECT TOP 100 AHR_CB_FTS_FILE_ID,
AHR_ACCT_REJ_FLAG,
AHR_PROC_FILE_NAME
FROM FTS_ACK_NAK_HEADER WITH (NOLOCK)
WHERE AHR_FLAG IS NULL
OR AHR_FLAG = 0
SET @varRecordCount = @@ROWCOUNT
答案 1 :(得分:2)
您可以在标识列上放置主键(索引),这可能会使其表现更好:
DECLARE @tmpANHdr TABLE (
hdrId INT IDENTITY PRIMARY KEY,
CBFId NVARCHAR(32),
ACPT_REJ_FLAG NVARCHAR(8),
PROC_FILE_NAME NVARCHAR(50))
另外,你最终还是将表变量用于其他任何事情吗?为什么不这样做?
DECLARE @varRecordCount INT
SELECT @varRecordCount = Count(TOP 100 *)
FROM FTS_ACK_NAK_HEADER WITH (NOLOCK)
WHERE AHR_FLAG IS NULL
OR AHR_FLAG = 0