mysql数据5.6.0和5.1.7的null字段索引测试

如果保函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>

时间: 2024-10-05 03:33:27

mysql数据5.6.0和5.1.7的null字段索引测试的相关文章

solr的DIH操作同步mysql数据

1.创建MySQL数据 CREATE TABLE `city` ( `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '城市编号', `province_id` INT(10) UNSIGNED NOT NULL COMMENT '省份编号', `city_name` VARCHAR(25) NULL DEFAULT NULL COMMENT '城市名称', `description` VARCHAR(25) NULL DEFAULT N

Mysql中前边有0的数据,0会被舍去的问题

最近由于项目的需求,需要频繁地拉取不同数据库中的数据,拉取数据的过程中,各种问题,十分悲催,真所谓,一个疏忽,你就要被推倒重来... 在经历了无数次被推倒又站起来,然后又被推倒的艰苦奋斗历程之后,终于完成了数据的迁移.更新.同步.修复.总结一下这期间遇到的部分问题: 1.Mysql中前边有0的数据,0会被舍去的问题 如一条数据为0371xxx,存入数据库后数据变为371xxx (1)如果字段类型必须为int,可以修改字段,增加zero fill,alter table 表名称 modify 字段

Mysql 给数据前用0补位,如:01

alter table z change column b b int(4) zerofill; 将列名加上 zerofill 你要补到多少位,就int(N) 多少 Mysql 给数据前用0补位,如:01

使用Solr索引MySQL数据

环境搭建 1.到apache下载solr,地址:http://mirrors.hust.edu.cn/apache/lucene/solr/ 2.解压到某个目录 3.cd into D:\Solr\solr-4.10.3\example 4.Execute the server by “java -jar startup.jar”Solr会自动运行在自带的Jetty上 5.访问http://localhost:8983/solr/#/ 创建MySQL数据 DataBase Name: mybat

mysql数据的类型以及字段的约束问题【重点】

1.确保mysql字段[包括库名.表名]的正确,必要使用反引号 2.校对集指的是字符之间的关系[对字段值的字符处理]校对集依赖于字符集校对集:在某个字符集下面,这些字符的排序关系称之为校对集[对字段进行某种排序处理] 在phpmyadmin中设置库的编码,使用的ci结尾的编码就是使用了校对集[默认是使用的,但是其实校对集基本用不上] _bin:二进制编码层面直接比较_ci:忽略大小写_cs:不忽略大小写[http://www.jb51.net/article/30865.htm] 3.整型介绍s

Hadoop集群(第10期副刊)_常用MySQL数据库命令

1.系统管理 1.1 连接MySQL 格式: mysql -h主机地址 -u用户名 -p用户密码 举例: 例1:连接到本机上的MySQL. 首先在打开DOS窗口,然后进入目录 mysqlbin,再键入命令"mysql –u root –p",回车后提示你输密码,如果刚安装好MySQL,超级用户"root"是没有密码的,故直接回车即可进入到MySQL中了,MySQL的提示符是: mysql>. 例2:连接到远程主机上的MYSQL.假设远程主机的IP为:110.1

PHP导出MySQL数据到Excel文件

PHP导出MySQL数据到Excel文件 转载 常会碰到需要从数据库中导出数据到Excel文件,用一些开源的类库,比如PHPExcel,确实比较容易实现,但对大量数据的支持很不好,很容易到达PHP内存使用上限.这里的方法是利用fputcsv写CSV文件的方法,直接向浏览器输出Excel文件. ? 1 <br><!--?php// 输出Excel文件头,可把user.csv换成你要的文件名header('Content-Type: application/vnd.ms-excel');he

mysql数据迁移

一. 数据迁移简介所谓的数据迁移,就是指将原有的数据库系统迁移到另外一个业务系统上数据迁移的原因是多种多样的,有可能是业务变更.硬件升级.平台切换或升级mysql数据库 迁移的注意事项:(1).相同版本可以迁移(2).注意版本可以使用的引擎(3).低版本可以向高版本进行数据迁移,高版本一般兼容低版本的特性(4).高版本的数据不能向低版本进行数据迁移,会出现数据不兼容的情况(5).迁移是请注意导出数据和备份数据,出现迁移失败时,立即启动备份方案,保证公司业务的正常运行(6).在数据迁移时,要选择用

html、java、mysql数据交互

此文用于java学习,在此小记. 在此之前有写过一些小的Java Demo,由于时间隔得比较长故而淡忘了,重新拾起. 环境的安装 开发环境:Win7(x64)+MyEclipse 2014+Mysql5.5.34+Navicat MyEclipse 2014的安装和破解网上有很多资料,可自行查找,这里不再过多叙述.我这里安装的是myeclipse-pro-2014-GA-offline-installer-windows,破解文件用的是Myeclipse-2014-破解文件.顺便安装了一个web