Home Database Mysql Tutorial 详述如何提高MySQL中数据装载效率_MySQL

详述如何提高MySQL中数据装载效率_MySQL

Jun 01, 2016 pm 02:03 PM
use refresh how improve efficiency data index load statement

 

很多时候关心的是优化SELECT 查询,因为它们是最常用的查询,而且确定怎样优化它们并不总是直截了当。相对来说,将数据装入数据库是直截了当的。然而,也存在可用来改善数据装载操作效率的策略,其基本原理如下:

成批装载较单行装载更快,因为在装载每个记录后,不需要刷新索引高速缓存;可在成批记录装入后才刷新。

在表无索引时装载比索引后装载更快。如果有索引,不仅必须增加记录到数据文件,而且还要修改每个索引以反映增加了的新记录。

较短的SQL 语句比较长的SQL 语句要快,因为它们涉及服务器方的分析较少,而且还因为将它们通过网络从客户机发送到服务器更快。这些因素中有一些似乎微不足道(特别是最后一个因素),但如果要装载大量的数据,即使是很小的因素也会产生很大的不同结果。我们可以利用上述的一般原理推导出几个关于如何最快地装载数据的实际结论:

LOAD DATA(包括其所有形式)比INSERT 效率高,因为其成批装载行。索引刷新较少,并且服务器只需分析和解释一条语句而不是几条语句。

LOAD DATA 比LOAD DATA LOCAL 效率更高。利用LOAD DATA,文件必须定位在服务器上,而且必须具有FILE 权限,但服务器可从磁盘直接读取文件。利用LOAD DATA LOCAL,客户机读取文件并将其通过网络发送给服务器,这样做很慢。

如果必须使用INSERT,应该利用允许在单个语句中指定多行的形式,例如:

 

可在语句中指定的行越多越好。这样会减少所需的语句数目,降低索引刷新量。如果使用mysqldump 生成数据库备份文件,应该使用--extended-insert 选项,使转储文件包含多行INSERT 语句。还可以使用- - o p t(优化) ,它启用--extended-insert 选项。反之,应该避免使用mysqldump 的--complete-insert 选项;此选项会导致INSERT 语句为单行,执行时间更长,比不用--complete-insert 选项生成的语句需要更多的分析。

使用压缩了的客户机/服务器协议以减少网络数据流量。对于大多数MySQL客户机,可以用--compress 命令行选项来指定。它一般只用于较慢的网络,因为压缩需要占用大量的处理器时间。

让MySQL插入缺省值;不要在INSERT 语句中指定将以任意方式赋予缺省值的列。平均来说,这样做语句会更短,能减少通过网络传送给服务器的字符数。此外,语句包含的值较少,服务器所进行的分析和转换就会较少。

如果表是索引的,则可利用批量插入( LOAD DATA 或多行的INSERT 语句)来减少索引的开销。这样会最小化索引更新的影响,因为索引只需要在所有行处理过时才进行刷新,而不是在每行处理后就刷新。

如果需要将大量数据装入一个新表,应该创建该表且在未索引时装载,装载数据后才创建索引,这样做较快。一次创建索引(而不是每行修改一次索引)较快。

如果在装载之前删除或禁用索引,装入数据后再重新创建或启用索引可能使装载更快。如果想对数据装载使用删除或禁用策略,一定要做一些实验,看这样做是否值得(如果将少量数据装入一个大表中,重建和索引所花费的时间可能比装载数据的时间还要长)。

可用DROP INDEX 和CREATE INDEX 来删除和重建索引。另一种可供选择的方法是利用myisamchk 或isamchk 禁用和启用索引。这需要在MySQL服务器主机上有一个帐户,并对表文件有写入权。为了禁用表索引,可进入相应的数据库目录,执行下列命令之一:

 

对具有.MYI 扩展名的索引文件的MyISAM 表使用myisamchk,对具有.ISM 扩展名的索引文件的ISAM 表使用isamchk。在向表中装入数据后,按如下激活索引:

 

