Implicit Data Type Conversion
Copyright © 2024 PawSQL
Problem
Let's say the O_ORDERDATE
column in TPCH.ORDERS
table is defined as VARCHAR(16)
, and there is an index ORDDATE_IDX
on ORDERS(O_ORDERDATE)
to accelerate the queries against this table if there is a condition on O_ORDERDATE
. If we want to count the number of orders of today using following SQL query.
select count(*) from ORDERS
where O_ORDERDATE = current_date();
Use explain
to get the query plan:
-> Filter: (orders.O_ORDERDATE = <cache>(curdate())) (cost=2621.05 rows=19938)
-> Table scan on ORDERS (cost=2621.05 rows=199378)
From the query plan, we can see the database optimizer uses a table scan instead of the index lookup as we expected. The query cost is high.