Home Database Mysql Tutorial 让你的 MySQL 数据库更有效地装载数据_MySQL

让你的 MySQL 数据库更有效地装载数据_MySQL

Jun 01, 2016 pm 02:03 PM
use refresh data database efficient Serve 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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 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)

The vitality of super intelligence awakens! But with the arrival of self-updating AI, mothers no longer have to worry about data bottlenecks The vitality of super intelligence awakens! But with the arrival of self-updating AI, mothers no longer have to worry about data bottlenecks Apr 29, 2024 pm 06:55 PM

I cry to death. The world is madly building big models. The data on the Internet is not enough. It is not enough at all. The training model looks like "The Hunger Games", and AI researchers around the world are worrying about how to feed these data voracious eaters. This problem is particularly prominent in multi-modal tasks. At a time when nothing could be done, a start-up team from the Department of Renmin University of China used its own new model to become the first in China to make "model-generated data feed itself" a reality. Moreover, it is a two-pronged approach on the understanding side and the generation side. Both sides can generate high-quality, multi-modal new data and provide data feedback to the model itself. What is a model? Awaker 1.0, a large multi-modal model that just appeared on the Zhongguancun Forum. Who is the team? Sophon engine. Founded by Gao Yizhao, a doctoral student at Renmin University’s Hillhouse School of Artificial Intelligence.

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

The U.S. Air Force showcases its first AI fighter jet with high profile! The minister personally conducted the test drive without interfering during the whole process, and 100,000 lines of code were tested for 21 times. The U.S. Air Force showcases its first AI fighter jet with high profile! The minister personally conducted the test drive without interfering during the whole process, and 100,000 lines of code were tested for 21 times. May 07, 2024 pm 05:00 PM

Recently, the military circle has been overwhelmed by the news: US military fighter jets can now complete fully automatic air combat using AI. Yes, just recently, the US military’s AI fighter jet was made public for the first time and the mystery was unveiled. The full name of this fighter is the Variable Stability Simulator Test Aircraft (VISTA). It was personally flown by the Secretary of the US Air Force to simulate a one-on-one air battle. On May 2, U.S. Air Force Secretary Frank Kendall took off in an X-62AVISTA at Edwards Air Force Base. Note that during the one-hour flight, all flight actions were completed autonomously by AI! Kendall said - "For the past few decades, we have been thinking about the unlimited potential of autonomous air-to-air combat, but it has always seemed out of reach." However now,

Tesla robots work in factories, Musk: The degree of freedom of hands will reach 22 this year! Tesla robots work in factories, Musk: The degree of freedom of hands will reach 22 this year! May 06, 2024 pm 04:13 PM

The latest video of Tesla's robot Optimus is released, and it can already work in the factory. At normal speed, it sorts batteries (Tesla's 4680 batteries) like this: The official also released what it looks like at 20x speed - on a small "workstation", picking and picking and picking: This time it is released One of the highlights of the video is that Optimus completes this work in the factory, completely autonomously, without human intervention throughout the process. And from the perspective of Optimus, it can also pick up and place the crooked battery, focusing on automatic error correction: Regarding Optimus's hand, NVIDIA scientist Jim Fan gave a high evaluation: Optimus's hand is the world's five-fingered robot. One of the most dexterous. Its hands are not only tactile

BTCC tutorial: How to bind and use MetaMask wallet on BTCC exchange? BTCC tutorial: How to bind and use MetaMask wallet on BTCC exchange? Apr 26, 2024 am 09:40 AM

MetaMask (also called Little Fox Wallet in Chinese) is a free and well-received encryption wallet software. Currently, BTCC supports binding to the MetaMask wallet. After binding, you can use the MetaMask wallet to quickly log in, store value, buy coins, etc., and you can also get 20 USDT trial bonus for the first time binding. In the BTCCMetaMask wallet tutorial, we will introduce in detail how to register and use MetaMask, and how to bind and use the Little Fox wallet in BTCC. What is MetaMask wallet? With over 30 million users, MetaMask Little Fox Wallet is one of the most popular cryptocurrency wallets today. It is free to use and can be installed on the network as an extension

Single card running Llama 70B is faster than dual card, Microsoft forced FP6 into A100 | Open source Single card running Llama 70B is faster than dual card, Microsoft forced FP6 into A100 | Open source Apr 29, 2024 pm 04:55 PM

FP8 and lower floating point quantification precision are no longer the "patent" of H100! Lao Huang wanted everyone to use INT8/INT4, and the Microsoft DeepSpeed ​​team started running FP6 on A100 without official support from NVIDIA. Test results show that the new method TC-FPx's FP6 quantization on A100 is close to or occasionally faster than INT4, and has higher accuracy than the latter. On top of this, there is also end-to-end large model support, which has been open sourced and integrated into deep learning inference frameworks such as DeepSpeed. This result also has an immediate effect on accelerating large models - under this framework, using a single card to run Llama, the throughput is 2.65 times higher than that of dual cards. one

Open-Sora comprehensive open source upgrade: supports 16s video generation and 720p resolution Open-Sora comprehensive open source upgrade: supports 16s video generation and 720p resolution Apr 25, 2024 pm 02:55 PM

Open-Sora has been quietly updated in the open source community. It now supports video generation up to 16 seconds, with resolutions up to 720p, and can handle text-to-image, text-to-video, image-to-video, and video-to-video of any aspect ratio. and the generation needs of infinitely long videos. Let's try it out. Generate a horizontal screen Christmas snow scene, post to B site and then generate a vertical screen, and use Douyin to generate a 16-second long video. Now everyone can have a screenwriting addiction. How to play? Guidance GitHub: https://github.com/hpcaitech/Open-Sora What’s even cooler is that Open-Sora is still all open source, including the latest model architecture, the latest model weights, multi-time/resolution/long-term

Within hours of release, Microsoft deleted a large open source model comparable to GPT-4 in seconds! Forgot to take the poison test Within hours of release, Microsoft deleted a large open source model comparable to GPT-4 in seconds! Forgot to take the poison test Apr 23, 2024 pm 05:22 PM

Last week, Microsoft airdropped WizardLM-2, an open source model called GPT-4 level. But I didn’t expect that it would be deleted immediately a few hours after it was posted. Some netizens suddenly discovered that WizardLM’s model weights and announcement posts had all been deleted and were no longer in the Microsoft collection. Apart from the mention of the site, no evidence could be found to prove that this was an official Microsoft project. The GitHub project homepage has become a 404. Project address: https://wizardlm.github.io/ Including the weight of the model on HF, all have disappeared... The whole network is full of confusion, why is WizardLM gone? However, the reason Microsoft did this was because the team forgot to "test" the model. Later, micro

See all articles