如果决定使用索引禁用和激活,应该使用第13章中介绍的表修复锁定协议以阻止服务器同时更改锁(虽然此时不对表进行修复,但要对它像表修复过程一样进行修改,因此需要使用相同的锁定协议)。

上述数据装载原理也适用于与需要执行不同操作的客户机有关的固定查询。例如,一般希望避免在频繁更新的表上长时间运行SELECT 查询。长时间运行SELECT 查询会产生大量争用,并降低写入程序的性能。一种可能的解决方法为,如果执行写入的主要是INSERT 操作,那么先将记录存入一个临时表,然后定期地将这些记录加入主表中。如果需要立即访问新记录,这不是一个可行的方法。但只要能在一个较短的时间内不访问它们,就可以使用这个方法。使用临时表有两个方面的好处。首先,它减少了与主表上SELECT 查询语句的争用,因此,执行更快。其次,从临时表将记录装入主表的总时间较分别装载记录的总时间少;相应的索引高速缓存只需在每个批量装载结束时进行刷新,而不是在每行装载后刷新。这个策略的一个应用是进入Web 服务器的Web 页访问MySQL数据库。在此情形下,可能没有保证记录立即进入主表的较高权限。

如果数据并不完全是那种在系统非正常关闭事件中插入的单个记录,那么减少索引刷新的另一策略是使用MyISAM 表的DELAYED_KEY_WRITE 表创建选项(如果将MySQL用于某些数据录入工作时可能会出现这种情况)。此选项使索引高速缓存只偶尔刷新,而不是在每次插入后都要刷新。

如果希望在服务器范围内利用延迟索引刷新,只要利用--delayed-key-write 选项启动mysqld 即可。在此情形下,索引块写操作延迟到必须刷新块以便为其他索引值腾出空间为止,或延迟到执行了一个flush-tables 命令后,或延迟到该索引表关闭。

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 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)

Use ddrescue to recover data on Linux Use ddrescue to recover data on Linux Mar 20, 2024 pm 01:37 PM

DDREASE is a tool for recovering data from file or block devices such as hard drives, SSDs, RAM disks, CDs, DVDs and USB storage devices. It copies data from one block device to another, leaving corrupted data blocks behind and moving only good data blocks. ddreasue is a powerful recovery tool that is fully automated as it does not require any interference during recovery operations. Additionally, thanks to the ddasue map file, it can be stopped and resumed at any time. Other key features of DDREASE are as follows: It does not overwrite recovered data but fills the gaps in case of iterative recovery. However, it can be truncated if the tool is instructed to do so explicitly. Recover data from multiple files or blocks to a single

What software is crystaldiskmark? -How to use crystaldiskmark? What software is crystaldiskmark? -How to use crystaldiskmark? Mar 18, 2024 pm 02:58 PM

CrystalDiskMark is a small HDD benchmark tool for hard drives that quickly measures sequential and random read/write speeds. Next, let the editor introduce CrystalDiskMark to you and how to use crystaldiskmark~ 1. Introduction to CrystalDiskMark CrystalDiskMark is a widely used disk performance testing tool used to evaluate the read and write speed and performance of mechanical hard drives and solid-state drives (SSD). Random I/O performance. It is a free Windows application and provides a user-friendly interface and various test modes to evaluate different aspects of hard drive performance and is widely used in hardware reviews

Open source! Beyond ZoeDepth! DepthFM: Fast and accurate monocular depth estimation! Open source! Beyond ZoeDepth! DepthFM: Fast and accurate monocular depth estimation! Apr 03, 2024 pm 12:04 PM

0.What does this article do? We propose DepthFM: a versatile and fast state-of-the-art generative monocular depth estimation model. In addition to traditional depth estimation tasks, DepthFM also demonstrates state-of-the-art capabilities in downstream tasks such as depth inpainting. DepthFM is efficient and can synthesize depth maps within a few inference steps. Let’s read about this work together ~ 1. Paper information title: DepthFM: FastMonocularDepthEstimationwithFlowMatching Author: MingGui, JohannesS.Fischer, UlrichPrestel, PingchuanMa, Dmytr

