本文一直以来都只是个标题,在三个月后的今天,熬夜补上内容。
直接上代码:
建utf-8编码的表 t1:
CREATE TABLE IF NOT EXISTS `t1` ( `name` varchar(50) NOT NULL DEFAULT '', KEY `name` (`name`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
随便插入些数据,数量大一点,后面实验结果更清晰,偷个懒,构造随机字符串插入语句
insert into t1(name) select concat( char(round((rand())*25)+97), char(round((rand())*25)+65), char(round((rand())*25)+65), char(round((rand())*25)+97), char(round((rand())*25)+65), char(round((rand())*25)+65), char(round((rand())*25)+97), char(round((rand())*25)+65) )
每次执行插入一条记录,用你熟悉的脚本(python,php,shell等都行)写个循环,执行一万次以上。
将该表复制成一个新表t2,删除一部分数据,1000条左右即可。(推荐使用phpMyAdmin)
再将t2复制为t3,并将字段改为gb2312编码。
使用一个left join语句,写一个语句,查出t2/t3比t1少了哪些记录。
语句很简单,如下:
SELECT SQL_NO_CACHE t1.name, t2.name
FROM t1
LEFT JOIN t2 ON t1.name = t2.name
WHERE t2.name IS NULL
LIMIT 0 , 30
注意加入 SQL_NO_CACHE
,禁用mysql缓存。
先看编码一致的t2表,phpMyAdmin里执行结果:
显示行 0 - 29 ( 1,129 总计, 查询花费 0.0010 秒)
平均耗时大概为0.0010秒
SELECT SQL_NO_CACHE t1.name, t3.name
FROM t1
LEFT JOIN t3 ON t1.name = t3.name
WHERE t2.name IS NULL
LIMIT 0 , 30
phpMyAdmin执行结果:
显示行 0 - 29 ( 30 总计, 查询花费 0.1871 秒)
差两个数量级!
查询语句解释:
t2
id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
---|---|---|---|---|---|---|---|---|---|
1 | SIMPLE | t1 | index | NULL | name | 152 | NULL | 11129 | Using index |
1 | SIMPLE | t2 | ref | name | name | 152 | test.t1.name | 1 | Using where; Using index; Not exists |
t3
id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
---|---|---|---|---|---|---|---|---|---|
1 | SIMPLE | t1 | index | NULL | name | 152 | NULL | 11129 | Using index |
1 | SIMPLE | t3 | index | NULL | name | 102 | NULL | 10000 | Using where; Using index; Not exists |
附带上本次实验的三个表及数据
实验用表导出备份test.sql
延伸阅读
- 全球IP地址分配表_MySQL版_by_fengyqf100131_ip.sql.zip
- mysql修改配置参数innodb_log_file_size后不能正常工作,在phpmyadmin中innodb表状态为“使用中”
- mysql错误/usr/libexec/mysqld: Out of memory (Needed xxx bytes)及The table 'pre_common_session' is full处理手记
- mysql多表join联查语句错误:#1241 - Operand should contain 1 column(s)
- MySQL语句实现字段拆分成多行
- 手把手教你在centos下编译安装配置高性能web服务器环境nginx+php(fast-cgi)+mysql等/适合小内存vps使用
- windows下安装zip压缩版的mysql服务器端v5.7.16
- MySQL/phpmyadmin问题解决手记:#2002 - 服务器没有响应 (或者本地 MySQL 服务器的套接字没有正确配置)
- excel表格导入mysql方法(手工导入)
- php函数mult_iconv:转换任意维数组的字符集编码(扩展iconv函数功能)
- [转]趣话字符集与字符编码
- Microsoft SQL server性能优化必备工具
- mysql对大表执行update速度慢时,试试改用insert可能会有意想不到的发现
- wordpress文件附件转移到另外服务器上实现性能提升/web与文件数据分离
- php版本(5.3,5.5,7.0)及运行模式(fast-cgi/fpm,apache模块)之间性能对比测试