|
/home/db2inst/Scripts > db2 -v "select * from test0809"
select * from test0809
A B C
----------- ---------- -----------
1 121@1 1
2 22 0
1 11 0
3 3*3 1
3 33 0
4 3*3 0
5 55.5 0
7 record(s) selected.
在一般情况下,可能DB2会从右往左apply the predicates。
/home/db2inst/Scripts > db2 -v "select * from test0809 where a in(2,5) and dec(b,10,5)>0"
select * from test0809 where a in(2,5) and dec(b,10,5)>0
A B C
----------- ---------- -----------
SQL0420N Invalid character found in a character string argument of the
function "DECIMAL". SQLSTATE=22018
/home/db2inst/Scripts > db2 -v "select * from test0809 where dec(b,10,5)>0 and a in (2,5)"
select * from test0809 where dec(b,10,5)>0 and a in (2,5)
A B C
----------- ---------- -----------
2 22 0
5 55.5 0
2 record(s) selected.
但我创建了index on column a之后,DB2 optimizer发现使用index可能会快一些,而刚好在这种情况下after the filtering of column a, all the data in column can be casted to decimal values:
/home/db2inst/Scripts > db2 -v "create index idx_t1 on test0809(a)"
create index idx_t1 on test0809(a)
DB20000I The SQL command completed successfully.
/home/db2inst/Scripts > db2 -v "select * from test0809 where a in(2,5) and dec(b,10,5)>0"
select * from test0809 where a in(2,5) and dec(b,10,5)>0
A B C
----------- ---------- -----------
2 22 0
5 55.5 0
2 record(s) selected.
However, after I did a runstats on the table, DB2 optimizer realize that to fulfill this query for this particular table, accessing by index may be even slower because the tablesize is so small that accessed by index may need two or three physical reads, while accessed by table itself (full table scan) may need only one physical read. And this causes the filtering to be applied from right to the left again, and resulted in error again.
/home/db2inst/Scripts > db2 -v "runstats on table db2.test0809 and indexes all"
runstats on table db2.test0809 and indexes all
DB20000I The RUNSTATS command completed successfully.
/home/db2inst/Scripts > db2 -v "select * from test0809 where a in(2,5) and dec(b,10,5)>0"
select * from test0809 where a in(2,5) and dec(b,10,5)>0
A B C
----------- ---------- -----------
SQL0420N Invalid character found in a character string argument of the
function "DECIMAL". SQLSTATE=22018
所以,我的认识是,如果真的有必要做data type conversion的话,最好确保所有的数据都是真的可以CONVERTED。 |
|