设为首页 收藏本站
查看: 629|回复: 0

[经验分享] mysql数据5.6.0和5.1.7的null字段索引测试

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-4-15 09:07:01 | 显示全部楼层 |阅读模式
如果保函null字段是数字 is null 和is not null都不走索引

如果保函null字段是字符 is null不走索引 和is not null会走索引
字符类型可以默认'' 数字类型可以默认0



1、数据库为5.6.0版本测试


mysql> select @@version;
+------------+
| @@version  |
+------------+
| 5.6.10-log |
+------------+
1 row in set (0.00 sec)

mysql>

mysql>  create table test_innodb (id int(11) unsigned NOT NULL primary key AUTO_INCREMENT,name varchar(100)) engine=innodb;
Query OK, 0 rows affected (0.16 sec)

mysql> show create table test_innodb\G
*************************** 1. row ***************************
       Table: test_innodb
Create Table: CREATE TABLE `test_innodb` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

mysql> insert into test_innodb(name) values('hlf'),('huanglf');
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> show create table test_innodb\G                        
*************************** 1. row ***************************
       Table: test_innodb
Create Table: CREATE TABLE `test_innodb` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

mysql> optimize table test_innodb;
+------------------+----------+----------+-------------------------------------------------------------------+
| Table            | Op       | Msg_type | Msg_text                                                          |
+------------------+----------+----------+-------------------------------------------------------------------+
| test.test_innodb | optimize | note     | Table does not support optimize, doing recreate + analyze instead |
| test.test_innodb | optimize | status   | OK                                                                |
+------------------+----------+----------+-------------------------------------------------------------------+
2 rows in set (0.30 sec)

mysql> show create table test_innodb\G
*************************** 1. row ***************************
       Table: test_innodb
Create Table: CREATE TABLE `test_innodb` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

mysql>


#####################################




增加列并测试null字段索引


