有没有办法优化我的查询?当我添加这个参数时,我的存储过程会增加6秒来输出我的结果。
SET @primary_name =
ISNULL(
STUFF((SELECT
'Convert(nvarchar(4000),' + column_name + ')+'',,''+'
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE
OBJECTPROPERTY(OBJECT_ID(constraint_name), 'IsPrimaryKey') = 1
AND QUOTENAME(table_schema) + '.' + QUOTENAME(table_name) = @table_name
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)')
,1,0,'')
,'ISNULL')
SET @primary_name = (LEFT(@primary_name,LEN(@primary_name) -6 ))
IF @primary_name = ''
BEGIN
SET @primary_name = ''''''
END
答案 0 :(得分:0)
您可以尝试删除ISNULL并在第二次SET操作中执行吗?
答案 1 :(得分:0)
刚刚执行的代码减少了6秒@TavoloPerUno
SELECT @primary_name = (SELECT top 1 QUOTENAME(keys.COLUMN_NAME) FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE keys
WHERE keys.TABLE_NAME =PARSENAME(@table_name, 1)
and keys.TABLE_SCHEMA = PARSENAME(@table_name, 2)
and OBJECTPROPERTY(OBJECT_ID(constraint_name), 'IsPrimaryKey') = 1)