mysql的insert与update效率提高上万倍的经历

mysql数据库大表,插入还是更新? 操作的速度取决于多种因素,包括表的大小、索引设计、锁机制、存储引擎以及具体的操作场景。假设有一个包含 1000 万行数据的表。希望这些分析和建议对你有所帮助!在 MySQL 数据库中, 阅读详情

公司最近要做类似关注与粉丝的功能,需要将之前已经加为好友或者已经提交好友请求的数据转化为follow关系,我设计了两个表,follow和fan,同时在user_info表中添加了follow_count和fan_count两个字段,在功能上线之前,需要将现网的数据导出,然后转化为follow关系再导入,总数据为75万条(社区刚放开,还没有大规模推广)。

 

策略1:

根据导出的经过排序的uid来操作,一共75万insert操作,75万update操作,整理写成sql文件, 直接cat sql | mysql 执行,居然一个下午都没有完成,通过show processlist发现没死,还在一个一个执行,只能中断操作

 

策略2:

将策略1生成的sql请求分成10份,有点并行操作的意思,结果还是一下午没有做完(其实没有从本跟上解决问题)

 

策略3:

经过分析,确定问题肯定在随机uid的insert和update操作使得mysql频繁的跨库跨表lock table和unlock taoble,时间都花费在表的切换上了。于是决定将操作按照db和table进行分类, 采用了multimap结构,

 

multimap< string, string >  insert_query;

multimap< string, string >  update_query;

 

以db_xx.table_xx为key,value就是要执行的sql,这样生成了两个sql:insert.sql和update.sql, 执行结果:

 

insert complete --- sns_user_97.user_follow_96

insert complete --- sns_user_97.user_follow_97

insert complete --- sns_user_97.user_follow_98

insert complete --- sns_user_97.user_follow_99

insert complete --- sns_user_98.user_follow_00

insert complete --- sns_user_98.user_follow_01

insert complete --- sns_user_98.user_follow_02

insert complete --- sns_user_98.user_follow_03

insert complete --- sns_user_98.user_follow_04

insert complete --- sns_user_98.user_follow_05

insert complete --- sns_user_98.user_follow_06

insert complete --- sns_user_98.user_follow_07

 

 

可以清晰的看到,顺序执行sql操作使得lock table造成的消耗降到了最低,最终结果:

 

update  1分钟内完成

insert    10分钟完成

 

策略4:

将策略3的结果sql按照key分成N块,起N个进程并行执行,这样会在1分钟之内完成,因为不同的表根本不会有锁的进程,效率提升会又上一个量级

 

从这个经验我想到了两点:

1.任务的队列化,如果任务的执行会涉及到大范围的随机跳转操作,而这种跳转还会引起资源竞争,那么最好的办法就是将任务队列化,按照跳转最少,资源竞争最少的原则进行排序。

2.在任务队列化的基础上,map/reduce

 

(备注:咱机器不行,大家不要太在意那个10分钟的时间消耗,我只是说有下这种思路,希望大家多多拍砖)

别再卡在Google验证了!用Header Editor插件5分钟搞定TryHackMe注册(附最新.json规则) 本文提供了使用Header Editor插件快速解决TryHackMe注册时Google验证失败问题的详细指南。通过导入特定的JSON规则文件,用户可以轻松绕过网络限制,5分钟内完成账号注册,无需复杂设置。文章包含完整的配置步骤、常见问题解决方案以及安全使用建议。 阅读详情

相关推荐

从虚拟机到双系统:手把手教你为Gromacs搭建最强Linux环境(含WSL2、Ubuntu22.04配置)

本文详细指导如何在Windows系统下为Gromacs分子动力学模拟搭建高性能Linux环境,涵盖WSL2和Ubuntu22.04双系统配置。通过对比虚拟机、WSL2和原生Linux的性能数据,提供GPU加速、内存优化等实用技巧,帮助科研人员提升计算效率。

weixin_30861797的博客 270

数据库操作优化(insert、update、delete、select)

一、insert优化: 要操作的表为test,字段test。这里优化的方式是先组合一条多行的sql语句,然后再进行插入操作,这样就很大程度上优化了插入数据的效率。 测试代码如下: $sql = "insert into `test` (`test`) values('$co

精彩人生 1244

一文速学数模-K-means聚类算法实战:信用卡用户画像聚类分析

该项目算是非常经典的金融业务用户画像的基础分析了,主要根据用户信用卡使用行为数据进行分析,根据收集到的不同字段信息,对每个用户划分类别。这里需要说明一下的是,聚类模型只是将具有相似行为的大部分用户聚集到一个类别里面,这点并不会考虑到每个字段的含义,也就是分成的类别并不是用户价值等级,此类别仅仅是这个类别大体相同的信用卡用户行为对象,并不能给每个用户价值打上标签,那是评价模型做的事,这里要注意不要搞混淆了,用户价值评价是评价模型决定的,而画像是聚类模型分析的。

