在以下查询中哪个查询提供更好的性能?怎么样?请建议我。
SELECT *FROM emp e
WHERE e.hiredate<='02-JAN-1981';
SELECT *FROM emp e
WHERE e.hiredate<'03-JAN-1981';
答案 0 :(得分:2)
WHERE e.hiredate&lt; ='02-JAN-1981'
'02 -JAN-1981'是字符串,而不是 DATE 。您正在强制Oracle执行隐式数据类型转换,具体取决于您的特定于语言环境的NLS设置。始终使用 TO_DATE 或 ANSI日期文字。
WHERE e.hiredate <= TO_DATE('02-JAN-1981', 'DD-MON-YYYY')
,或者
WHERE e.hiredate <= DATE '1981-01-02'
关于运营商之间业绩差异的问题:
很明显,如果要提取的行与hiredate的值的谓词匹配,那么Oracle必须做更多的工作。
“&LT;”只会获取小于提供日期的行,但“&lt; =”将包含与提供日期匹配的所有行。
在您的情况下,两个查询都将具有相同的解释计划。
打开 SQL * Plus ,然后执行以下操作:
SQL> SET AUTOTRACE ON EXPLAIN
查询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'))
查询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'))
答案 1 :(得分:1)