Mysql innodb myisam引擎 聚簇索引主键比较长 且有过长字段 实验

数据准备

  • 建立innodb 和myisam 表

  • 利用php连接mysql,规则插入10000条数据,

  • 利用php连接mysql,不规则插入10000条数据

建表MySQL
create table t1(
id char(64) not null,
ver int(11) not null default 0,
c1 varchar(3000),
c2 varchar(3000),
c3 varchar(3000),
primary key  (id),
key idver (id,ver)
) engine myisam charset utf8;

create table t2(
id char(64) not null,
ver int(11) not null default 0,
c1 varchar(3000),
c2 varchar(3000),
c3 varchar(3000),
primary key  (id),
key idver (id,ver)
) engine innodb charset utf8;
插入数据脚本

t1表插入的PHP代码

<?php
$conn = mysqli_connect("localhost","root","","test");
$time_start = microtime_float();

$str = str_repeat('m',3000);
for($i=1;$i<=10000;$i++) {
   $id = md5($i).md5(time());
   $sql = "insert into t1 values ('$id',$i,'$str' , '$str' , '$str')";
   $conn->query($sql);
}

$time_end = microtime_float();
echo 'insert cost:' , ($time_end - $time_start) , " seconds\n";
function microtime_float()
{
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}

t2表将上面脚本里面的t1改成t2即可

<?php
$conn = mysqli_connect("localhost","root","","test");
$time_start = microtime_float();

$str = str_repeat('m',3000);
for($i=1;$i<=10000;$i++) {
   $id = md5($i).md5(time());
   $sql = "insert into t2 values ('$id',$i,'$str' , '$str' , '$str')";
   $conn->query($sql);
}

$time_end = microtime_float();
echo 'insert cost:' , ($time_end - $time_start) , " seconds\n";
function microtime_float()
{
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}

时间

myisam

╰─$ php test.php
insert cost:2.1865890026093 seconds

innodb

╰─$ php test.php
insert cost:8.7707788944244 seconds

测试查询

myisam

set profiling=1;

select id from t1 order by id;

select id from t1 order by id,ver;

show profiles;

mysql> show profiles;
+----------+------------+-----------------------------------+
| Query_ID | Duration   | Query                             |
+----------+------------+-----------------------------------+
|        1 | 0.00806300 | select id from t1 order by id     |
|        2 | 0.00877300 | select id from t1 order by id,ver |
+----------+------------+-----------------------------------+
2 rows in set, 1 warning (0.00 sec)

可以看出 myisam 没多大区别

innodb

select id from t2 order by id;

select id from t2 order by id,ver;

show profiles;

mysql> show profiles;
+----------+------------+-----------------------------------+
| Query_ID | Duration   | Query                             |
+----------+------------+-----------------------------------+
|        1 | 0.00806300 | select id from t1 order by id     |
|        2 | 0.00877300 | select id from t1 order by id,ver |
|        3 | 0.07648800 | select id from t2 order by id     |
|        4 | 0.00538300 | select id from t2 order by id,ver |
+----------+------------+-----------------------------------+
4 rows in set, 1 warning (0.00 sec)

可以看出 innodb (Query_ID 3和4) 差别还是挺大的

删除 innodb中过长字段 c1 c2 c3

alter table t2 drop column c1;

alter table t2 drop column c2;

alter table t2 drop column c3;

select id from t2 order by id;

select id from t2 order by id,ver;

show profiles;

mysql> show profiles;
+----------+------------+-----------------------------------+
| Query_ID | Duration   | Query                             |
+----------+------------+-----------------------------------+
|        1 | 0.00806300 | select id from t1 order by id     |
|        2 | 0.00877300 | select id from t1 order by id,ver |
|        3 | 0.07648800 | select id from t2 order by id     |
|        4 | 0.00538300 | select id from t2 order by id,ver |
|        5 | 0.00003900 | alter table drop column c1        |
|        6 | 4.43217200 | alter table t2 drop column c1     |
|        7 | 1.61069100 | alter table t2 drop column c2     |
|        8 | 0.92016800 | alter table t2 drop column c3     |
|        9 | 0.00013600 | show create table t2              |
|       10 | 0.00475000 | select id from t2 order by id     |
|       11 | 0.00464300 | select id from t2 order by id,ver |
+----------+------------+-----------------------------------+
11 rows in set, 1 warning (0.00 sec)

可以看出 innodb (Query_ID 10和11) 几乎没差别

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值