master_hunter的博客 1万+

oracle undo表空间与redo日志

undo表空间与redo日志文件在oracle中的作用非常重要,本文重点介绍undo回滚段的作用与特点,同时简单介绍undo与redo的区别和各自己的作用: 一.undo中数据的特点: 1。是数据修改前的备份,主要是保证用户的读一致性 2. 在事务修改数据时产生 3。至少保存到事务结束 二。undo数据的作用: 1.回滚(rollback)操作 2.实现读一...

weixin_34062469的博客 525

OSG第三方库编译之二十一:minizip编译(Windows、Linux、Macos环境下编译)

OSG编译;第三方库;跨平台编译;minizip编译(Windows、Linux、Macos环境下编译)

LuckyHanMo的博客 2621

knowhere | 第一课:项目导论与环境搭建

摘要 本课程是Knowhere项目的第一课,重点介绍项目整体架构和开发环境搭建。Knowhere定位为AI Agent的文档记忆基础设施,通过解析多格式文档并保留结构化信息(如章节层级、表格语义等),为Agent提供可导航的文档知识。项目采用Python monorepo结构,核心包括: apps/api:FastAPI服务,处理HTTP请求和流程调用 apps/worker:Celery服务,执行文档解析等耗时任务 packages/shared-python:共享基础设施层 课程明确了各层职责边界,并推

weixin_43674738的博客 153

20w条数据MySQL速度_有20万条数据,使用mysql数据库,insert与update哪个速度快;

展开全部insert会更快一点,可以使用 INSERT INTO SELECT FROM < source_table> 高效地将大量行从一个表(例如临时表)。传输到按62616964757a686964616fe4b893e5b19e31333431363637最小方式记录日志的其他表中。按最小方式记录日志可以提高语句的性能,减少在事务期间此操作填充可用事务日志空间的可能性。IN...

weixin_31155097的博客 1974

SAP Clean Core 对传统 ABAP 开发人员来说意味着什么:机遇和挑战

SAP Clean Core是一种战略方法,旨在优化和现代化 SAP S/4HANA 系统,通过尽量减少定制,确保核心系统的稳定性、高效性和对云环境的兼容性。Clean Core 作为一种设计思路,致力于让 ERP 系统的核心功能保持简洁、规范,避免不必要的复杂化,最终维持一个标准化的核心,使系统更容易升级、性能更优越,同时支持快速的创新能力。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 611

mysql delte_[转载]MySQL的insert、replace和update语句执行效率

测试insert、replace和update语句执行效率测试程序#include #include #include #include #include #include "/usr/local/include/mysql/mysql.h"using namespace std;MYSQL mysql;void exiterr(int exitcode){printf("%sn", mysql_...

weixin_33557333的博客 837

华为OD机试 C++【TLV解析】

这里的valueOffset指的是value在整串消息里的起始位置(从0开始数)。这表示tag是15(十六进制的0F),length是4,接着的8个字符是value的内容。你收到了一串由两端设备传递的TLV格式的消息,现在你需要根据这串消息生成一个对应的(tag, length, valueOffset)列表。tag是15,对应的数据长度为4,value从第3个字节开始,所以偏移量为2。第二行:一个数字n,表示我们给出的tag的数量(0 < n < 1000)。第一行:一串消息,长度不超过10000。

wtswts1232的博客 482

YOLOv8 更换损失函数DIoU、EIoU、GIoU...Inner-IoU...

介绍了一些损失函数,并将其应用到yolov8训练中

分享一些自己改进代码的实例 888

提升mysql大数据插入或者更新数据条目的执行效率

