Home Database Mysql Tutorial 大数据量下MySQL插入方法的性能比较_MySQL

大数据量下MySQL插入方法的性能比较_MySQL

May 27, 2016 pm 01:46 PM
mysql

不管是日常业务数据处理中,还是数据库的导入导出,都可能遇到需要处理大量数据的插入。插入的方式和数据库引擎都会对插入速度造成影响,这篇文章旨在从理论和实践上对各种方法进行分析和比较,方便以后应用中插入方法的选择。

插入分析

MySQL中插入一个记录需要的时间由下列因素组成,其中的数字表示大约比例:

  • 连接:(3)
  • 发送查询给服务器:(2)
  • 分析查询:(2)
  • 插入记录:(1x记录大小)
  • 插入索引:(1x索引)
  • 关闭:(1)

如果我们每插入一条都执行一个SQL语句,那么我们需要执行除了连接和关闭之外的所有步骤N次,这样是非常耗时的,优化的方式有一下几种:

  1. 在每个insert语句中写入多行,批量插入
  2. 将所有查询语句写入事务中
  3. 利用Load Data导入数据

每种方式执行的性能如下。

Innodb引擎

InnoDB 给 MySQL 提供了具有事务(commit)、回滚(rollback)和崩溃修复能力(crash recovery capabilities)的事务安全(transaction-safe (ACID compliant))型表。InnoDB 提供了行锁(locking on row level)以及外键约束(FOREIGN KEY constraints)。

InnoDB 的设计目标是处理大容量数据库系统,它的 CPU 利用率是其它基于磁盘的关系数据库引擎所不能比的。在技术上,InnoDB 是一套放在 MySQL 后台的完整数据库系统,InnoDB 在主内存中建立其专用的缓冲池用于高速缓冲数据和索引。

测试环境

Macbook Air 12mid apache2.2.26 php5.5.10 mysql5.6.16

总数100W条数据

插入完后数据库大小38.6MB(无索引),46.8(有索引)

  • 无索引单条插入 总耗时:229s 峰值内存:246KB
  • 有索引单条插入 总耗时:242s 峰值内存:246KB
  • 无索引批量插入 总耗时:10s 峰值内存:8643KB
  • 有索引批量插入 总耗时:16s 峰值内存:8643KB
  • 无索引事务插入 总耗时:78s 峰值内存:246KB
  • 有索引事务插入 总耗时:82s 峰值内存:246KB
  • 无索引Load Data插入 总耗时:12s 峰值内存:246KB
  • 有索引Load Data插入 总耗时:11s 峰值内存:246KB

MyIASM引擎

MyISAM 是MySQL缺省存贮引擎。设计简单,支持全文搜索。

测试环境

Macbook Air 12mid apache2.2.26 php5.5.10 mysql5.6.16

总数100W条数据

插入完后数据库大小19.1MB(无索引),38.6(有索引)

  • 无索引单条插入 总耗时:82s 峰值内存:246KB
  • 有索引单条插入 总耗时:86s 峰值内存:246KB
  • 无索引批量插入 总耗时:3s 峰值内存:8643KB
  • 有索引批量插入 总耗时:7s 峰值内存:8643KB
  • 无索引Load Data插入 总耗时:6s 峰值内存:246KB
  • 有索引Load Data插入 总耗时:8s 峰值内存:246KB

总结

我测试的数据量不是很大,不过可以大概了解这几种插入方式对于速度的影响,最快的必然是Load Data方式。这种方式相对比较麻烦,因为涉及到了写文件,但是可以兼顾内存和速度。

测试代码

