动静分离网站架构,中国丹阳,安阳企业网站建设,分享到wordpress公司有一个项目#xff0c;需要频繁的插入数据到MySQL数据库中#xff0c;设计目标要求能支持平均每秒插入1000条数据以上。目前功能已经实现#xff0c;不过一做压力测试#xff0c;发现数据库成为瓶颈#xff0c;每秒仅能插入100多条数据#xff0c;远远达不到设计目标… 公司有一个项目需要频繁的插入数据到MySQL数据库中设计目标要求能支持平均每秒插入1000条数据以上。目前功能已经实现不过一做压力测试发现数据库成为瓶颈每秒仅能插入100多条数据远远达不到设计目标。 到MySQL官方网站查了查资料发现MySQL支持在一条INSERT语句中插入多条记录格式如下 INSERT table_name (column1, column2, ..., columnN) VALUES (rec1_val1, rec1_val2, ..., rec1_valN), (rec2_val1, rec2_val2, ..., rec2_valN), ... ... (recM_val1, recM_val2, ..., recM_valN); 按MySQL官方网站用这种方法一次插入多条数据速度比一条一条插入要快很多。在一台开发用的笔记本电脑上做了个测试果然速度惊人。 测试环境DELL Latitude D630 CPU T7250 2.00GHz 内存 2G。Windows XP Pro中文版SP2MySQL 5.0 for Windows。 MySQL是新安装的建立了一个名为test的数据库在test数据库建了一个t_integer表共两个字段test_id和test_value两个字段都是INTEGER类型其中test_id是Primary Key。 准备了两个SQL脚本文件写了个小程序生成的内容分别如下 -- test1.sql TRUNCATE TABLE t_integer; INSERT t_integer (test_id, test_value) VALUES (1, 1234), (2, 1234), (3, 1234), (4, 1234), (5, 1234), (6, 1234), ... ... (9997, 1234), (9998, 1234), (9999, 1234), (10000, 1234); -- test2.sql TRUNCATE TABLE t_integer; INSERT t_integer (test_id, test_value) VALUES (1, 1234); INSERT t_integer (test_id, test_value) VALUES (2, 1234); INSERT t_integer (test_id, test_value) VALUES (3, 1234); INSERT t_integer (test_id, test_value) VALUES (4, 1234); INSERT t_integer (test_id, test_value) VALUES (5, 1234); INSERT t_integer (test_id, test_value) VALUES (6, 1234); ... ... INSERT t_integer (test_id, test_value) VALUES (9997, 1234); INSERT t_integer (test_id, test_value) VALUES (9998, 1234); INSERT t_integer (test_id, test_value) VALUES (9999, 1234); INSERT t_integer (test_id, test_value) VALUES (10000, 1234); 以上两个脚本通过mysql命令行运行分别耗时0.44秒和136.14秒相差达300倍。 基于这个思路只要将需插入的数据进行合并处理应该可以轻松达到每秒1000条的设计要求了。 补充以上的测试都是在InnoDB表引擎基础上进行的而且是AUTOCOMMIT1对比下来速度差异非常显著。之后我将t_integer表引擎设置为MyISAM进行测试test1.sql执行时间为0.11秒test2.sql为1.64秒。 补充2以上的测试均为单机测试之后做了跨机器的测试测试客户端运行脚本的机器和服务器是不同机器服务器是另一台笔记本比单机测试时配置要好些。做跨机器的测试时发现不管是InnoDB还是MyISAMtest1.sql速度都在0.4秒左右而test2.sql在InnoDB时且AUTOCOMMIT1时要80多秒而设置为MyISAM时也要20多秒。 转载于:https://blog.51cto.com/ylj798/1061860