1 Mart 2011 Salı

T-SQL String Veri Tipleri ve SQL Server 2008 R2 Üzerinde Performans Kıyaslaması

Character Strings

  • char Definition: Fixed-Length character string.
    Declaration: char(n).
    n = the number of characters.

    Space: n number of bytes

    Valid lengths for a char datatype are 1 through 8,000.
  • varchar Definition: Variable-Length character string.
    Declaration: varchar(n | max).
    n = the number of characters.

    Space: characters actually used in datatype (1 byte per character) + 2 additional bytes

    Valid lengths for a varchar datatype are 1 through 8,000. Alternatively, MAX allows for a much larger maximum storage size (2^31-1 bytes - 2,147,483,647 characters).
  • text Definition: Variable-Length character string in the code page of the server.

    Maximum length is 2,147,483,647 characters.
    Note: will be removed in future versions. Use varchar(max) instead.

Unicode Character Strings

  • nchar Definition: Fixed-Length Unicode character string.
    Declaration: nchar(n).
    n = the number of characters.

    Space: n * 2 number of bytes

    Valid lengths for a char datatype are 1 through 4,000.
  • nvarchar Definition: Variable-Length Unicode character string.
    Declaration: nvarchar(n | max).
    n = the number of characters.

    Space: characters actually used in datatype (2 bytes per character) + 2 additional bytes

    Valid lengths for a varchar datatype are 1 through 4,000. Alternatively MAX indicates that the maximum storage size is much larger (2^31-1 bytes - 2,147,483,647 characters).
  • ntext Definition: Variable-Length character string in the code page of the server.

    Maximum length is 1,073,741,823 characters.
    Note: will be removed in future versions. Use nvarchar(max) instead.


