我在我的应用程序中使用 Oracle 11G。
在我的数据库中有一个表 MST_PRODUCT,其中包含 80 00 000 条记录。
根据要求,我正在使用 JPA 本机查询从表中检索数据。
前任 :
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" ;
这是查询的执行计划......
谁能建议我,我该如何改进这个查询?或者在准备查询时我需要注意哪些因素?
Reagrds,Gunjan。