Home Database Mysql Tutorial SQL Server碎片知多少之:物理磁盘碎片与数据库碎片的区别

SQL Server碎片知多少之:物理磁盘碎片与数据库碎片的区别

Jun 07, 2016 pm 03:48 PM
server sql database physics fragments disk

SQL Server 碎片知多少之:物理磁盘碎片与数据库碎片的区别 每次提到“碎片”,我们自然而然的就想到了我们电脑中的那个磁盘碎片。在数据库中,我们提到碎片,很多的时候我们就开始犯糊涂了,因为我们曾经也在某些地方听说过“索引碎片”这个东西。最后,我

SQL Server碎片知多少之:物理磁盘碎片与数据库碎片的区别

每次提到“碎片”,我们自然而然的就想到了我们电脑中的那个磁盘碎片。在数据库中,我们提到碎片,很多的时候我们就开始犯糊涂了,因为我们曾经也在某些地方听说过“索引碎片”这个东西。最后,我们就开始认为:可能索引“碎片”中的那个“碎片和”和“磁盘碎片”中的那个“碎片”是差不多的,甚至是相同的。


有朋友可能会从数据库的存储机制去分析,但是最后可能分析清晰了索引碎片是怎么样回事,至于它和磁盘碎片是不是一样,就不得而知了。

其实上面说的那些问题,也是我们团队在为客户解决问题的时候遇到的曾经遇到的问题,而且也做了不少傻事情,最后才慢慢的明白到底是怎么回事。


当然,既然写了这篇文章,那么就说明:它们不是一样的,不是所有的“碎片”都是一样的,就好比不是所有长长头发的人都是女人一样。

那我们就来看看它们之前的区别吧。



其实物理磁盘碎片是Windows在物理磁盘上面工作而产生的一个副作用。我们也知道,清理磁盘碎片可以使得我们的计算机运行的更快,而且Windows也是内置了一些磁盘碎片清理的工具。


磁盘碎片之所以使得我们计算机的性能下降,主要是因为它增加了磁头读取数据的延迟时间。我这里借用CareySon的一个图片来说明一下:

SQL Server碎片知多少之:物理磁盘碎片与数据库碎片的区别

我们知道,Windows会把文件保存到磁盘的空闲空间中,如果寻找到的空间不足以存放所有的文件数据,那么文件就分段会被保存到磁盘的多个地方,我们暂且称这些分段为数据块,或者理解为磁盘中的那个“扇区”,每个扇区都是有大小的。那么,当在读取数据的时候,磁盘的磁头就会定位到磁盘中的各个不同的扇区。


一般而言,磁头移动数据块的时间是读取数据块中数据时间的3-4倍。或者说,寻道时间一般是数据读取时间的3-4倍。


如果此时,在计算机的的磁盘中存在碎片(至于碎片如何产生,我这里也就不花时间讲述,重点不在这里,而且也不难),那么数据的读取操作就发生了变化。原本数据读取时的操作是“寻道-读取数据-读取数据”,因为没有碎片,数据所在的扇区是连续的。如果有了碎片,那么读取的操作就变为了“寻道-读取-寻道-读取-寻道-读取”,很显然,数据的读取就延迟了。我们还是给大家看一个更加形象的图示:


SQL Server碎片知多少之:物理磁盘碎片与数据库碎片的区别SQL Server碎片知多少之:物理磁盘碎片与数据库碎片的区别 SQL Server碎片知多少之:物理磁盘碎片与数据库碎片的区别20120821113810.png(20.76 K)
8/21/2012 11:38:40 AM


从上图中可以知道,磁盘碎片使得原本只要读取两个扇区的时间从6ms变到了18ms。试想:如果一个文件有成百上千个扇区,那么读取的延时是非常的严重的,尽管我们感觉不到,因为读取的时间依然很短,但是如何在多用户,大并发的情况下,延时的后果可想而知,当然,此时我们可以采用更多的措施和策略。


对于磁盘碎片的清理,现在已经有了非常多的工具,包括微软自带和第三方的,我这里只是稍微的列举一下:Windows defrag, Power Defra, Page Defrag

说完了磁盘碎片,我就来看看SQL Server的碎片问题。首先要说的是:它们二者是不一样的。


