通过实践发现,DB2和MYSQL数据库对应 where is null 都会使用索引,当然where is not null不会使用索引,相当于!=判断了。
DB2:
建表并插入数据并建立索引
db2 "create table employee (id int,name varchar(10))"
db2 "begin atomic declare i int default 0;while(i <200000) do insert into mysql.employee values (i,'aaaajjj');set i=i+1;end while;end"
db2 "creat index employee_idx1 on employee(id)"
1.如果第一次执行,请先 connect to test, 执行db2 -tvf $HOME/sqllib/misc/EXPLAIN.DDL建立执行计划表
2.db2expln -database test -statement "select * from employee where id =9995" -terminal -graph -opids
Optimizer Plan:
Rows
Operator
(ID)
Cost
1
RETURN
( 1)
20.3034
|
1
FETCH
( 2)
20.3034
/ \
1 200000
IXSCAN Table:
( 3) DB2INST1
13.537 EMPLOYEE
|
200000
Index:
DB2INST1
EMPLOYEE_IDX1
3.db2expln -database test -statement "select * from employee where id is null " -terminal -graph -opids
Optimizer Plan:
Rows
Operator
(ID)
Cost
1
RETURN
( 1)
20.3034
|
1
FETCH
( 2)
20.3034
/ \
1 200000
IXSCAN Table:
( 3) DB2INST1
13.537 EMPLOYEE
|
200000
4.db2expln -database test -statement "select * from employee where id is not null " -terminal -graph -opids
Optimizer Plan:
Rows
Operator
(ID)
Cost
200000
RETURN
( 1)
1225.29
|
200000
TBSCAN
( 2)
1225.29
|
200000
Table:
DB2INST1
EMPLOYEE
MYSQL:
建表:
插入数据:
vi /home/mysql/liys/select.sql
delimiter // #定义标识符为双斜杠
drop procedure if exists test; #如果存在test存储过程则删除
create procedure test() #创建无参存储过程,名称为test
begin
declare i int; #申明变量
set i = 0; #变量赋值
while i < 200000 do #结束循环的条件: 当i大于10时跳出while循环
insert into employee values (i,'lysss'); #往test表添加数据
set i = i + 1; #循环一次,i加一
end while; #结束while循环
select count(*) from test; #查看test表数据
end
// #结束定义语句
source /home/mysql/liys/select.sql;
delimiter ;
call test();
创建索引:
mysql> create index employee_idx1 on employee(id);
Query OK, 0 rows affected (0.48 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> explain select * from employee where id =9995;
+----+-------------+----------+------------+------+---------------+---------------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+----------+------------+------+---------------+---------------+---------+-------+------+----------+-------+
| 1 | SIMPLE | employee | NULL | ref | employee_idx1 | employee_idx1 | 5 | const | 1 | 100.00 | NULL |
+----+-------------+----------+------------+------+---------------+---------------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)
mysql> explain select * from employee where id is null;
+----+-------------+----------+------------+------+---------------+---------------+---------+-------+------+----------+-----------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+----------+------------+------+---------------+---------------+---------+-------+------+----------+-----------------------+
| 1 | SIMPLE | employee | NULL | ref | employee_idx1 | employee_idx1 | 5 | const | 1 | 100.00 | Using index condition |
+----+-------------+----------+------------+------+---------------+---------------+---------+-------+------+----------+-----------------------+
1 row in set, 1 warning (0.00 sec)
mysql> explain select * from employee where id is not null;
+----+-------------+----------+------------+------+---------------+------+---------+------+--------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+----------+------------+------+---------------+------+---------+------+--------+----------+-------------+
| 1 | SIMPLE | employee | NULL | ALL | employee_idx1 | NULL | NULL | NULL | 199795 | 50.00 | Using where |
+----+-------------+----------+------------+------+---------------+------+---------+------+--------+----------+-------------+
1 row in set, 1 warning (0.00 sec)