HSQL 2.2.4,错误的查询计划
为什么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);
Why does IR1 need a full scan?
table=INTR
alias=IR1
access=FULL SCAN
IR2 did not need a full scan:
table=INTR
alias=IR2
access=INDEX PRED
It is the exact same join:
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
Here are my indexes:
s INDEX 1 CLASS_1
s INDEX 2 CLASS_2
ir PRIMARY KEY(KDC1,CLASS)
ir INDEX (CLASS)
If I disable the index on s.CLASS_1 (by adding 0),
HSQL decides that it can use the same index for IR1
and 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
This is the schema. In the Database Manager, I had to copy/paste
the three create tables separately to make the tables:
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此问题已在 2.2.5 中修复,报告如下。
关于DatabaseManager的使用,您可以将整个模式及其表和索引定义为单个SQL语句。仅在末尾使用分号。该语句完全由 DatabaseManager 执行,因为它是 SQL 中的单个 CREATE SCHEMA 语句:
This issue has been fixed in 2.2.5 which reports the following.
Regarding the use of DatabaseManager, you can define a whole schema, together with its tables and indexes as a single SQL statement. A semicolon is used only at the end. This statement is executed fully by DatabaseManager because it is a single CREATE SCHEMA statement in SQL: