Article ID: 198625 - Last Review: November 21, 2003 - Revision: 3.1 INF: Optimizing Comparisons with Numeric, Integer, and Others
This article was previously published under Q198625 SUMMARY
Data type optimization with numeric, decimal, and integer data types use some very strict rules. This article explains and clarifies the conditions and constraints that the optimizer uses to perform exact numeric data type comparisons. Review the following section to help clarify the terms and concepts used in this article.
"Precision" is the number of digits in a number. "Scale" is the number of digits to the right of the decimal point in a number. For example, the number 123.45 has a precision of 5 and a scale of 2. Due to limitations with the binary numbering system used by computers, some decimal fractions simply cannot be represented exactly. For example, the decimal fraction 0.1 does not have an exact binary representation. It can only be approximated. It is for this reason that floating point and real data types are considered approximate values; whereas integer, numeric, and decimal data types are treated as exact data types. The terms "strict" or "exact" are referred to in their computational definition. For example, a numeric(10,1) will not exactly or strictly compare with a numeric(10,2) even if the numbers involved are mathematically equivalent. Mathematically, a value of 10.1 and 10.10 are exactly the same. However, due to the difference in scale, they are not treated as a computationally exact match. MORE INFORMATION
To understand the optimizer's comparison choices, you must first understand the way incoming data is parsed and handled.
The optimizer makes a choice when it decides to complete the plan: is the incoming argument more or less precise than the table definition? If the argument value is more precise than the column data, the column data must be promoted to the argument precision and scale. This requires the conversion of the data in the column, and can result in plans than contain table scans. If the argument value is less precise than the column data, the argument can be promoted to the precision and scale of the column. This generally results in a plan that can use an index or more direct approach to the data retrieval effort. To help clarify this concept, some examples are given below. Example 1
Consider the following as an example: All numeric values with scale less than 10 and precision 0 can be stored as a tinyint, smallint, or integer type, taking 1, 2, and 4 bytes of storage, respectively. By comparison, even the smallest numeric value will take 5 bytes, with more storage required as the precision increases. The following table shows the mapping between the types. Collapse this table
In addition to the benefits available by using indexes more effectively, you may also save considerable space by using one of these data types where appropriate. SQL Server 7.0 adds the ALTER TABLE .. ALTER COLUMN statements, which can be used to dynamically change the data type of a column. In earlier versions of SQL Server, this conversion can be done only by creating a new table with the desired definitions and doing an INSERT .. SELECT to populate the table. Note that this change may require you to update any triggers, stored procedures, or other code that may use variables expecting the column to be a numeric type.
| Other Resources Other Support Sites
CommunityGet Help NowArticle Translations
|





















Back to the top