HSQL 2.2.4,错误的查询计划

发布于 2024-11-24 19:56:22 字数 2160 浏览 0 评论 0原文

为什么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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

摇划花蜜的午后 2024-12-01 19:56:22

此问题已在 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);

This issue has been fixed in 2.2.5 which reports the following.

  ][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        

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:

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文