In the following queries which query give better performance ? and how ?Kindly suggest me .
SELECT *FROM emp e
WHERE e.hiredate<='02-JAN-1981';
SELECT *FROM emp e
WHERE e.hiredate<'03-JAN-1981';
In the following queries which query give better performance ? and how ?Kindly suggest me .
SELECT *FROM emp e
WHERE e.hiredate<='02-JAN-1981';
SELECT *FROM emp e
WHERE e.hiredate<'03-JAN-1981';
WHERE e.hiredate<='02-JAN-1981'
'02-JAN-1981' is a string, and not a DATE. You are forcing Oracle to do an implicit datatype conversion depending on your locale-specific NLS settings. Always use TO_DATE or ANSI date literal.
WHERE e.hiredate <= TO_DATE('02-JAN-1981', 'DD-MON-YYYY')
or,
WHERE e.hiredate <= DATE '1981-01-02'
Coming to your question regarding the difference in performance between the operators:
It is very obvious that if there are rows to be fetched matching the predicate for the value of hiredate, then Oracle has to do more work.
"<" will only fetch rows which are less than the provided date, however, "<=" will include all the rows which match the provided date.
In your case, both the queries will have same explain plan.
Open SQL*Plus, and do the following:
SQL> SET AUTOTRACE ON EXPLAIN
Query 1:
SQL> SELECT * FROM emp e WHERE e.hiredate<= DATE '1981-01-02';
EMPNO ENAME JOB MGR HIREDATE SAL COMM
---------- ---------- --------- ---------- --------- ---------- ----------
DEPTNO
----------
7369 SMITH CLERK 7902 17-DEC-80 800
20
Execution Plan
----------------------------------------------------------
Plan hash value: 3956160932
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 37 | 4 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| EMP | 1 | 37 | 4 (0)| 00:00:01 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("E"."HIREDATE"<=TO_DATE(' 1981-01-02 00:00:00',
'syyyy-mm-dd hh24:mi:ss'))
Query 2:
SQL> SELECT * FROM emp e WHERE e.hiredate< DATE '1981-01-03';
EMPNO ENAME JOB MGR HIREDATE SAL COMM
---------- ---------- --------- ---------- --------- ---------- ----------
DEPTNO
----------
7369 SMITH CLERK 7902 17-DEC-80 800
20
Execution Plan
----------------------------------------------------------
Plan hash value: 3956160932
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 37 | 4 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| EMP | 1 | 37 | 4 (0)| 00:00:01 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("E"."HIREDATE"<TO_DATE(' 1981-01-03 00:00:00',
'syyyy-mm-dd hh24:mi:ss'))