解决VARCHAR2和NVARCHAR2隐式数据类型转换导致的性能问题

           前段时间,接到公司一项目组的数据库优化需求:一个计算费用的存储过程执行起来非常之慢,需要优化。

           拿到存储过程之后,快速看了下代码,然后通过PL/SQL Developer去查看SQL代码的执行计划。其中,看到下面的一条简单的SQL语句的执行计划有点儿问题,导致主表CA_SE_MANIFEST走的全表扫描,而该表的数据量很大,而TMP_COM_ID2又是一张临时表。可是主表CA_SE_MANIFEST的BL_NO_ID字段是主键字段,有唯一索引,却没有走索引,这样的话,执行效率应该不会好到哪儿去!

           SQL语句:

DELETE FROM TMP_COM_ID2 WHERE EXISTS(SELECT 1 FROM CA_SE_MANIFEST WHERE BL_NO_ID = C_ID AND DOCUMENT_TYPE IN ('1','2'))

           执行计划:

           经过分析,发现表TMP_COM_ID2的结构信息如下,只有1个字段,其中C_ID字段的数据类型是NVARCHAR2

SQL> desc tmp_com_id2
Name Type          Nullable Default Comments 
---- ------------- -------- ------- -------- 
C_ID NVARCHAR2(20)                           
 
SQL> 

           而主表CA_SE_MANIFEST的BL_NO_ID字段数据类型却是VARCHAR2

SQL> desc ca_se_manifest
Name                      Type         Nullable Default Comments                                                                                                                                                                                                           
------------------ ------------------- -------- ------- -------------------------------- 
BL_NO_ID           VARCHAR2(20 BYTE)                    出口提单序号                                                                                                                                                                                                       
BL_NO              VARCHAR2(40 BYTE)                    同一船名、航次下、分公司的提单号不重复                                                                                                                                             
CARRIER_BL_NO      VARCHAR2(20 BYTE)   Y                船公司提单号 
...
...

           基本上定位到了原因,是由于VARCHAR2和NVARCHAR2数据类型不一致导致的隐式数据类型转换,进而导致主表CA_SE_MANIFEST的主键字段BL_NO_ID上的索引失效,最终产生了全表扫描

           经过分析,可以将临时表TMP_COM_ID2的C_ID字段数据类型改成VARCHAR2,而主表CA_SE_MANIFEST的定义信息显然不可轻易修改。修改完临时表的字段数据类型之后,该条SQL的执行计划已经不再是CA_SE_MANIFEST的全表扫描了:

          最终,这个小问题得以解决。

          补充:① VARCHAR2 可以认为是VARCHAR的变种,它是一个变长的字符串数据类型;

                    ②NVARCHAR2 是包含UNICODE编码格式数据的变长字符串。

Oracle OCP考试1z0-007系列4:学会使用分组函数

继上篇文章,Oracle OCP考试1z0-007系列3:学会使用单行函数后,本篇是系列4,学会使用分组函数。 本篇是1z0-007课程的第四章,主要内容:

     1  了解在SQL语句中可以使用的分组函数;

     2  学会在SELECT语句中使用GROUP BY从句;

     3  学会使用HAVING从句来过滤数据。

本篇内容相对比较难,具体文档和讲义如下:

如何获取Oracle数据库中某个用户下的所有表、索引、约束、触发器的创建脚本?

在Oracle数据库中,如何快速获取某个用户下的所有表、索引、约束及触发器的创建SQL语句呢?本文给出一个简单的示例脚本,经过测试,除位于回收站下的对象及只读视图的约束语句不能获取,其它对象基本均可获取CREATE语句。

脚本内容如下:

[oracle@oracledg ~]$ cat get_ddl.sql 
-- Be sure the variable 'S' means the Schema name,it should be UPPERCASE.
-- And this script COULDNOT get the constraints CREATE DDL,which CONSTRAINT_TYPE is 'O',cause it's READ ONLY on a view.
-- Author Info:Asher Huang
-- http://OracleOnlinux.cn
-- Mail:3dian14@gmail.com

def S='&Schema'
set serveroutput on
set long 20000
set lines 2000 pages 0 ver off echo off head off feed off
set newpage none
set trimspool on
set long 5000000
spool &S..sql 
DECLARE
  CURSOR C1 IS
    SELECT '&S' AS u_name, table_name AS tab_name FROM user_tables;
  CURSOR C2 IS
    SELECT '&S' AS u_name, index_name AS idx_name FROM user_indexes;
  CURSOR C3 IS
    SELECT '&S' AS u_name, constraint_name AS cons_name FROM user_constraints where constraint_type not in('R','O');
  CURSOR C4 IS
    SELECT '&S' AS u_name, table_name AS tab_name FROM user_constraints where constraint_type in('R');
  CURSOR C5 IS
    SELECT '&S' AS u_name, trigger_name AS trig_name FROM user_triggers;
  V_MSG CLOB;