I thought I would try to do a few quick tests to compare the performance of assigning values and comparing values using some of the different string data types that you have available in SQL Server. It turns out that there is a significant difference in performance for these operations depending on the data type of the variable that you use. Using varchar(MAX) and char(8000) are much slower than using varchar(8000) or char(3).
Of course if you are writing T-SQL that is doing millions of loops, you are probably “thinking like a developer” instead of thinking like a good DBA. In my opinion, you really should not be writing T-SQL code that has lots of complicated business logic or that needs to do high numbers of loops. One exception to this would be if you are doing some sort of operation where you need to delete or update millions of rows in a table, but you can’t afford to take the concurrency hit that a single, set-based delete or update would cause.
It is much more important to match the data types that you use for input parameters and variables to the data type that you have for the corresponding column in your table or view, so you can avoid very expensive implicit data type conversions.
Code Snippet
  1. — Note: These scripts only work on SQL Server 2008 and 2008 R2
  2. — since I am doing things like declaring and assigning a value
  3. — to a variable in a single line and using the += operator
  4. — My timings were on an Intel 2.83GHz Core2 Quad Q9550
  5. — Assignment Tests *******************************************************
  6. — Assign a value to char(3)
  7. DECLARE @StringValue char(3);
  8. DECLARE @IterationCount int = 0;
  9. DECLARE @StartTime datetime = GETDATE();
  10. WHILE @IterationCount < 1000000
  11.     BEGIN
  12.       SET @StringValue = ‘abc’;
  13.       SET @IterationCount += 1;
  14.     END
  15. – 966ms
  16. SELECT DATEDIFF(ms, @StartTime, GETDATE()) AS [char(3) Elapsed Time (ms)];
  17. GO
  18. – Assign a value to varchar(8000)
  19. DECLARE @StringValue varchar(8000);
  20. DECLARE @IterationCount int = 0;
  21. DECLARE @StartTime datetime = GETDATE();
  22. WHILE @IterationCount < 1000000
  23.     BEGIN
  24.       SET @StringValue = ‘abc’;
  25.       SET @IterationCount += 1;
  26.     END
  27. – 966ms
  28. SELECT DATEDIFF(ms, @StartTime, GETDATE()) AS [varchar(8000) Elapsed Time (ms)];
  29. GO
  30. – Assign a value to nvarchar(4000)
  31. DECLARE @StringValue nvarchar(4000);
  32. DECLARE @IterationCount int = 0;
  33. DECLARE @StartTime datetime = GETDATE();
  34. WHILE @IterationCount < 1000000
  35.     BEGIN
  36.       SET @StringValue = N’abc’;
  37.       SET @IterationCount += 1;
  38.     END
  39. – 970ms
  40. SELECT DATEDIFF(ms, @StartTime, GETDATE()) AS [nvarchar(4000) Elapsed Time (ms)];
  41. GO
  42. – Assign a value to char(8000)
  43. DECLARE @StringValue char(8000);
  44. DECLARE @IterationCount int = 0;
  45. DECLARE @StartTime datetime = GETDATE();
  46. WHILE @IterationCount < 1000000
  47.     BEGIN
  48.       SET @StringValue = ‘abc’;
  49.       SET @IterationCount += 1;
  50.     END
  51. – 1670ms
  52. SELECT DATEDIFF(ms, @StartTime, GETDATE()) AS [char(8000) Elapsed Time (ms)];
  53. GO
  54. – Assign a value to varchar(MAX)
  55. DECLARE @StringValue varchar(MAX);
  56. DECLARE @IterationCount int = 0;
  57. DECLARE @StartTime datetime = GETDATE();
  58. WHILE @IterationCount < 1000000
  59.     BEGIN
  60.       SET @StringValue = ‘abc’;
  61.       SET @IterationCount += 1;
  62.     END
  63. – 2380ms
  64. SELECT DATEDIFF(ms, @StartTime, GETDATE()) AS [varchar(MAX) Elapsed Time (ms)];
  65. GO
  66. – End of Assignment Tests
  67. — Comparision Tests **************************************
  68. — Char (3)
  69. DECLARE @Result bit = 0;
  70. DECLARE @StringValue char(3) = ‘abc’;
  71. DECLARE @IterationCount int = 0;
  72. DECLARE @StartTime datetime = GETDATE();
  73. WHILE @IterationCount < 1000000
  74.     BEGIN
  75.       SET @Result = CASE
  76.                         WHEN @StringValue = ‘abc’ THEN 1
  77.                         ELSE 0
  78.                     END;
  79.       SET @IterationCount += 1;
  80.     END
  81. – 1170ms
  82. SELECT DATEDIFF(ms, @StartTime, GETDATE())AS [char(3) Elapsed Time (ms)];
  83. GO
  84. – Varchar (8000)
  85. DECLARE @Result bit = 0;
  86. DECLARE @StringValue varchar(8000) = ‘abc’;
  87. DECLARE @IterationCount int = 0;
  88. DECLARE @StartTime datetime = GETDATE();
  89. WHILE @IterationCount < 1000000
  90.     BEGIN
  91.       SET @Result = CASE
  92.                         WHEN @StringValue = ‘abc’ THEN 1
  93.                         ELSE 0
  94.                     END;
  95.       SET @IterationCount += 1;
  96.     END
  97. – 1200ms
  98. SELECT DATEDIFF(ms, @StartTime, GETDATE())AS [varchar(8000) Elapsed Time (ms)];
  99. GO
  100. – varchar(MAX)
  101. DECLARE @Result bit = 0;
  102. DECLARE @StringValue varchar(MAX) = ‘abc’;
  103. DECLARE @IterationCount int = 0;
  104. DECLARE @StartTime datetime = GETDATE();
  105. WHILE @IterationCount < 1000000
  106.     BEGIN
  107.       SET @Result = CASE
  108.                         WHEN @StringValue = ‘abc’ THEN 1
  109.                         ELSE 0
  110.                     END;
  111.       SET @IterationCount += 1;
  112.     END
  113. – 2106ms
  114. SELECT DATEDIFF(ms, @StartTime, GETDATE()) AS [varchar(MAX) Elapsed Time (ms)];
  115. GO
  116. – Caution: This will take a long time!
  117. — Char (8000)
  118. DECLARE @Result bit = 0;
  119. DECLARE @StringValue char(8000) = ‘abc’;
  120. DECLARE @IterationCount int = 0;
  121. DECLARE @StartTime datetime = GETDATE();
  122. WHILE @IterationCount < 1000000
  123.     BEGIN
  124.       SET @Result = CASE
  125.                         WHEN @StringValue = ‘abc’ THEN 1
  126.                         ELSE 0
  127.                     END;
  128.       SET @IterationCount += 1;
  129.     END
  130. – 317333ms
  131. SELECT DATEDIFF(ms, @StartTime, GETDATE())AS [char(8000) Elapsed Time (ms)];
  132. GO
  133. – End of Comparision Tests

Hiç yorum yok:

Yorum Gönder