我在做项目的时候有这么个需求,我需要对每天产生的16000条数据进行批量的更新修改, 首先我们先在数据表里面查询新的数据条目是否已经在现有的表里存在,如果存在就执行update方法,如果不存在就直接执行insert方法 我的做法就是 for(循环16000条数据){ String sql = "select * from user where userid=? and uname=? an

思考的蜗牛 8791

IO Virtualization with Virtio.part 2 [十三]

通过对virtio_device_id 中的 device 和 vendor 的值进行比较,我们找到对应的 virtio 驱动,由于这里我们使用 i2c virtio 为例,所以调用 virtio_i2c_probe 进行 virtio i2c 设备的初始化。在和 virtio mmio 设备交互时我们会读取 VIRTIO_MMIO_DEVICE_ID 和 VIRTIO_MMIO_VENDOR_ID,并将其值写入 virtio_device_id 中,这个就会用作后面和 virtio 设备驱动的匹配参数。

cxjczy1990的博客 1211

insert和update的一些性能对比测试

这是学习笔记的第1933篇文章 今天在做中间件的测试时,突然想到之前的一些思路也可以借鉴一下,这块的内容还是比较有意思,简单剧透一下,如何把drop操作转换为alt...

杨建荣的学习笔记 3420

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

mysql批量插入跟更新_Mysql批量插入和更新的性能

利用Hibernate,连接池使用的是BoneCP,做了一个MySql批量插入和批量更新的Demo,出现了下面两个问题。1.批量插入。我采用的是原生态的JDBC,每次批量插入60条数据左右(数据量不固定),循环3000次。每次在循环到100次左右的时候,会出现“com.jolbox.bonecp.ConnectionPartition - BoneCP detected an unclosed c...

weixin_36444022的博客 577

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

mysql 插入更新多条数据的效率问题

http://topic.youkuaiyun.com/u/20110310/13/3279054f-5022-4fac-baa7-d194f5335ba0.html http://www.rosoo.net/a/201103/11043.html http://topic.youkuaiyun.com/u/20080921/18/718f167e-e030-439d-a206-b2dc3dc99a3f.ht

linfengfeiye的专栏 1037

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

mysql 主键重复则覆盖_sql实现插入数据主键重复或数据已经存在,则更新这条数据...

插入数据主键重复或数据已经存在,则更新这条数据了这个可以使用ON DUPLICATE KEY UPDATE了,下面我们来看一下如何实现我们需要的要求。在做数据库开发的时候,经常会遇到这样的一种情景:当一条数据不存在的时候,插入这条数据,如果这条数据的主键已经在数据库中存在,那么更新这条数据。你们一般怎么做呢?先根据主键查询数据,然后判断是否存在数据,如果存在数据,则update字段,否则inser...

weixin_35322457的博客 4200

update加索引还是很慢_insert和update的一些性能对比测试

这是学习笔记的第1933篇文章 今天在做中间件的测试时,突然想到之前的一些思路也可以借鉴一下,这块的内容还是比较有意思,简单剧透一下,如何把drop操作转换为alter,如何把alter操作转换为DML操作,其实都是设计中的一些经验法则,而今天要聊的是另外一个,如何把一个update转换为一条insert.先说下问题的背景,最近在测试一个业务时,业务的并发很高,我们在应用架构改造中...

weixin_39622225的博客 818

mysql对大表执行update速度慢时,试试改用insert可能会有意想不到的发现

实例:需要根据用户日志的ip地址计算出其地理地址 表结构: 用户日志表(200万条记录),其中address是待填充的字段: CREATE TABLE `tmp_open_ip` ( `email` varchar(60) NOT NULL DEFAULT '', `address` varchar(50) NOT NULL DEFAULT '', ...

weixin_33985507的博客 1905

mysql批量更新方式的比较

sql mysql 批量更新 Update 效率 replace duplicate

m0_52133525的博客 2655

mysql insert update慢,MySQL插入,更新非常慢

Our server database is in mysql 5.1we have 754 tables in our db.We create a table for each project. Hence the large no of tables.From past one week i have noticed a very long delay in inserts and upda...

weixin_29795345的博客 657

20w条数据MySQL速度_有20万条数据,使用mysql数据库,insert与update哪个速度快

MySQL 5.7.12主要测试 不同刷盘参数e68a8462616964757a686964616f31333433643032 对性能的影响, 使用以下三个场景:sync_binlog=1, innodb_flush_log_at_trx_commit=1, 简写为b1e1 (binlog-1-engine-1)sync_binlog=0, innodb_flush_log_at_trx_co...

weixin_34616304的博客 1168

MySQL中修改数据的命令: INSERT、UPDATA、DELETE。

INSERT:向数据库插入新的记录 INSERT语句的基本语法: INSERT INTO <表名> (字段1, 字段2, ...) VALUES (值1, 值2, ...); 练习命令: INSERT INTO students (id,class_id,name,gender,score) VALUES(20,2,"诸葛钢铁","女",88); 运行SELECT * FROM students;命令得到: 在本练习中也可以不用列出id字段和对应的值,因为在这题中id字段是一个自增主键,

weixin_49198853的博客 7472

mysql多表批量插入数据根据唯一索引选择插入还是更新ON DUPLICATE KEY UPDATE

在一次做项目后台统计的时候出现了一个问题:怎么从其他表(例如日志表)批量插入数据到统计表当中,我的数据库用的是mysql,而且这个时候还要考虑是是否存在该条记录选择插入还是更新,那个时候对我这个菜鸟还是有点难度的,后来查了一些资料,最好使用的是ON DUPLICATE KEY UPDATE,但是我查了半天没查到是将其他表批量插入的sql,倒是可以用mybatis通过<collection&g...

weixin_42736075的博客 910

Oracle数据库更新和插入哪个更快?数据库实测

Oracle数据库更新和插入哪个更快?数据库实测,Oracle数据库在1千万条数据前提下,根据主键更新和插入新数据的速度相差不大。

weixin_46967040的博客 1696
上一篇: core文件生成时代进程号
下一篇: fastcgi如何取得url或者cookie
cleanfield
博客等级 码龄21年 126粉丝 189原创
评论 5
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值