HSQL 2.2.4,错误的查询计划
Posted
技术标签:
【中文标题】HSQL 2.2.4,错误的查询计划【英文标题】:HSQL 2.2.4, bad query plan 【发布时间】:2011-07-18 18:04:36 【问题描述】:为什么 IR1 需要全面扫描?
table=INTR
alias=IR1
access=FULL SCAN
IR2 不需要全面扫描:
table=INTR
alias=IR2
access=INDEX PRED
这是完全相同的连接:
EXPLAIN PLAN FOR SELECT *
FROM DTMS.INTR_SUB s
JOIN DTMS.INTR ir1 ON s.CLASS_1 = ir1.CLASS
JOIN DTMS.NDC_INDEX n1 ON ir1.KDC1 = n1.KDC1
JOIN DTMS.INTR ir2 ON s.CLASS_2 = ir2.CLASS
JOIN DTMS.NDC_INDEX n2 ON ir2.KDC1 = n2.KDC1
WHERE n1.NDC = 378204701 AND n2.NDC = 378204701
这是我的索引:
s INDEX 1 CLASS_1
s INDEX 2 CLASS_2
ir PRIMARY KEY(KDC1,CLASS)
ir INDEX (CLASS)
如果我禁用 s.CLASS_1 上的索引(通过添加 0), HSQL 决定它可以为 IR1 使用相同的索引 和 IR2。
alias=S
access=FULL SCAN
table=INTR
alias=IR2
access=INDEX PRED
table=INTR
alias=IR1
access=INDEX PRED
EXPLAIN PLAN FOR SELECT *
FROM DTMS.INTR_SUB s
JOIN DTMS.INTR ir1 ON s.CLASS_1+0 = ir1.CLASS
JOIN DTMS.NDC_INDEX n1 ON ir1.KDC1 = n1.KDC1
JOIN DTMS.INTR ir2 ON s.CLASS_2 = ir2.CLASS
JOIN DTMS.NDC_INDEX n2 ON ir2.KDC1 = n2.KDC1
WHERE n1.NDC = 378204701 AND n2.NDC = 378204701
这是架构。在数据库管理器中,我必须复制/粘贴 三者分别建表做表:
CREATE SCHEMA DTMS AUTHORIZATION SA;
SET SCHEMA DTMS;
CREATE CACHED TABLE DTMS.INTR(KDC1 NUMERIC(5) NOT NULL,CLASS NUMERIC(5) NOT NULL,PRIMARY KEY(KDC1,CLASS));
CREATE CACHED TABLE DTMS.NDC_INDEX(NDC NUMERIC(11) PRIMARY KEY,KDC1 NUMERIC(5) NOT NULL,KDC2 NUMERIC(2) NOT NULL,KDC3 NUMERIC(3) NOT NULL,ACTIVITY_CODE NUMERIC(1) NOT NULL,ROUTE_ABRV CHARACTER(2) NOT NULL);
CREATE CACHED TABLE DTMS.INTR_SUB(CLASS_1 NUMERIC(5) NOT NULL,DURATION_1 NUMERIC(3) NOT NULL,SCHEDULE_1 NUMERIC(3) NOT NULL,ACTIVITY_CODE_1 NUMERIC(1) NOT NULL,CLASS_2 NUMERIC(5) NOT NULL,DURATION_2 NUMERIC(3) NOT NULL,SCHEDULE_2 NUMERIC(3) NOT NULL,ACTIVITY_CODE_2 NUMERIC(1) NOT NULL,ONSET_CODE NUMERIC(1) NOT NULL,SEVERITY_CODE NUMERIC(1) NOT NULL,DOC_CODE NUMERIC(1) NOT NULL,MGMT_CODE NUMERIC(1) NOT NULL,FILE_POS NUMERIC(15) NOT NULL);
CREATE INDEX INTR_SUB_CLASS_1 ON DTMS.INTR_SUB(CLASS_1);
CREATE INDEX INTR_SUB_CLASS_2 ON DTMS.INTR_SUB(CLASS_2);
CREATE INDEX INTR_CLASS ON DTMS.INTR(CLASS);
【问题讨论】:
【参考方案1】:此问题已在 2.2.5 中修复,报告如下。
][range variable 2
join type=INNER
table=INTR
alias=IR1
access=INDEX PRED
][range variable 4
join type=INNER
table=INTR
alias=IR2
access=INDEX PRED
关于 DatabaseManager 的使用,您可以将整个架构连同其表和索引一起定义为单个 SQL 语句。分号仅在末尾使用。该语句完全由 DatabaseManager 执行,因为它是 SQL 中的单个 CREATE SCHEMA 语句:
CREATE SCHEMA DTMS AUTHORIZATION SA
CREATE CACHED TABLE DTMS.INTR(KDC1 NUMERIC(5) NOT NULL,CLASS NUMERIC(5) NOT NULL,PRIMARY KEY(KDC1,CLASS))
CREATE CACHED TABLE DTMS.NDC_INDEX(NDC NUMERIC(11) PRIMARY KEY,KDC1 NUMERIC(5) NOT NULL,KDC2 NUMERIC(2) NOT NULL,KDC3 NUMERIC(3) NOT NULL,ACTIVITY_CODE NUMERIC(1) NOT NULL,ROUTE_ABRV CHARACTER(2) NOT NULL)
CREATE CACHED TABLE DTMS.INTR_SUB(CLASS_1 NUMERIC(5) NOT NULL,DURATION_1 NUMERIC(3) NOT NULL,SCHEDULE_1 NUMERIC(3) NOT NULL,ACTIVITY_CODE_1 NUMERIC(1) NOT NULL,CLASS_2 NUMERIC(5) NOT NULL,DURATION_2 NUMERIC(3) NOT NULL,SCHEDULE_2 NUMERIC(3) NOT NULL,ACTIVITY_CODE_2 NUMERIC(1) NOT NULL,ONSET_CODE NUMERIC(1) NOT NULL,SEVERITY_CODE NUMERIC(1) NOT NULL,DOC_CODE NUMERIC(1) NOT NULL,MGMT_CODE NUMERIC(1) NOT NULL,FILE_POS NUMERIC(15) NOT NULL)
CREATE INDEX INTR_SUB_CLASS_1 ON DTMS.INTR_SUB(CLASS_1)
CREATE INDEX INTR_SUB_CLASS_2 ON DTMS.INTR_SUB(CLASS_2)
CREATE INDEX INTR_CLASS ON DTMS.INTR(CLASS);
【讨论】:
以上是关于HSQL 2.2.4,错误的查询计划的主要内容,如果未能解决你的问题,请参考以下文章