Шнуго сделал замечание, что TRY_CAST
должно быть быстрее, поэтому я подумал, что лучше, чем тестировать, и из «быстрого» теста я согласен.
Во-первых, короткая настройкачтобы создать некоторые данные:
USE Sandbox;
GO
DECLARE @SQL nvarchar(MAX);
CREATE TABLE TallyTable (I int);
WITH N AS(
SELECT N
FROM (VALUES(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL)) N(N)),
Tally AS(
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS I
FROM N N1
CROSS JOIN N N2
CROSS JOIN N N3)
INSERT INTO dbo.TallyTable (I)
SELECT I
FROM Tally;
SELECT @SQL =
N'CREATE TABLE SampleTable (' +
STUFF((SELECT TOP 100
N',' + NCHAR(10) +
N' ' + QUOTENAME(CONCAT(N'Col',T.I)) + N' varchar(10)'
FROM TallyTable T
FOR XML PATH(N'')),1,1,N'') + NCHAR(10) + N');';
EXEC sp_executesql @SQL;
SELECT @SQL =
N'INSERT INTO SampleTable' + NCHAR(10) +
N'VALUES(' + STUFF((SELECT TOP 100 ',0'
FROM TallyTable T
FOR XML PATH(N'')),1,1,N'') + N');';
EXEC sp_executesql @SQL;
GO
Это создает таблицу из 100 столбцов со значением «0».Теперь, если мы попробуем TRY_CAST
и TRY_PARSE
:
SET STATISTICS TIME ON;
PRINT 'TRY_PARSE';
SELECT TRY_PARSE(Col1 AS float),
TRY_PARSE(Col2 AS float),
TRY_PARSE(Col3 AS float),
TRY_PARSE(Col4 AS float),
TRY_PARSE(Col5 AS float),
TRY_PARSE(Col6 AS float),
TRY_PARSE(Col7 AS float),
TRY_PARSE(Col8 AS float),
TRY_PARSE(Col9 AS float),
TRY_PARSE(Col10 AS float),
TRY_PARSE(Col11 AS float),
TRY_PARSE(Col12 AS float),
TRY_PARSE(Col13 AS float),
TRY_PARSE(Col14 AS float),
TRY_PARSE(Col15 AS float),
TRY_PARSE(Col16 AS float),
TRY_PARSE(Col17 AS float),
TRY_PARSE(Col18 AS float),
TRY_PARSE(Col19 AS float),
TRY_PARSE(Col20 AS float),
TRY_PARSE(Col21 AS float),
TRY_PARSE(Col22 AS float),
TRY_PARSE(Col23 AS float),
TRY_PARSE(Col24 AS float),
TRY_PARSE(Col25 AS float),
TRY_PARSE(Col26 AS float),
TRY_PARSE(Col27 AS float),
TRY_PARSE(Col28 AS float),
TRY_PARSE(Col29 AS float),
TRY_PARSE(Col30 AS float),
TRY_PARSE(Col31 AS float),
TRY_PARSE(Col32 AS float),
TRY_PARSE(Col33 AS float),
TRY_PARSE(Col34 AS float),
TRY_PARSE(Col35 AS float),
TRY_PARSE(Col36 AS float),
TRY_PARSE(Col37 AS float),
TRY_PARSE(Col38 AS float),
TRY_PARSE(Col39 AS float),
TRY_PARSE(Col40 AS float),
TRY_PARSE(Col41 AS float),
TRY_PARSE(Col42 AS float),
TRY_PARSE(Col43 AS float),
TRY_PARSE(Col44 AS float),
TRY_PARSE(Col45 AS float),
TRY_PARSE(Col46 AS float),
TRY_PARSE(Col47 AS float),
TRY_PARSE(Col48 AS float),
TRY_PARSE(Col49 AS float),
TRY_PARSE(Col50 AS float),
TRY_PARSE(Col51 AS float),
TRY_PARSE(Col52 AS float),
TRY_PARSE(Col53 AS float),
TRY_PARSE(Col54 AS float),
TRY_PARSE(Col55 AS float),
TRY_PARSE(Col56 AS float),
TRY_PARSE(Col57 AS float),
TRY_PARSE(Col58 AS float),
TRY_PARSE(Col59 AS float),
TRY_PARSE(Col60 AS float),
TRY_PARSE(Col61 AS float),
TRY_PARSE(Col62 AS float),
TRY_PARSE(Col63 AS float),
TRY_PARSE(Col64 AS float),
TRY_PARSE(Col65 AS float),
TRY_PARSE(Col66 AS float),
TRY_PARSE(Col67 AS float),
TRY_PARSE(Col68 AS float),
TRY_PARSE(Col69 AS float),
TRY_PARSE(Col70 AS float),
TRY_PARSE(Col71 AS float),
TRY_PARSE(Col72 AS float),
TRY_PARSE(Col73 AS float),
TRY_PARSE(Col74 AS float),
TRY_PARSE(Col75 AS float),
TRY_PARSE(Col76 AS float),
TRY_PARSE(Col77 AS float),
TRY_PARSE(Col78 AS float),
TRY_PARSE(Col79 AS float),
TRY_PARSE(Col80 AS float),
TRY_PARSE(Col81 AS float),
TRY_PARSE(Col82 AS float),
TRY_PARSE(Col83 AS float),
TRY_PARSE(Col84 AS float),
TRY_PARSE(Col85 AS float),
TRY_PARSE(Col86 AS float),
TRY_PARSE(Col87 AS float),
TRY_PARSE(Col88 AS float),
TRY_PARSE(Col89 AS float),
TRY_PARSE(Col90 AS float),
TRY_PARSE(Col91 AS float),
TRY_PARSE(Col92 AS float),
TRY_PARSE(Col93 AS float),
TRY_PARSE(Col94 AS float),
TRY_PARSE(Col95 AS float),
TRY_PARSE(Col96 AS float),
TRY_PARSE(Col97 AS float),
TRY_PARSE(Col98 AS float),
TRY_PARSE(Col99 AS float),
TRY_PARSE(Col100 AS float)
FROM dbo.SampleTable;
PRINT 'TRY_CAST';
SELECT TRY_CAST(Col1 AS float),
TRY_CAST(Col2 AS float),
TRY_CAST(Col3 AS float),
TRY_CAST(Col4 AS float),
TRY_CAST(Col5 AS float),
TRY_CAST(Col6 AS float),
TRY_CAST(Col7 AS float),
TRY_CAST(Col8 AS float),
TRY_CAST(Col9 AS float),
TRY_CAST(Col10 AS float),
TRY_CAST(Col11 AS float),
TRY_CAST(Col12 AS float),
TRY_CAST(Col13 AS float),
TRY_CAST(Col14 AS float),
TRY_CAST(Col15 AS float),
TRY_CAST(Col16 AS float),
TRY_CAST(Col17 AS float),
TRY_CAST(Col18 AS float),
TRY_CAST(Col19 AS float),
TRY_CAST(Col20 AS float),
TRY_CAST(Col21 AS float),
TRY_CAST(Col22 AS float),
TRY_CAST(Col23 AS float),
TRY_CAST(Col24 AS float),
TRY_CAST(Col25 AS float),
TRY_CAST(Col26 AS float),
TRY_CAST(Col27 AS float),
TRY_CAST(Col28 AS float),
TRY_CAST(Col29 AS float),
TRY_CAST(Col30 AS float),
TRY_CAST(Col31 AS float),
TRY_CAST(Col32 AS float),
TRY_CAST(Col33 AS float),
TRY_CAST(Col34 AS float),
TRY_CAST(Col35 AS float),
TRY_CAST(Col36 AS float),
TRY_CAST(Col37 AS float),
TRY_CAST(Col38 AS float),
TRY_CAST(Col39 AS float),
TRY_CAST(Col40 AS float),
TRY_CAST(Col41 AS float),
TRY_CAST(Col42 AS float),
TRY_CAST(Col43 AS float),
TRY_CAST(Col44 AS float),
TRY_CAST(Col45 AS float),
TRY_CAST(Col46 AS float),
TRY_CAST(Col47 AS float),
TRY_CAST(Col48 AS float),
TRY_CAST(Col49 AS float),
TRY_CAST(Col50 AS float),
TRY_CAST(Col51 AS float),
TRY_CAST(Col52 AS float),
TRY_CAST(Col53 AS float),
TRY_CAST(Col54 AS float),
TRY_CAST(Col55 AS float),
TRY_CAST(Col56 AS float),
TRY_CAST(Col57 AS float),
TRY_CAST(Col58 AS float),
TRY_CAST(Col59 AS float),
TRY_CAST(Col60 AS float),
TRY_CAST(Col61 AS float),
TRY_CAST(Col62 AS float),
TRY_CAST(Col63 AS float),
TRY_CAST(Col64 AS float),
TRY_CAST(Col65 AS float),
TRY_CAST(Col66 AS float),
TRY_CAST(Col67 AS float),
TRY_CAST(Col68 AS float),
TRY_CAST(Col69 AS float),
TRY_CAST(Col70 AS float),
TRY_CAST(Col71 AS float),
TRY_CAST(Col72 AS float),
TRY_CAST(Col73 AS float),
TRY_CAST(Col74 AS float),
TRY_CAST(Col75 AS float),
TRY_CAST(Col76 AS float),
TRY_CAST(Col77 AS float),
TRY_CAST(Col78 AS float),
TRY_CAST(Col79 AS float),
TRY_CAST(Col80 AS float),
TRY_CAST(Col81 AS float),
TRY_CAST(Col82 AS float),
TRY_CAST(Col83 AS float),
TRY_CAST(Col84 AS float),
TRY_CAST(Col85 AS float),
TRY_CAST(Col86 AS float),
TRY_CAST(Col87 AS float),
TRY_CAST(Col88 AS float),
TRY_CAST(Col89 AS float),
TRY_CAST(Col90 AS float),
TRY_CAST(Col91 AS float),
TRY_CAST(Col92 AS float),
TRY_CAST(Col93 AS float),
TRY_CAST(Col94 AS float),
TRY_CAST(Col95 AS float),
TRY_CAST(Col96 AS float),
TRY_CAST(Col97 AS float),
TRY_CAST(Col98 AS float),
TRY_CAST(Col99 AS float),
TRY_CAST(Col100 AS float)
FROM dbo.SampleTable;
SET STATISTICS TIME OFF;
Результаты выше незначительны, но все равно показывает:
TRY_PARSE
SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 0 ms.
(1 row affected)
SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 4 ms.
TRY_CAST
SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 0 ms.
(1 row affected)
SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 0 ms.
Это 4ms отличается от 1ряд из 100 столбцов.Тогда давайте увеличим объем (это займет некоторое время):
DECLARE @SQL nvarchar(MAX);
SELECT @SQL =
N'INSERT INTO SampleTable' + NCHAR(10) +
N'VALUES' +STUFF((SELECT N',' + NCHAR(10) +
N' (' + STUFF((SELECT TOP 100 ',0'
FROM TallyTable T
FOR XML PATH(N'')),1,1,N'') + N')'
FROM TallyTable T
FOR XML PATH(N'')),1,1,N'') + N';';
SELECT @SQL;
EXEC sp_executesql @SQL;
EXEC sp_executesql @SQL;
EXEC sp_executesql @SQL;
EXEC sp_executesql @SQL;
EXEC sp_executesql @SQL;
--Now we have 5001 rows
Теперь у нас есть 5 001 строка и 100 столбцов.Теперь, если мы снова запустим описанное выше ...
TRY_PARSE
SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 0 ms.
(5001 rows affected)
SQL Server Execution Times:
CPU time = 14672 ms, elapsed time = 18690 ms.
TRY_CAST
SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 0 ms.
(5001 rows affected)
SQL Server Execution Times:
CPU time = 125 ms, elapsed time = 671 ms.
Ошеломляющая разница в 18019 мс (это на 18 секунд или почти в 28 раз быстрее!).
Итак, ответ таков:используйте TRY_CAST
(или TRY_CONVERT
, что, я уверен, будет так же быстро).
--cleanup
DROP TABLE dbo.TallyTable;
DROP TABLE SampleTable;