mysql> alter table test_INNODB ADD COLUMN numb int(8) default null;
Query OK, 0 rows affected (0.55 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql>
mysql>
mysql> alter table test_INNODB drop COLUMN numb ;
Query OK, 0 rows affected (0.55 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show create table test_innodb;
+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table       | Create Table                                                                                                                                                                                 |
+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| test_innodb | CREATE TABLE `test_innodb` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 |
+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> alter table test_INNODB ADD COLUMN numb int(8) default null,add column addr varchar(100) default null;
Query OK, 0 rows affected (0.55 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show create table test_innodb;
+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table       | Create Table                                                                                                                                                                                                                                                   |
+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| test_innodb | CREATE TABLE `test_innodb` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(100) DEFAULT NULL,
  `numb` int(8) DEFAULT NULL,
  `addr` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 |
+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql>

mysql> alter table test_innodb add index idx_numb(numb);
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql>
mysql> alter table test_innodb add index idx_addr(addr);
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show create table test_innodb;
+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table       | Create Table                                                                                                                                                                                                                                                                                                         |
+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| test_innodb | CREATE TABLE `test_innodb` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(100) DEFAULT NULL,
  `numb` int(8) DEFAULT NULL,
  `addr` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_numb` (`numb`),
  KEY `idx_addr` (`addr`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 |
+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql>


mysql> insert into test_innodb(name,numb,addr) values('huanglingfei',80,'chengdu'),('wanger'),('mazhi','guizhou'),('zhangsan',60);
ERROR 1136 (21S01): Column count doesn't match value count at row 2
mysql>
mysql> insert into test_innodb(name,numb,addr) values('huanglingfei',80,'chengdu');
Query OK, 1 row affected (0.00 sec)

mysql>
mysql>
mysql>
mysql>
mysql> insert into test_innodb(name) values('wanger');         
Query OK, 1 row affected (0.00 sec)
mysql>




插入错误类型是出现警告并把插字符串自动替换为数字0

mysql> insert into test_innodb(name,numb) values('mazhi','guizhou'),('zhangsan',60);               
Query OK, 2 rows affected, 1 warning (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 1

mysql> select * from test_innodb;
+----+--------------+------+---------+
| id | name         | numb | addr    |
+----+--------------+------+---------+
|  1 | hlf          | NULL | NULL    |
|  2 | huanglf      | NULL | NULL    |
|  3 | huanglingfei |   80 | chengdu |
|  4 | wanger       | NULL | NULL    |
|  5 | mazhi        |    0 | NULL    |
|  6 | zhangsan     |   60 | NULL    |
+----+--------------+------+---------+
6 rows in set (0.00 sec)

mysql>


mysql> explain select * from test_innodb where id in(3,4,5);
+----+-------------+-------------+-------+---------------+---------+---------+------+------+-------------+
| id | select_type | table       | type  | possible_keys | key     | key_len | ref  | rows | Extra       |
+----+-------------+-------------+-------+---------------+---------+---------+------+------+-------------+
|  1 | SIMPLE      | test_innodb | range | PRIMARY       | PRIMARY | 4       | NULL |    3 | Using where |
+----+-------------+-------------+-------+---------------+---------+---------+------+------+-------------+
1 row in set (0.00 sec)

mysql> select * from test_innodb where id in(3,4,5);
+----+--------------+------+---------+
| id | name         | numb | addr    |
+----+--------------+------+---------+
|  3 | huanglingfei |   80 | chengdu |
|  4 | wanger       | NULL | NULL    |
|  5 | mazhi        |    0 | NULL    |
+----+--------------+------+---------+
3 rows in set (0.00 sec)

mysql>



数字字段


mysql>
mysql> explain select * from test_innodb where numb=80;
+----+-------------+-------------+------+---------------+----------+---------+-------+------+-------+
| id | select_type | table       | type | possible_keys | key      | key_len | ref   | rows | Extra |
+----+-------------+-------------+------+---------------+----------+---------+-------+------+-------+
|  1 | SIMPLE      | test_innodb | ref  | idx_numb      | idx_numb | 5       | const |    1 | NULL  |
+----+-------------+-------------+------+---------------+----------+---------+-------+------+-------+
1 row in set (0.00 sec)

mysql>




mysql> explain select * from test_innodb where numb is not null;
+----+-------------+-------------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table       | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+-------------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | test_innodb | ALL  | idx_numb      | NULL | NULL    | NULL |    6 | Using where |
+----+-------------+-------------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)

mysql>
mysql>
mysql> select * from test_innodb where numb is not null;        
+----+--------------+------+---------+
| id | name         | numb | addr    |
+----+--------------+------+---------+
|  3 | huanglingfei |   80 | chengdu |
|  5 | mazhi        |    0 | NULL    |
|  6 | zhangsan     |   60 | NULL    |
+----+--------------+------+---------+
3 rows in set (0.00 sec)

mysql>
mysql> explain select * from test_innodb where numb is null;   
+----+-------------+-------------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table       | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+-------------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | test_innodb | ALL  | idx_numb      | NULL | NULL    | NULL |    6 | Using where |
+----+-------------+-------------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)

mysql>
mysql> select * from test_innodb where numb is null;        
+----+---------+------+------+
| id | name    | numb | addr |
+----+---------+------+------+
|  1 | hlf     | NULL | NULL |
|  2 | huanglf | NULL | NULL |
|  4 | wanger  | NULL | NULL |
+----+---------+------+------+
3 rows in set (0.00 sec)

mysql>





字符字段 is not null 居然走了索引


mysql> explain select * from test_innodb where  addr='chengdu';
+----+-------------+-------------+------+---------------+----------+---------+-------+------+-----------------------+
| id | select_type | table       | type | possible_keys | key      | key_len | ref   | rows | Extra                 |
+----+-------------+-------------+------+---------------+----------+---------+-------+------+-----------------------+
|  1 | SIMPLE      | test_innodb | ref  | idx_addr      | idx_addr | 303     | const |    1 | Using index condition |
+----+-------------+-------------+------+---------------+----------+---------+-------+------+-----------------------+
1 row in set (0.00 sec)

mysql>
mysql> select * from test_innodb where  addr='chengdu';        
+----+--------------+------+---------+
| id | name         | numb | addr    |
+----+--------------+------+---------+
|  3 | huanglingfei |   80 | chengdu |
+----+--------------+------+---------+
1 row in set (0.00 sec)

mysql>


mysql> explain select * from test_innodb where  addr is null;   
+----+-------------+-------------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table       | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+-------------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | test_innodb | ALL  | idx_addr      | NULL | NULL    | NULL |    6 | Using where |
+----+-------------+-------------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)

mysql>
mysql>
mysql>
mysql> select * from test_innodb where  addr is null;
+----+----------+------+------+
| id | name     | numb | addr |
+----+----------+------+------+
|  1 | hlf      | NULL | NULL |
|  2 | huanglf  | NULL | NULL |
|  4 | wanger   | NULL | NULL |
|  5 | mazhi    |    0 | NULL |
|  6 | zhangsan |   60 | NULL |
+----+----------+------+------+
5 rows in set (0.00 sec)

mysql>



mysql> explain select * from test_innodb where  addr is not null;
+----+-------------+-------------+-------+---------------+----------+---------+------+------+-----------------------+
| id | select_type | table       | type  | possible_keys | key      | key_len | ref  | rows | Extra                 |
+----+-------------+-------------+-------+---------------+----------+---------+------+------+-----------------------+
|  1 | SIMPLE      | test_innodb | range | idx_addr      | idx_addr | 303     | NULL |    1 | Using index condition |
+----+-------------+-------------+-------+---------------+----------+---------+------+------+-----------------------+
1 row in set (0.00 sec)

mysql>
mysql>
mysql> select * from test_innodb where  addr is not null;        
+----+--------------+------+---------+
| id | name         | numb | addr    |
+----+--------------+------+---------+
|  3 | huanglingfei |   80 | chengdu |
+----+--------------+------+---------+
1 row in set (0.00 sec)

mysql>




2、数据库为5.1.7版本测试:


mysql> select @@version;
+------------+
| @@version  |
+------------+
| 5.1.73-log |
+------------+
1 row in set (0.00 sec)

mysql>

create table test_innodb (id int(11) unsigned NOT NULL primary key AUTO_INCREMENT,
name varchar(100),
numb int(8) default null,
addr varchar(100) default null,
key idx_numb (numb),
key idx_addr (addr)
) engine=innodb;

mysql> create table test_innodb (id int(11) unsigned NOT NULL primary key AUTO_INCREMENT,
    -> name varchar(100),
    -> numb int(8) default null,
    -> addr varchar(100) default null,
    -> key idx_numb (numb),
    -> key idx_addr (addr)
    -> ) engine=innodb;
Query OK, 0 rows affected (0.25 sec)

mysql>

insert into test_innodb(name) values('hlf'),('huanglf');
insert into test_innodb(name,numb,addr) values('huanglingfei',80,'chengdu');
insert into test_innodb(name) values('wanger');         
insert into test_innodb(name,numb) values('mazhi','guizhou'),('zhangsan',60);      


mysql> insert into test_innodb(name) values('hlf'),('huanglf');
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> insert into test_innodb(name,numb,addr) values('huanglingfei',80,'chengdu');
Query OK, 1 row affected (0.00 sec)

mysql> insert into test_innodb(name) values('wanger');         
Query OK, 1 row affected (0.00 sec)

mysql> insert into test_innodb(name,numb) values('mazhi','guizhou'),('zhangsan',60);  
ERROR 1366 (HY000): Incorrect integer value: 'guizhou' for column 'numb' at row 1
mysql>

mysql> insert into test_innodb(name,numb) values('mazhi','0'),('zhangsan',60);         
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from test_innodb;                                               
+----+--------------+------+---------+
| id | name         | numb | addr    |
+----+--------------+------+---------+
|  1 | hlf          | NULL | NULL    |
|  2 | huanglf      | NULL | NULL    |
|  3 | huanglingfei |   80 | chengdu |
|  4 | wanger       | NULL | NULL    |
|  5 | mazhi        |    0 | NULL    |
|  6 | zhangsan     |   60 | NULL    |
+----+--------------+------+---------+
6 rows in set (0.00 sec)

mysql>


从结果看字符字段is not null 也是会走索引的

mysql> explain select * from test_innodb where numb=80;
+----+-------------+-------------+------+---------------+----------+---------+-------+------+-------------+
| id | select_type | table       | type | possible_keys | key      | key_len | ref   | rows | Extra       |
+----+-------------+-------------+------+---------------+----------+---------+-------+------+-------------+
|  1 | SIMPLE      | test_innodb | ref  | idx_numb      | idx_numb | 5       | const |    1 | Using where |
+----+-------------+-------------+------+---------------+----------+---------+-------+------+-------------+
1 row in set (0.00 sec)

mysql>
mysql> explain select * from test_innodb where numb is null;
+----+-------------+-------------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table       | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+-------------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | test_innodb | ALL  | idx_numb      | NULL | NULL    | NULL |    6 | Using where |
+----+-------------+-------------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)

mysql> explain select * from test_innodb where numb is not null;
+----+-------------+-------------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table       | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+-------------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | test_innodb | ALL  | idx_numb      | NULL | NULL    | NULL |    6 | Using where |
+----+-------------+-------------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)

mysql>
mysql> explain select * from test_innodb where addr is null;        
+----+-------------+-------------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table       | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+-------------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | test_innodb | ALL  | idx_addr      | NULL | NULL    | NULL |    6 | Using where |
+----+-------------+-------------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)

mysql>
mysql>
mysql> explain select * from test_innodb where addr is not null;
+----+-------------+-------------+-------+---------------+----------+---------+------+------+-------------+
| id | select_type | table       | type  | possible_keys | key      | key_len | ref  | rows | Extra       |
+----+-------------+-------------+-------+---------------+----------+---------+------+------+-------------+
|  1 | SIMPLE      | test_innodb | range | idx_addr      | idx_addr | 303     | NULL |    1 | Using where |
+----+-------------+-------------+-------+---------------+----------+---------+------+------+-------------+
1 row in set (0.00 sec)

mysql>



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-57281-1-1.html 上篇帖子: Mysql5.6.21-GTID复制 下篇帖子: MySQL双机主从同步与双向同步 mysql null
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表