在开始测试前先创建测试数据:
create table
student
(
id
int
primary key not null
,
sid
int
)
create procedure
insertDate
()
BEGIN
DECLARE
v_id
int
;
set
v_id
=
0
;
while
v_id
<
100000
DO
insert into
student
values
(
v_id
,
v_id
);
set
v_id
=
v_id
+
1
;
end while
;
END
call
insertDate
()
在这里,我首先创建了一个表,其中
id
为主键。注意
DB2
中默认将主键列创建为索引,所以在这个表中
id
列有索引,
sid
列没有索引。之后插入了
10
万行的数据。下面测试
or
和
and
一边有索引时一边没索引时的情况
select
*
from
student
where
sid
=
5
or
id
=
10
select
*
from
student
where
sid
=
5
and
id
=
10
可以看到当
or
的一边有索引一边没有索引时,没有利用索引,而
and
的一边有索引一边没有索引时
,
却利用了索引。
下面测试
or
和
and
的两边都有索引的情况
select
*
from
student
where
sid
=
5
or
id
=
10
select
*
from
student
where
sid
=
5
and
id
=
10
下面测试
or
和
in
的在有索引下的情况
select
*
from
student
where
sid
=
5
or
sid
=
10
select
*
from
student
where
sid
in
(
5
,
10
)
可以看到在有索引的情况下,
in
和
or
的速度差不多。
下面测试没有索引的情况下,
in
和
or
的速度:
首先删除索引
drop index stu_sid
运行下面语句
:
select
*
from
student
where
sid
=
5
and
id
=
10
select
*
from
student
where
sid
in
(
5
,
10
)
两个语句的执行计划都如下
:
所以,
in
和
or
在没有索引的情况下速度也是一样的。
总结
:
经过在
DB2
上的测试,如果
or
的左右两边的列都有索引,则可以利用索引,否则(只有一边的列有索引或者两边都没有索引)不能利用索引。而
and
两种情况都可以利用索引。
有人说因为
or
不能利用索引所以要将
or
换成
in,
其实是错的。如果
or
的两边是不同列,则不能换成
in,
如果是同样的列,可以转换成
in
的写法。但是这列如果有索引
in
或者
or
都会利用索引,而且速度一样。这列没有索引的话,
in
或
or
都不能利用索引,速度也是一样的。
- 大小: 10.1 KB
- 大小: 15.5 KB
- 大小: 36.5 KB
- 大小: 21.4 KB
- 大小: 23.9 KB
- 大小: 29 KB
- 大小: 9.5 KB
分享到:
相关推荐
Advances and Open Problems in Federated Learning。Federated learning (FL) is a machine learning setting where many clients (e.g. mobile devices or whole organizations) collaboratively train a model ...
Erase or Fill? Deep Joint Recurrent Rain Removal and Reconstruction in Videos论文以及相应代码,这是一篇解决视频derain的论文。
The fourth edition of Data Structures and Algorithm Analysis in C++ describes data structures, methods of organizing large amounts of data, and algorithm analysis, the estimation of the running time ...
Modeling and Canceling Tremor in Human-Machine Interfaces
You don′t need expensive or complicated software or a super–powerful computer to build a Web site that does all sorts of amazing things. All you need is a text editor and the clear, step–by–step ...
In this book, the authors explore multiple perspectives on computer graphics: the user's, the application programmer's, the package implementor's, and the hardware designer's. For example, the issues...
The hands-on, no-nonsense style of this book will help you get started by offering practical advice from someone who's been in your shoes and wants to help you learn quickly <br>The techniques and...
Various batch computation methods are described in detail, while production planning is considered on several levels, including supply planningf or customers, master planning, and production ...
This book presents recent developments and research trends in the field of feature selection for data and pattern recognition, highlighting a number of latest advances. The field of feature selection ...
Compilation - Compiling and Running C Programs, Installing Codeblocks IDE on Raspberry Pi / Linux, Variables and Data Types in C, Operators, Input and output - printf and scanf, Strings, Arrays, ...
SQL is the de facto standard for database retrieval, and if you need to access, update, or utilize data in a modern database management system, you will need SQL to do it. The Second Edition of Joe ...
To even try to keep pace with the rapid evolution of game development, you need a strong foundation in core programming techniques-not a hefty volume on one narrow topic or one that devotes itself to ...
This article provides a historical account of the major developments in the area of curves and surfaces as they entered the area of CAGD { ...and representation of free-form curves, surfaces, or volumes.
Generate a function or script file call tree and plot it in a figure
优化方面的书籍,用cplex12.5讲解
Multi-core processors and the increasing demand for maximum performance and scalability in mission-critical applications have renewed interest in functional languages like Erlang that are designed to...
Discrete Choice Analysis presents these results in such a way that they are fully accessible to the range of students and professionals who are involved in modelling demand and consumer behavior in ...