Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
63093d87
提交
63093d87
authored
2月 24, 2010
作者:
Thomas Mueller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
The index was not used if there were multiple IN(..) conditions.
上级
be2326c6
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
58 行增加
和
9 行删除
+58
-9
IndexCondition.java
h2/src/main/org/h2/index/IndexCondition.java
+37
-7
optimizations.sql
h2/src/test/org/h2/samples/optimizations.sql
+21
-2
没有找到文件。
h2/src/main/org/h2/index/IndexCondition.java
浏览文件 @
63093d87
...
...
@@ -6,6 +6,7 @@
*/
package
org
.
h2
.
index
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.Comparator
;
import
java.util.HashSet
;
...
...
@@ -206,10 +207,10 @@ public class IndexCondition {
/**
* Get the comparison bit mask.
*
* @param
conditionCount the number of
index conditions
* @param
indexConditions all
index conditions
* @return the mask
*/
public
int
getMask
(
int
conditionCount
)
{
public
int
getMask
(
ArrayList
<
IndexCondition
>
indexConditions
)
{
switch
(
compareType
)
{
case
Comparison
.
FALSE
:
return
ALWAYS_FALSE
;
...
...
@@ -217,11 +218,30 @@ public class IndexCondition {
return
EQUALITY
;
case
Comparison
.
IN_LIST
:
case
Comparison
.
IN_QUERY
:
if
(
conditionCount
>
1
)
{
// if there are more conditions, don't use the index on IN(..)
// IN(..) can not be combined with other conditions,
// otherwise the query returns the wrong result
return
0
;
if
(
indexConditions
.
size
()
>
1
)
{
// IN(..) can not be combined with other conditions.
// If there are other conditions (except for other
// IN_ conditions), don't use the index on IN(..)
// otherwise the query returns the wrong result.
boolean
beforeThis
=
true
;
for
(
IndexCondition
c
:
indexConditions
)
{
if
(
c
==
this
)
{
beforeThis
=
false
;
continue
;
}
if
(
c
.
isEvaluatable
())
{
if
(
beforeThis
)
{
// If there are only multiple IN_ conditions,
// only the first one can be used
return
0
;
}
if
(!
c
.
isIn
())
{
// If there are other (non-IN_) conditions,
// this one can't be used
return
0
;
}
}
}
}
return
EQUALITY
;
case
Comparison
.
BIGGER_EQUAL
:
...
...
@@ -244,6 +264,16 @@ public class IndexCondition {
return
compareType
==
Comparison
.
FALSE
;
}
private
boolean
isIn
()
{
switch
(
compareType
)
{
case
Comparison
.
IN_LIST
:
case
Comparison
.
IN_QUERY
:
return
true
;
default
:
return
false
;
}
}
/**
* Check if this index condition is of the type column larger or equal to
* value.
...
...
h2/src/test/org/h2/samples/optimizations.sql
浏览文件 @
63093d87
...
...
@@ -22,7 +22,7 @@ SELECT COUNT(*) FROM TEST;
-- Display the query plan - 'direct lookup' means the index is used
EXPLAIN
SELECT
COUNT
(
*
)
FROM
TEST
;
--> SELECT COUNT(*)
--> FROM PUBLIC.TEST /* PUBLIC.TEST
_DATA
*/
--> FROM PUBLIC.TEST /* PUBLIC.TEST
.tableScan
*/
--> /* direct lookup */
;
...
...
@@ -90,7 +90,7 @@ SELECT MIN(VALUE), MAX(VALUE) FROM TEST;
-- Display the query plan - 'direct lookup' means it's optimized
EXPLAIN
SELECT
MIN
(
VALUE
),
MAX
(
VALUE
)
FROM
TEST
;
--> SELECT MIN(VALUE), MAX(VALUE)
--> FROM PUBLIC.TEST /* PUBLIC.TEST
_DATA
*/
--> FROM PUBLIC.TEST /* PUBLIC.TEST
.tableScan
*/
--> /* direct lookup */
;
...
...
@@ -223,3 +223,22 @@ EXPLAIN SELECT * FROM TEST WHERE ID IN(1, 1000);
DROP
TABLE
TEST
;
-------------------------------------------------------------------------------
-- Optimize Multiple IN(..)
-------------------------------------------------------------------------------
-- This code snippet shows how multiple IN(...) conditions use an index.
-- Initialize the data
CREATE
TABLE
TEST
(
ID
INT
PRIMARY
KEY
,
DATA
INT
);
CREATE
INDEX
TEST_DATA
ON
TEST
(
DATA
);
INSERT
INTO
TEST
SELECT
X
,
MOD
(
X
,
10
)
FROM
SYSTEM_RANGE
(
1
,
1000
);
-- Display the query plan
EXPLAIN
SELECT
*
FROM
TEST
WHERE
ID
IN
(
10
,
20
)
AND
DATA
IN
(
1
,
2
);
--> SELECT TEST.ID, TEST.DATA
--> FROM PUBLIC.TEST /* PUBLIC.PRIMARY_KEY_2: ID IN(10, 20) */
--> WHERE (ID IN(10, 20)) AND (DATA IN(1, 2))
;
DROP
TABLE
TEST
;
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论