建表
drop table if exists t1;
CREATE TABLE t1(
id int NOT NULL AUTO_INCREMENT PRIMARY KEY comment '主键',
person_id tinyint not null comment '用户id',
person_name varchar(30) comment '用户名称',
gmt_create datetime not null comment '创建时间',
gmt_modified datetime comment '修改时间'
) comment '测试表2048条';
插入一条数据
insert into t1 values(1,1,'user_1', NOW(), now());
利用mysql伪列rownum 设置伪列起始点为1
select (@i:=@i+1) as rownum, person_name from t1, (select @i:=100) as init;
set @i=1;
运行下面的sql
执行20次即2的20次方=1048576 条记录(100万)
执行23次即2的23次方=8388608 条记录(800万)
执行24次即2的24次方=16777216 条记录(1600万)
执行25次即2的25次方=33554432 条记录(3300万)
执行26次即2的26次方=67108864 条记录(6700万)
执行27次即2的27次方=134217728 条记录(1亿3千万)
insert into t1(id, person_id, person_name, gmt_create, gmt_modified)
select @i:=@i+1,
left(rand()*10,1) as person_id,
concat('user_',@i%2048),
date_add(gmt_create,interval + @i*cast(rand()*100 as signed) SECOND),
date_add(date_add(gmt_modified,interval +@i*cast(rand()*100 as signed) SECOND), interval + cast(rand()*1000000 as signed) SECOND)
from t1;
说明
LEFT()函数是一个字符串函数,它返回具有指定长度的字符串的左边部分。
下面是LEFT()函数的语法 -
LEFT(str,length);
SQL
LEFT()函数接受两个参数:
str是要提取子字符串的字符串。length是一个正整数,指定将从左边返回的字符数
INTERVAL关键字
INTERVAL关键字可以用于计算时间间隔,可以有以下用法。
1,直接计算时间间隔。
例1:查询当前时间之前2个小时的日期:
SELECT NOW()-INTERVAL '2' HOUR;
SELECT
id,
percent,
t_date,
t_date - INTERVAL 2 HOUR
FROM
test
where t_date - INTERVAL 2 HOUR>'2020-11-02';
INTERVAL后面的数字可以用数字格式或者字符格式,当时间单位是YEAR_MONTH这种时,必须用字符格式
MySQL DATE_ADD() 函数
定义和用法
DATE_ADD() 函数向日期添加指定的时间间隔。
语法
DATE_ADD(date,INTERVAL expr type)
我们希望向 "OrderDate" 添加 2 天
DATE_ADD(OrderDate,INTERVAL 2 DAY)
CAST函数将任何类型的值转换为具有指定类型的值
rand()的取值范围为[0,1)
tinyint字段举例unsigned字段的取值范围是0-255,而signed的范围是-128 - 127
SECOND 秒
rand()的取值范围为[0,1)
tinyint字段举例unsigned字段的取值范围是0-255,而signed的范围是-128 - 127
SECOND 秒
创建索引
create index idx_person_id on t1(person_id);
create index idx_gmt_create on t1(gmt_create);
create index idx_gmt_modified on t1(gmt_modified);
--end--