Beware of the implicit data type conversions that can occur in the queries. If you are not careful, this can lead to performance issues. Consider this for an example:
If an indexed column is a string data-type column in a table and hence can have numbers as well as strings and alphanumerics and then if you specify:
where col1 = 100
in the where clause, this implicit conversion will lead to a scan since it does not know whether the value is a number, a string or an alphanumeric…see examples below (of course, DB2 will cry as soon as you do this since DB2 does not allow implicit conversions which in my opinion is a very good feature):
Example:
SQL Server:
create table my_test (col1 varchar(10) primary key)
go
/* I use table_x which is a very large table in one of our client’s schema - name changed because of proprietary code reasons*/
select identity(int, 1, 1) as row_num, * into #temp from table_x
insert into my_test select row_num from #temp
insert into my_test select locn_id from #temp
go
set showplan_text on
go
select * from my_test where col1 = 100
StmtText
——————————————————————————————————————————————————-
|–Parallelism(Gather Streams)
|–Clustered Index Scan(OBJECT:([Decipher].[dbo].[my_test].[PK__my_test__5749A5B5]), WHERE:(Convert([my_test].[col1])=Convert([@1])))
select * from my_test where col1 = ‘100′
StmtText
———————————————————————————————————————————————–
|–Clustered Index Seek(OBJECT:([Decipher].[dbo].[my_test].[PK__my_test__5749A5B5]), SEEK:([my_test].[col1]=[@1]) ORDERED FORWARD)
Oracle:
create table my_test (col1 varchar2(10) primary key)
/
create table temp_test as select rownum as row_num, locn_id from table_x
/
insert into my_test select row_num from temp_test
/
/*using analyze for quick and dirty test - use DBMS_STATS in real production*/
Analyze table my_test compute statistics for table for all indexes for all indexed columns;
/
set autotrace traceonly
/
select * from my_test where col1 = 100
/
1 0 INDEX (FULL SCAN) OF ‘SYS_C0090776′ (UNIQUE) (Cost=157 Card=1 Bytes=5)
select * from my_test where col1 = ‘100′
/
1 0 INDEX (UNIQUE SCAN) OF ‘SYS_C0090776′ (UNIQUE) (Cost=1 Card=1 Bytes=5)
Now, if on the other hand, you had the indexed column as a number data-type and provide a quoted string to it in the comparison in the where clause, then since the only value that it can ever have is a number, the optimizer is intelligent enough to take a number in a string and use the index after convert i.e. the convert is then done at the constant value rather than the indexed column.

