mysql数据库每秒能写入多少条数据_数据库插入速度能有50W每秒吗

5c04aea34db742c42db2b5855b3b1868.png

写入速度

MySQL每秒可以插入50w条记录吗?

带着疑问,我们一起看看mysql每秒可以插入多少条记录?

要回答这个问题,首先要考虑影响mysql插入速度的因素有哪些?

硬盘的速度,网卡的速度,写入行的数据量,数据在硬盘中的存放位置等等因素。

简单的数据,插入速度肯定快,复杂的插入肯定慢。

所以单纯这个问题不好回答,最好的办法是进行压力测试,最后求一个平均值。

一 测试环境:

MySQL表结构

CREATE TABLE `user_10w` (

`id` int(10) NOT NULL AUTO_INCREMENT,

`name` varchar(20) DEFAULT NULL,

`mobile` varchar(11) DEFAULT NULL,

`add_time` int(11) DEFAULT NULL,

`groupid` tinyint(1) DEFAULT NULL,

`login_time` int(11) DEFAULT NULL,

PRIMARY KEY (`id`)

) ENGINE=MyISAM AUTO_INCREMENT=4730016 DEFAULT CHARSET=utf8mb4

电脑配置

4核 Intel(R) Core(TM) i3-8100 CPU @ 3.60GHz

ssd 120G硬盘

二. 开始测试

说明:单机测试,不涉及网卡,网络传输的影响

测试程序php pdo

include_once("tool.php");

$dsn = 'mysql:dbname=demo;host=127.0.0.1';

$user = 'root';

$pass = 'root';

try{

$hand = new PDO($dsn, $user, $pass);

}catch(PDOException $e){

echo 'Connection failed: ' . $e->getMessage();

}

$query = $hand->prepare("insert into user_10w(name,mobile,add_time,groupid,last_login_time) values(?,?,?,?,?)");

$n=0;

$count = 1000000; //每次插入100w

$t = time();

echo 'start write,time is '. $t. "\\n";

while(true){

$n++;

$name = Tool::getRandomStr(5);

$mobile = Tool::getRandNum(9);

$group = Tool::getRandNum(1);

$rs = $query->execute([$name,'13'.$mobile,time(),$group,time()]);

if($n>$count)break;

}

echo 'write end, time is '. (time()-$t) . "\\n";

1.无任何索引,单进程

start write,time is 1593338798

write end, time is 50

耗时50s, 平均2w/秒

2. 给name添加普通索引

write end, time is 60

root@test:/data/php# php7 pdo.php

start write,time is 1593393695

write end, time is 61

大概慢了10s ,所以索引会导致插入变慢,因为要更新索引

0773e891a651a8d7df5106a9a4606ee4.png

负载

3. 多进程测试

用go写了一个简单的多进程执行程序

package main

import(

"os/exec"

"sync"

)

var wg sync.WaitGroup

func testRun(wg *sync.WaitGroup){

c := exec.Command("php7", "./pdo.php")

c.Run()

wg.Done()

}

func main(){

var n int = 10

wg.Add(n)

for i:=0;i

go testRun(&wg)

}

wg.Wait();

}02eb58dce0e1dbc1dd6254e7ca168f40.png

4进程负载

结果:

耗时115s ,插入速度平均3.4w/s

MySQL锁表状态

de69a1629175ff68883b0f190049b26a.png

mysql status

10进程一起执行

5c2dd94b6a63a6c2f709c3e5e4b03951.png

10进程

耗时5‘10“ ,插入速度大概3.2w/s

MySQL锁表状态

7230be4f3148a0f7c66ef00edc7070b9.png

MySQL status

最后:

可见,插入速度并没有网上说的那么高,随着进程数增多,锁冲突逐渐增加,插入效率下降。

通过存储过程批量进行插入 ,插入速度应该会更高,但在实际高并发应用中使用并不多!

抛开业务,单纯测这个好像没什么意义,一定要结合业务复杂度,性能需求来做测试,做优化。

通过分布式、拆表提高并发量。

不足之处,请各位指正。

数据库是一个 单位或是一个应用领域的通用数据处理系统,它存储的是属于企业和事业部门、 团体和个人的有关数据的 集合。数据库中的数据是从全局观点出发建立的,按一定的 数据模型进行组织、描述和存储。其结构基于数据间的自然联系,从而可提供一切必要的存取 路径,且数据不再针对某一应用,而是面向全组织,具有整体的结构化特征。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个基本的实现示例,仅供参考,具体实现需要根据实际情况进行调整。 1. 引入RestTemplate和MyBatis-Plus的依赖 在pom.xml中分别添加以下依赖: ```xml <!-- RestTemplate --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- MyBatis-Plus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>{version}</version> </dependency> ``` 其中,{version}指的是MyBatis-Plus的版本号。 2. 创建RestTemplate实例 在SpringBoot的配置类中,创建RestTemplate实例,并将其注入到需要使用的类中。 ```java @Configuration public class MyConfig { @Bean public RestTemplate restTemplate() { return new RestTemplate(); } } ``` 3. 控制每秒调用次数 使用Semaphore来控制每秒调用次数。 ```java private Semaphore semaphore = new Semaphore(3); public String callThirdPartyApi() { if (semaphore.tryAcquire()) { String url = "http://example.com/api"; ResponseEntity<String> response = restTemplate.getForEntity(url, String.class); semaphore.release(); return response.getBody(); } else { throw new RuntimeException("Too many requests"); } } ``` 这里的Semaphore初始化为3,表示每秒最多只能发起3个请求。tryAcquire()方法会尝试获取一个信号量,如果获取成功则发送HTTP请求,否则抛出异常。 4. 将响应报文写入MySQL数据库 使用MyBatis-Plus提供的ORM框架来将响应报文写入MySQL数据库。 首先,定义一个实体类来映射数据库中的表: ```java @Data @TableName("api_response") public class ApiResponse { @TableId(type = IdType.AUTO) private Long id; private String content; private LocalDateTime createTime; } ``` 其中,@TableName注解用来指定表名,@TableId注解用来指定主键,@IdType.AUTO表示自动生成主键。 然后,定义一个Mapper接口来和数据库进行交互: ```java @Mapper public interface ApiResponseMapper extends BaseMapper<ApiResponse> { } ``` 其中,BaseMapper是MyBatis-Plus提供的一个通用Mapper接口,继承它即可获得基本的CRUD操作。 最后,在接收到HTTP响应之后,将响应报文保存到数据库中: ```java @Autowired private ApiResponseMapper apiResponseMapper; public String callThirdPartyApi() { if (semaphore.tryAcquire()) { String url = "http://example.com/api"; ResponseEntity<String> response = restTemplate.getForEntity(url, String.class); semaphore.release(); ApiResponse apiResponse = new ApiResponse(); apiResponse.setContent(response.getBody()); apiResponse.setCreateTime(LocalDateTime.now()); apiResponseMapper.insert(apiResponse); return response.getBody(); } else { throw new RuntimeException("Too many requests"); } } ``` 这里先将响应报文封装成ApiResponse对象,然后调用ApiResponseMapper的insert方法将其插入数据库中。 以上就是一个基本的实现示例,具体实现需要根据实际情况进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值