[基操篇]mysql快速准备100w测试数据
1、首先创建个表
CREATE TABLE `t_user` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(60) DEFAULT NULL,
`age` int(4) DEFAULT NULL,
`sex` tinyint(2) DEFAULT NULL,
`like` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2、创建个存储过程用于插入数据
CREATE PROCEDURE `insert_data`()
BEGIN
set @i = 2, @total = 1000000, @insert_sql = 'INSERT INTO `t_user`(`name`, `age`, `sex`, `like`) VALUES ("user0", 0, 0, "like0")', @target = 10000;
WHILE @i <= @total DO
if ((@i / @target) >= 1) then
PREPARE stmt FROM @insert_sql;
EXECUTE stmt;
deallocate prepare stmt;
set @target = @target + 10000;
set @insert_sql = concat('INSERT INTO `t_user`(`name`, `age`, `sex`, `like`) VALUES("',concat('user', @i),'",', @i,',',0,',"', concat('like', @i), '")');
else
set @insert_sql = concat(@insert_sql, ',("',concat('user', @i), '",',@i,',',0,',"', concat('like', @i) , '")');
end if;
SET @i = @i + 1;
END WHILE;
END;
3、执行存储过程
call insert_data();
4、创建联合索引
ALTER TABLE `t_user` ADD INDEX `idx_name_age_like`
(`name`, `age`, `like`);
好了,准备工作已经做好了,那么可以考虑下 下面的问题了
1、明明创建了索引为什么会失效;
EXPLAIN select * from t_user where age = 10000 and `like` = 'like10000';
2、mysql类型隐式转换你了解么;
select id from t_user where name = 0;
3、Btree,B+tree有什么区别;
4、聚簇索引,回表查询,覆盖查询,最左前缀原则;
5、mysql怎么排序的,什么时候使用内存,什么情况使用文件排序;