SQL优化 1

SQL_ID:fvdwtfv18yy0m

先看看sql的预估执行计划
select * from table(dbms_xplan.display_awr('fvdwtfv18yy0m'));

sql问题如下
SELECT cu.ecno,
	cu.NAME,
	cu.birthday,
	cu.sex,
	cu_id.idtype,
	cu_id.idcard,
	reg.username
FROM customer cu,
	customerid cu_id,
	customerreg reg
WHERE cu.ecno = cu_id.ecno
	AND cu.ecno = reg.ecno
	AND upper(reg.username) = upper('13979083223@139.com');
	
看看表上面有没有索引
SELECT INDEX_NAME FROM  DBA_INDEXES WHERE TABLE_NAME ='CUSTOMERREG';

切换一下session的用户,当不知道某用户名密码的时候,可以设置session的current_schema
alter session set current_schema=ESERVICE;

查看数据表的创建语句

CREATE TABLE "ESERVICE"."CUSTOMERREG"
   (    "ECNO" VARCHAR2(32) NOT NULL ENABLE,
        "USERNAME" VARCHAR2(50),
        "USERPASSWORD" VARCHAR2(50),
        "CRANECARD" VARCHAR2(30),
        "CREATEDATE" DATE,
        "LOGONCOUNT" NUMBER(10,0),
        "LASTLOGONDATE" DATE,
        "ISUSE" VARCHAR2(1),
        "CUSTOMERREG_LDAP_FLAG" VARCHAR2(10),
        "RANDOMNO" VARCHAR2(20),
        "LASTSENDMAILDATE" VARCHAR2(20),
        "DAYLOGINCOUNT" VARCHAR2(1),
        "DAYLOGINDATE" VARCHAR2(14),
         PRIMARY KEY ("ECNO")
  USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS
  STORAGE(INITIAL 19922944 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1
  BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE "ESERVICE"  ENABLE,
         CONSTRAINT "USERNAME" UNIQUE ("USERNAME")
  USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS
  STORAGE(INITIAL 29360128 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1
  BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE "ESERVICE"  ENABLE
   ) SEGMENT CREATION IMMEDIATE
  PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255
 NOCOMPRESS LOGGING
  STORAGE(INITIAL 75497472 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1
  BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE "ESERVICE"

select name,DATATYPE_STRING,VALUE_STRING from v$sql_bind_capture where sql_id='fvdwtfv18yy0m';

-----------------------------------------------------------------------------------
| Id  | Operation           | Name        | Rows  | Bytes | Cost (%CPU)| Time     |
-----------------------------------------------------------------------------------
|   0 | SELECT STATEMENT    |             | 15626 |  1449K| 12213   (1)| 00:02:27 |
|*  1 |  HASH JOIN          |             | 15626 |  1449K| 12213   (1)| 00:02:27 |
|*  2 |   HASH JOIN         |             | 15594 |   974K|  9806   (1)| 00:01:58 |
|*  3 |    TABLE ACCESS FULL| CUSTOMERREG | 15594 |   517K|  5635   (1)| 00:01:08 |
|   4 |    TABLE ACCESS FULL| CUSTOMER    |  1489K|    42M|  4167   (1)| 00:00:51 |
|   5 |   TABLE ACCESS FULL | CUSTOMERID  |  1501K|    44M|  2402   (1)| 00:00:29 |
-----------------------------------------------------------------------------------

SELECT TABLE_NAME,COLUMN_NAME,INDEX_NAME FROM DBA_IND_COLUMNS WHERE TABLE_NAME IN ('CUSTOMER','CUSTOMERID','CUSTOMERREG');

TABLE_NAME                     COLUMN_NAME                    INDEX_NAME
------------------------------ ------------------------------ ------------------------------
CUSTOMER                       MOBILE                         IND_CUSTOMER
CUSTOMER                       ECNO                           SYS_C0015620
CUSTOMERID                     ECNO                           IDX_CUS_ID
CUSTOMERREG                    USERNAME                       USERNAME
CUSTOMERREG                    ECNO                           SYS_C0015621

IDX_CUS_ID表空间是USERS

create index eservice.ind_CUSTOMERREG_upperusername on eservice.CUSTOMERREG(upper('USERNAME')) tablespace EBDBINDEX;

---------------------------------------------------------------------------------------
| Id  | Operation              | Name         | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT       |              | 15626 |   976K| 11775   (1)| 00:02:22 |
|*  1 |  HASH JOIN             |              | 15626 |   976K| 11775   (1)| 00:02:22 |
|*  2 |   HASH JOIN            |              | 15594 |   746K|  9370   (1)| 00:01:53 |
|*  3 |    TABLE ACCESS FULL   | CUSTOMERREG  | 15594 |   517K|  5635   (1)| 00:01:08 |
|   4 |    INDEX FAST FULL SCAN| SYS_C0015620 |  1489K|    21M|  3732   (1)| 00:00:45 |
|   5 |   TABLE ACCESS FULL    | CUSTOMERID   |  1501K|    21M|  2401   (1)| 00:00:29 |
---------------------------------------------------------------------------------------
SELECT 1
FROM customer cu,
	customerid cu_id,
	customerreg reg
WHERE cu.ecno = cu_id.ecno
	AND cu.ecno = reg.ecno
	AND upper(reg.username) = upper('13979083223@139.com');

感觉这个sql的执行计划是正确的。

posted @ 2016-08-20 01:16  WSmallTail  阅读(366)  评论(0编辑  收藏  举报