|
|
|
|
|
|
|
|
|
|
|
Re: different outputs on same parameter (datatype date) while using 'like' and '=' [message #631063 is a reply to message #631058] |
Thu, 08 January 2015 09:42 |
Solomon Yakobson
Messages: 3273 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
Bill B wrote on Thu, 08 January 2015 09:54when you have a 4 digit string it makes no sense to use wildcards
I would use neither:
SELECT COUNT(*) FROM EMP
WHERE TO_CHAR(HIREDATE,'YYYY') = '1981';
nor:
SELECT COUNT(*) FROM EMP
WHERE extract(year from HIREDATE) = 1981
/
since neither would use index on hiredate. We should code in such way that Oracle could use index if such index will be needed in the future. So I'd use:
SELECT COUNT(*) FROM EMP
WHERE HIREDATE >= DATE '1981-01-01'
AND HIREDATE < DATE '1982-01-01'
/
Compare:
SQL> create index emp_idx1 on emp(hiredate);
Index created.
SQL> set linesize 132
SQL> explain plan for
2 SELECT COUNT(*) FROM EMP
3 WHERE TO_CHAR(HIREDATE,'YYYY') = '1981';
Explained.
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
---------------------------------------------------------------------------------------
Plan hash value: 2083865914
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 8 | 3 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 8 | | |
|* 2 | TABLE ACCESS FULL| EMP | 1 | 8 | 3 (0)| 00:00:01 |
---------------------------------------------------------------------------
Predicate Information (identified by operation id):
PLAN_TABLE_OUTPUT
---------------------------------------------------------------------------------------
---------------------------------------------------
2 - filter(TO_CHAR(INTERNAL_FUNCTION("HIREDATE"),'YYYY')='1981')
14 rows selected.
SQL> explain plan for
2 SELECT COUNT(*) FROM EMP
3 WHERE extract(year from HIREDATE) = 1981
4 /
Explained.
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
---------------------------------------------------------------------------------------
Plan hash value: 2083865914
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 8 | 3 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 8 | | |
|* 2 | TABLE ACCESS FULL| EMP | 1 | 8 | 3 (0)| 00:00:01 |
---------------------------------------------------------------------------
Predicate Information (identified by operation id):
PLAN_TABLE_OUTPUT
---------------------------------------------------------------------------------------
---------------------------------------------------
2 - filter(EXTRACT(YEAR FROM INTERNAL_FUNCTION("HIREDATE"))=1981)
14 rows selected.
SQL> explain plan for
2 SELECT COUNT(*) FROM EMP
3 WHERE HIREDATE >= DATE '1981-01-01'
4 AND HIREDATE < DATE '1982-01-01'
5 /
Explained.
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
---------------------------------------------------------------------------------------
Plan hash value: 1016503744
------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 8 | 1 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 8 | | |
|* 2 | INDEX RANGE SCAN| EMP_IDX1 | 2 | 16 | 1 (0)| 00:00:01 |
------------------------------------------------------------------------------
Predicate Information (identified by operation id):
PLAN_TABLE_OUTPUT
---------------------------------------------------------------------------------------
---------------------------------------------------
2 - access("HIREDATE">=TO_DATE(' 1981-01-01 00:00:00', 'syyyy-mm-dd
hh24:mi:ss') AND "HIREDATE"<TO_DATE(' 1982-01-01 00:00:00',
'syyyy-mm-dd hh24:mi:ss'))
16 rows selected.
SQL>
SY.
|
|
|