BEGIN
      DBMS_OUTPUT.put_line('The CREATE DDL for schema &S''s Tables are:');
  FOR x IN C1 LOOP
      select dbms_metadata.get_ddl('TABLE', x.tab_name, x.u_name) INTO V_MSG from dual;
      DBMS_OUTPUT.put_line(V_MSG);
  END LOOP;

      DBMS_OUTPUT.put_line('The CREATE DDL for schema &S''s Indexes are:');
  FOR x IN C2 LOOP
      select dbms_metadata.get_ddl('INDEX', x.idx_name, x.u_name) INTO V_MSG from dual;
      DBMS_OUTPUT.put_line(V_MSG);
  END LOOP;

      DBMS_OUTPUT.put_line('The CREATE DDL for schema &S''s Constraints are:');
  FOR x IN C3 LOOP
      select dbms_metadata.get_ddl('CONSTRAINT', x.cons_name, x.u_name) INTO V_MSG from dual;
      DBMS_OUTPUT.put_line(V_MSG);
  END LOOP;

      DBMS_OUTPUT.put_line('The CREATE DDL for schema &S''s Reference Constraints are:');
  FOR x IN C4 LOOP
      select dbms_metadata.get_dependent_ddl('REF_CONSTRAINT', x.tab_name, x.u_name) INTO V_MSG from dual;
      DBMS_OUTPUT.put_line(V_MSG);
  END LOOP;

      DBMS_OUTPUT.put_line('The CREATE DDL for schema &S''s Triggers are:');
  FOR x IN C5 LOOP
      select dbms_metadata.get_ddl('TRIGGER', x.trig_name, x.u_name) INTO V_MSG from dual;
      DBMS_OUTPUT.put_line(V_MSG);
  END LOOP;
END;
/
spool off;
[oracle@oracledg ~]$ 

测试获取SCOTT用户的所有表、索引、约束、触发器创建脚本如下:

[oracle@oracledg ~]$ cat SCOTT.sql 
The CREATE DDL for schema SCOTT's Tables are:

  CREATE TABLE "SCOTT"."DEPT" 
   (    "DEPTNO" NUMBER(2,0), 
        "DNAME" VARCHAR2(14), 
        "LOC" VARCHAR2(13), 
         CONSTRAINT "PK_DEPT" PRIMARY KEY ("DEPTNO")
  USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS 
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
  TABLESPACE "USERS"  ENABLE
   ) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
  TABLESPACE "USERS"


  CREATE TABLE "SCOTT"."EMP" 
   (    "EMPNO" NUMBER(4,0), 
        "ENAME" VARCHAR2(10), 
        "JOB" VARCHAR2(9), 
        "MGR" NUMBER(4,0), 
        "HIREDATE" DATE, 
        "SAL" NUMBER(7,2), 
        "COMM" NUMBER(7,2), 
        "DEPTNO" NUMBER(2,0), 
         CONSTRAINT "PK_EMP" PRIMARY KEY ("EMPNO")
  USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS 
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
  TABLESPACE "USERS"  ENABLE, 
         CONSTRAINT "FK_DEPTNO" FOREIGN KEY ("DEPTNO")
          REFERENCES "SCOTT"."DEPT" ("DEPTNO") ENABLE
   ) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
  TABLESPACE "USERS"


  CREATE TABLE "SCOTT"."BONUS" 
   (    "ENAME" VARCHAR2(10), 
        "JOB" VARCHAR2(9), 
        "SAL" NUMBER, 
        "COMM" NUMBER
   ) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
  TABLESPACE "USERS"


  CREATE TABLE "SCOTT"."SALGRADE" 
   (    "GRADE" NUMBER, 
        "LOSAL" NUMBER, 
        "HISAL" NUMBER
   ) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
  TABLESPACE "USERS"

The CREATE DDL for schema SCOTT's Indexes are:

  CREATE UNIQUE INDEX "SCOTT"."PK_EMP" ON "SCOTT"."EMP" ("EMPNO") 
  PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS 
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
  TABLESPACE "USERS"


  CREATE UNIQUE INDEX "SCOTT"."PK_DEPT" ON "SCOTT"."DEPT" ("DEPTNO") 
  PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS 
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
  TABLESPACE "USERS"

The CREATE DDL for schema SCOTT's Constraints are:

  ALTER TABLE "SCOTT"."EMP" ADD CONSTRAINT "PK_EMP" PRIMARY KEY ("EMPNO")
  USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS 
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
  TABLESPACE "USERS"  ENABLE


  ALTER TABLE "SCOTT"."DEPT" ADD CONSTRAINT "PK_DEPT" PRIMARY KEY ("DEPTNO")
  USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS 
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
  TABLESPACE "USERS"  ENABLE

The CREATE DDL for schema SCOTT's Reference Constraints are:

  ALTER TABLE "SCOTT"."EMP" ADD CONSTRAINT "FK_DEPTNO" FOREIGN KEY ("DEPTNO")
          REFERENCES "SCOTT"."DEPT" ("DEPTNO") ENABLE

The CREATE DDL for schema SCOTT's Triggers are:
[oracle@oracledg ~]$ 

Oracle OCP考试1z0-007系列2:学会使用WHERE和ORDER BY从句

继上篇日志,Oracle OCP考试1z0-007系列1:学会使用基本的SQL语句后,本篇是系列2,学会使用WHERE和ORDER BY从句。 本篇是1z0-007课程的第二章,主要内容: 1 学会使用WHERE从句从结果集中过滤数据; 2 学会使用ORDER BY从句对结果集进行排序。 本篇内容比较简单,只涉及到WHERE从句和ORDER BY从句两个知识点。 附:具体文档和讲义。

Oracle OCP考试1z0-007系列1:学会使用基本的SQL语句

在上一篇博文里提到Oracle OCP考试1z0-007考试的题库。从本篇开始,将带来Oracle OCP 1z0-007考试的相关文档和资料。

本篇是1z0-007课程的第一章,主要内容:
1 学会使用基本的SQL语句;
2 了解SQL语句的功能;
3 学会如何执行基本的SQL语句
4 了解SQL语句与iSQL*PLUS命令的差别

附:下述是具体文档和讲义。