INF: Optimizing Comparisons with Numeric, Integer, and Others

ID: Q198625


The information in this article applies to:


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.

Exact data types require "strict" comparison. Unlike a float or real data type, rounding is not acceptable to maintain the integrity of the data type and associated comparisons.

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:


   create table tblTest( a numeric(8,0) PRIMARY KEY, b int)
   select * from tblTest where a = 123 


Referring back to the parsing rules, the 123 is fewer than 10 digits and it does not contain a decimal point. Therefore: Example 2:


   create table tblTest( a numeric(10,0) PRIMARY KEY, b int)
   select * from tblTest where a = 123. 


Example 3:


   create table tblTest( a numeric(10,0) PRIMARY KEY, b int)
   select * from tblTest where a = 123.0 


Remember, the table definition was of an exact data type. If the server attempted to do rounding, it would not maintain the rules of an exact data type and would result in invalid result processing.

Consider the following as an example:


   create table tblTest( a numeric(10,1) PRIMARY KEY, b int)
   select * from tblTest where a = 123.15 


If the SQL Server did not chose a plan to convert the column data of the incoming argument, (n,2) precision and scale, but instead elected to round the argument to the column precision and scale, it would elect 123.2 when the user most likely wanted to update 123.1. In this case, there should be no match.

Additional query words: datatype datatypes


Keywords          : SSrvTran_SQL kbbug6.50.sp2 kbbug6.50.sp3 kbbug6.50.sp4 
Version           : winnt:6.5
Platform          : winnt 
Issue type        : kbinfo 

Last Reviewed: February 5, 1999