<ol class="dp-j">
<li class="alt"><span><span><?php  </span></span></span></li>
<li><span>$dsn = <span class="string">'mysql:host=localhost;dbname=test'</span><span>; </span></span></li>
<li class="alt"><span>$db = <span class="keyword">new</span><span> PDO($dsn,</span><span class="string">'root'</span><span>,</span><span class="string">''</span><span>,array(PDO::ATTR_PERSISTENT => </span><span class="keyword">true</span><span>)); </span></span></li>
<li><span><span class="comment">//删除上次的插入数据</span><span> </span></span></li>
<li class="alt"><span>$db->query(<span class="string">'delete from `test`'</span><span>); </span></span></li>
<li><span><span class="comment">//开始计时</span><span> </span></span></li>
<li class="alt"><span>$start_time = time(); </span></li>
<li><span>$sum = <span class="number">1000000</span><span>; </span></span></li>
<li class="alt"><span><span class="comment">// 测试选项</span><span> </span></span></li>
<li><span>$num = <span class="number">1</span><span>; </span></span></li>
<li class="alt"><span> </span></li>
<li><span><span class="keyword">if</span><span> ($num == </span><span class="number">1</span><span>){ </span></span></li>
<li class="alt"><span><span class="comment">// 单条插入</span><span> </span></span></li>
<li><span><span class="keyword">for</span><span>($i = </span><span class="number">0</span><span>; $i </span></span></li>
<li class="alt"><span>$db->query(<span class="string">"insert into `test` (`id`,`name`) values ($i,'tsetssdf')"</span><span>); </span></span></li>
<li><span>} </span></li>
<li class="alt"><span>} elseif ($num == <span class="number">2</span><span>) { </span></span></li>
<li><span><span class="comment">// 批量插入,为了不超过max_allowed_packet,选择每10万插入一次</span><span> </span></span></li>
<li class="alt"><span><span class="keyword">for</span><span> ($i = </span><span class="number">0</span><span>; $i </span></span></li>
<li><span><span class="keyword">if</span><span> ($i == $sum - </span><span class="number">1</span><span>) { </span><span class="comment">//最后一次</span><span> </span></span></li>
<li class="alt"><span><span class="keyword">if</span><span> ($i%</span><span class="number">100000</span><span> == </span><span class="number">0</span><span>){ </span></span></li>
<li><span>$values = <span class="string">"($i, 'testtest')"</span><span>; </span></span></li>
<li class="alt"><span>$db->query(<span class="string">"insert into `test` (`id`, `name`) values $values"</span><span>); </span></span></li>
<li><span>} <span class="keyword">else</span><span> { </span></span></li>
<li class="alt"><span>$values .= <span class="string">",($i, 'testtest')"</span><span>; </span></span></li>
<li><span>$db->query(<span class="string">"insert into `test` (`id`, `name`) values $values"</span><span>); </span></span></li>
<li class="alt"><span>} </span></li>
<li><span><span class="keyword">break</span><span>; </span></span></li>
<li class="alt"><span>} </span></li>
<li><span><span class="keyword">if</span><span> ($i%</span><span class="number">100000</span><span> == </span><span class="number">0</span><span>) { </span><span class="comment">//平常只有在这个情况下才插入</span><span> </span></span></li>
<li class="alt"><span><span class="keyword">if</span><span> ($i == </span><span class="number">0</span><span>){ </span></span></li>
<li><span>$values = <span class="string">"($i, 'testtest')"</span><span>; </span></span></li>
<li class="alt"><span>} <span class="keyword">else</span><span> { </span></span></li>
<li><span>$db->query(<span class="string">"insert into `test` (`id`, `name`) values $values"</span><span>); </span></span></li>
<li class="alt"><span>$values = <span class="string">"($i, 'testtest')"</span><span>; </span></span></li>
<li><span>} </span></li>
<li class="alt"><span>} <span class="keyword">else</span><span> { </span></span></li>
<li><span>$values .= <span class="string">",($i, 'testtest')"</span><span>; </span></span></li>
<li class="alt"><span>} </span></li>
<li><span>} </span></li>
<li class="alt"><span>} elseif ($num == <span class="number">3</span><span>) { </span></span></li>
<li><span><span class="comment">// 事务插入</span><span> </span></span></li>
<li class="alt"><span>$db->beginTransaction(); </span></li>
<li><span><span class="keyword">for</span><span>($i = </span><span class="number">0</span><span>; $i </span></span></li>
<li class="alt"><span>$db->query(<span class="string">"insert into `test` (`id`,`name`) values ($i,'tsetssdf')"</span><span>); </span></span></li>
<li><span>} </span></li>
<li class="alt"><span>$db->commit(); </span></li>
<li><span>} elseif ($num == <span class="number">4</span><span>) { </span></span></li>
<li class="alt"><span><span class="comment">// 文件load data</span><span> </span></span></li>
<li><span>$filename = dirname(__FILE__).<span class="string">'/test.sql'</span><span>; </span></span></li>
<li class="alt"><span>$fp = fopen($filename, <span class="string">'w'</span><span>); </span></span></li>
<li><span><span class="keyword">for</span><span>($i = </span><span class="number">0</span><span>; $i </span></span></li>
<li class="alt"><span>fputs($fp, <span class="string">"$i,'testtest'\r\n"</span><span>); </span></span></li>
<li><span>} </span></li>
<li class="alt"><span>$db->exec(<span class="string">"load data infile '$filename' into table test fields terminated by ','"</span><span>); </span></span></li>
<li><span>} </span></li>
<li class="alt"><span> </span></li>
<li><span>$end_time = time(); </span></li>
<li class="alt"><span>echo <span class="string">"总耗时"</span><span>, ($end_time - $start_time), </span><span class="string">"秒\n"</span><span>; </span></span></li>
<li><span>echo <span class="string">"峰值内存"</span><span>, round(memory_get_peak_usage()/</span><span class="number">1000</span><span>), </span><span class="string">"KB\n"</span><span>; </span></span></li>
<li class="alt"><span> </span></li>
<li><span>?> </span></li>
</ol>
Copy after login

以上就是MySQL大量数据插入各种方法性能分析与比较,希望能帮到你。

博文出处:http://yansu.org/2014/04/16/insert-large-number-of-data-in-mysql.html

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1665
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

Solve MySQL mode problem: The experience of using the TheliaMySQLModesChecker module Solve MySQL mode problem: The experience of using the TheliaMySQLModesChecker module Apr 18, 2025 am 08:42 AM

When developing an e-commerce website using Thelia, I encountered a tricky problem: MySQL mode is not set properly, causing some features to not function properly. After some exploration, I found a module called TheliaMySQLModesChecker, which is able to automatically fix the MySQL pattern required by Thelia, completely solving my troubles.

MySQL: Structured Data and Relational Databases MySQL: Structured Data and Relational Databases Apr 18, 2025 am 12:22 AM

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL: Key Features and Capabilities Explained MySQL: Key Features and Capabilities Explained Apr 18, 2025 am 12:17 AM

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

See all articles