如何提高Oracle 11g中的查询性能

时间:2012-12-13 07:42:21

标签: oracle jpa

我在我的应用程序中使用Oracle 11G。

在我的数据库中有一个表MST_PRODUCT,其中包含80 00 000条记录。

根据要求,我使用JPA本机查询从表中检索数据。

EX:

     SELECT sku_id
FROM MST_PRODUCT prod
WHERE brand_code IN
  (
   SELECT brand_code
   FROM MST_PRODUCT prod
   WHERE prod.SKU_ID IN
   (SELECT prd_value FROM TRANS_SCH_UNIT_PRD_SCOPE TSUPS WHERE TSUPS.sch_id='600')
  ) and prod.sku_id NOT IN
  (SELECT prd_value
  FROM TRANS_SCH_UNIT_PRD_SCOPE TSUPS
  WHERE TSUPS.sch_id='600'
  );

上述查询花费了太多时间来检索数据。

表中大约有80 00 000个SKU ID和500个品牌代码。

此查询需要50到60秒才能完成执行。有时甚至更多。

这是我桌上的计划......

CREATE TABLE "PERK"."MST_PRODUCT" 
   (    "PRD_ID" NUMBER NOT NULL ENABLE, 
    "COMPANY" VARCHAR2(20 BYTE), 
    "LOB" VARCHAR2(20 BYTE), 
    "PRD_DEPT" VARCHAR2(20 BYTE), 
    "PRD_SUB_DEPT" VARCHAR2(20 BYTE), 
    "CLASS" VARCHAR2(20 BYTE), 
    "SUB_CLASS" VARCHAR2(20 BYTE), 
    "MC_CODE" VARCHAR2(20 BYTE), 
    "BRAND_CODE" VARCHAR2(20 BYTE), 
    "SKU_ID" VARCHAR2(20 BYTE), 
    "MC_DESC" VARCHAR2(20 BYTE), 
    "SKU_DESC" VARCHAR2(40 BYTE), 
    "BRAND_DESC" VARCHAR2(20 BYTE), 
    "MODIFIED_BY" VARCHAR2(30 CHAR), 
    "MODIFIED_DATE" TIMESTAMP (6), 
    "CREATED_BY" VARCHAR2(30 CHAR), 
    "CREATED_DATE" TIMESTAMP (6), 
    "IS_ACTIVE" NUMBER(1,0) DEFAULT 0, 
     CONSTRAINT "MST_PRODUCT_PK" PRIMARY KEY ("PRD_ID")
  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 INDEX "PERK"."BRAND_INDEX" ON "PERK"."MST_PRODUCT" ("BRAND_CODE") 
  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 INDEX "PERK"."CLASS_INDEX" ON "PERK"."MST_PRODUCT" ("CLASS") 
  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 INDEX "PERK"."COMP_INDEX" ON "PERK"."MST_PRODUCT" ("COMPANY") 
  PCTFREE 10 INITRANS 2 MAXTRANS 255 
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
  TABLESPACE "USERS" ;

  CREATE INDEX "PERK"."DEPT_INDEX" ON "PERK"."MST_PRODUCT" ("PRD_DEPT") 
  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 INDEX "PERK"."MST_PRD_ISACTIVE_IDX" ON "PERK"."MST_PRODUCT" ("IS_ACTIVE") 
  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 INDEX "PERK"."MST_PRD_MCCODE_IDX" ON "PERK"."MST_PRODUCT" ("MC_CODE") 
  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 INDEX "PERK"."MST_PRODUCT_INDEX1" ON "PERK"."MST_PRODUCT" ("LOB") 
  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 "PERK"."MST_PRODUCT_PK" ON "PERK"."MST_PRODUCT" ("PRD_ID") 
  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 INDEX "PERK"."SKU_INDEX" ON "PERK"."MST_PRODUCT" ("SKU_ID") 
  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 INDEX "PERK"."SUBCLASS_INDEX" ON "PERK"."MST_PRODUCT" ("SUB_CLASS") 
  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 INDEX "PERK"."SUB_INDEX" ON "PERK"."MST_PRODUCT" ("PRD_SUB_DEPT") 
  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" ;

以下是查询的执行计划......

enter image description here

有人可以建议我,我该如何改进此查询?或者在准备查询时我需要注意哪些因素?

Reagrds, Gunjan。

2 个答案:

答案 0 :(得分:0)

我会尝试使用连接而不是子选择。子选择通常比连接更差。

答案 1 :(得分:0)

你可以尝试这个:

Select Sku_Id
  From Mst_Product Prod
 Where Exists (Select Null
          From Mst_Product Iprod, Trans_Sch_Unit_Prd_Scope Tsups
         Where Prod.Brand_Code = Iprod.Brand_Code
           And Iprod.Sku_Id = Tsups.Prd_Value
           And Tsups.Sch_Id = '600')
   And Not Exists (Select Null
          From Trans_Sch_Unit_Prd_Scope Tsups
         Where Tsups.Sch_Id = '600'
           And Prod.Sku_Id = Tsups.Prd_Value);