SQL Server采用了比较高级的存储系统(或者说它的存储机制的设计和Windows中一般保存文件的存储机制不一样),使得多个磁盘可以串联起来一起工作,而且还改变了文件的读取和存储的方式。物理磁盘的碎片最终是从从硬件上面解决,不能通过一些运行脚本的方式解决,而SQL Server中的碎片,则是可以的。


SQL Server存储机制使得它可以使用多磁盘存储设备,例如RAIDSANNAS等。磁盘控制器在这些设置中控制着读取数据的操作。在这些设备中,数据被分布在多个磁盘驱动器上面,形成块,和条带。因为数据被分布的保存,在读取的时候就是多个磁盘并行读取,最后每个读取出来的分布数据被组合,成为一个大的数据,传递给上面。这里就不在深入了,因为再说就要涉及到那些N复杂的存储结构,I/O总线了。


我这里只给几个图,大家看看而已:

SQL Server碎片知多少之:物理磁盘碎片与数据库碎片的区别SQL Server碎片知多少之:物理磁盘碎片与数据库碎片的区别 SQL Server碎片知多少之:物理磁盘碎片与数据库碎片的区别20120821113459.png(40.41 K)
8/21/2012 11:38:40 AM


SQL Server碎片知多少之:物理磁盘碎片与数据库碎片的区别SQL Server碎片知多少之:物理磁盘碎片与数据库碎片的区别 SQL Server碎片知多少之:物理磁盘碎片与数据库碎片的区别20120821113542.png(25.66 K)
8/21/2012 11:38:40 AM



其实说了这么多,只是想要告诉朋友们,这里数据保存的不连贯,是因为设计到形成的。这和之前讲的那个磁盘碎片中数据的保存的不连贯的原因是不一样的。


到这里大家可能开始不明白了,可能要问:尽管把数据这样分布保存在多个磁盘上面,对于每个磁盘上面,依然会有碎片

有个疑问,说明大家在思考了。

这里一个要理解的重要概念就是:多磁盘存储系统中的磁盘控制器。这个控制器同时也具有碎片清理的功能,并且它还协调数据的读写操作。也就是说,这个控制器已经在磁盘级别做了清理工作。那么,对于SQL Server而言,只要自身清理碎片就行了。而如何清理SQL Server产生的碎片,只有SQL Server本身知道,这属于它的内部机制,控制器无法操作。

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 尊渡假赌尊渡假赌尊渡假赌

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
1664
14
PHP Tutorial
1269
29
C# Tutorial
1248
24
iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

How to handle database connection errors in PHP How to handle database connection errors in PHP Jun 05, 2024 pm 02:16 PM

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

Tsinghua Optics AI appears in Nature! Physical neural network, backpropagation is no longer needed Tsinghua Optics AI appears in Nature! Physical neural network, backpropagation is no longer needed Aug 10, 2024 pm 10:15 PM

Using light to train neural networks, Tsinghua University results were recently published in Nature! What should I do if I cannot apply the backpropagation algorithm? They proposed a Fully Forward Mode (FFM) training method that directly performs the training process in the physical optical system, overcoming the limitations of traditional digital computer simulations. To put it simply, it used to be necessary to model the physical system in detail and then simulate these models on a computer to train the network. The FFM method eliminates the modeling process and allows the system to directly use experimental data for learning and optimization. This also means that training no longer needs to check each layer from back to front (backpropagation), but can directly update the parameters of the network from front to back. To use an analogy, like a puzzle, backpropagation

How to use database callback functions in Golang? How to use database callback functions in Golang? Jun 03, 2024 pm 02:20 PM

Using the database callback function in Golang can achieve: executing custom code after the specified database operation is completed. Add custom behavior through separate functions without writing additional code. Callback functions are available for insert, update, delete, and query operations. You must use the sql.Exec, sql.QueryRow, or sql.Query function to use the callback function.

How to save JSON data to database in Golang? How to save JSON data to database in Golang? Jun 06, 2024 am 11:24 AM

JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

PHP Database Connection Pitfalls: Avoid Common Mistakes and Misunderstandings PHP Database Connection Pitfalls: Avoid Common Mistakes and Misunderstandings Jun 05, 2024 pm 10:21 PM

To avoid PHP database connection errors, follow best practices: check for connection errors and match variable names with credentials. Use secure storage or environment variables to avoid hardcoding credentials. Close the connection after use to prevent SQL injection and use prepared statements or bound parameters.

See all articles