How to download foobar2000? -How to use foobar2000 How to download foobar2000? -How to use foobar2000 Mar 18, 2024 am 10:58 AM

foobar2000 is a software that can listen to music resources at any time. It brings you all kinds of music with lossless sound quality. The enhanced version of the music player allows you to get a more comprehensive and comfortable music experience. Its design concept is to play the advanced audio on the computer The device is transplanted to mobile phones to provide a more convenient and efficient music playback experience. The interface design is simple, clear and easy to use. It adopts a minimalist design style without too many decorations and cumbersome operations to get started quickly. It also supports a variety of skins and Theme, personalize settings according to your own preferences, and create an exclusive music player that supports the playback of multiple audio formats. It also supports the audio gain function to adjust the volume according to your own hearing conditions to avoid hearing damage caused by excessive volume. Next, let me help you

Google is ecstatic: JAX performance surpasses Pytorch and TensorFlow! It may become the fastest choice for GPU inference training Google is ecstatic: JAX performance surpasses Pytorch and TensorFlow! It may become the fastest choice for GPU inference training Apr 01, 2024 pm 07:46 PM

The performance of JAX, promoted by Google, has surpassed that of Pytorch and TensorFlow in recent benchmark tests, ranking first in 7 indicators. And the test was not done on the TPU with the best JAX performance. Although among developers, Pytorch is still more popular than Tensorflow. But in the future, perhaps more large models will be trained and run based on the JAX platform. Models Recently, the Keras team benchmarked three backends (TensorFlow, JAX, PyTorch) with the native PyTorch implementation and Keras2 with TensorFlow. First, they select a set of mainstream

Slow Cellular Data Internet Speeds on iPhone: Fixes Slow Cellular Data Internet Speeds on iPhone: Fixes May 03, 2024 pm 09:01 PM

Facing lag, slow mobile data connection on iPhone? Typically, the strength of cellular internet on your phone depends on several factors such as region, cellular network type, roaming type, etc. There are some things you can do to get a faster, more reliable cellular Internet connection. Fix 1 – Force Restart iPhone Sometimes, force restarting your device just resets a lot of things, including the cellular connection. Step 1 – Just press the volume up key once and release. Next, press the Volume Down key and release it again. Step 2 – The next part of the process is to hold the button on the right side. Let the iPhone finish restarting. Enable cellular data and check network speed. Check again Fix 2 – Change data mode While 5G offers better network speeds, it works better when the signal is weaker

How to use NetEase Mailbox Master How to use NetEase Mailbox Master Mar 27, 2024 pm 05:32 PM

NetEase Mailbox, as an email address widely used by Chinese netizens, has always won the trust of users with its stable and efficient services. NetEase Mailbox Master is an email software specially created for mobile phone users. It greatly simplifies the process of sending and receiving emails and makes our email processing more convenient. So how to use NetEase Mailbox Master, and what specific functions it has. Below, the editor of this site will give you a detailed introduction, hoping to help you! First, you can search and download the NetEase Mailbox Master app in the mobile app store. Search for "NetEase Mailbox Master" in App Store or Baidu Mobile Assistant, and then follow the prompts to install it. After the download and installation is completed, we open the NetEase email account and log in. The login interface is as shown below

How to use Baidu Netdisk app How to use Baidu Netdisk app Mar 27, 2024 pm 06:46 PM

Cloud storage has become an indispensable part of our daily life and work nowadays. As one of the leading cloud storage services in China, Baidu Netdisk has won the favor of a large number of users with its powerful storage functions, efficient transmission speed and convenient operation experience. And whether you want to back up important files, share information, watch videos online, or listen to music, Baidu Cloud Disk can meet your needs. However, many users may not understand the specific use method of Baidu Netdisk app, so this tutorial will introduce in detail how to use Baidu Netdisk app. Users who are still confused can follow this article to learn more. ! How to use Baidu Cloud Network Disk: 1. Installation First, when downloading and installing Baidu Cloud software, please select the custom installation option.

See all articles