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

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

What is the difference between HQL and SQL in Hibernate framework? What is the difference between HQL and SQL in Hibernate framework? Apr 17, 2024 pm 02:57 PM

HQL and SQL are compared in the Hibernate framework: HQL (1. Object-oriented syntax, 2. Database-independent queries, 3. Type safety), while SQL directly operates the database (1. Database-independent standards, 2. Complex executable queries and data manipulation).

How does Go language implement the addition, deletion, modification and query operations of the database? How does Go language implement the addition, deletion, modification and query operations of the database? Mar 27, 2024 pm 09:39 PM

Go language is an efficient, concise and easy-to-learn programming language. It is favored by developers because of its advantages in concurrent programming and network programming. In actual development, database operations are an indispensable part. This article will introduce how to use Go language to implement database addition, deletion, modification and query operations. In Go language, we usually use third-party libraries to operate databases, such as commonly used sql packages, gorm, etc. Here we take the sql package as an example to introduce how to implement the addition, deletion, modification and query operations of the database. Assume we are using a MySQL database.

How to set a save disk for downloaded files in Windows 11 How to set a save disk for downloaded files in Windows 11 Apr 20, 2024 pm 06:32 PM

For users who are new to the Windows 11 system, usually the file download path will follow the system default settings, and the files are often saved in the system disk. In the long run, it may affect the overall performance of the computer by occupying too much system disk space. In order to manage and allocate storage space more rationally, it is important to learn how to customize the save disk for downloaded files in Windows 11 system. This article will explain in detail how to flexibly set the target disk for downloaded files in Windows 11 to meet users' personalized storage needs. If this is what you need, you may wish to continue reading and we will introduce you to the steps of this practical function in detail. Operation method 1. Double-click to open "This PC" on the desktop. 2. Then find the new interface in the left column

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

How does Hibernate implement polymorphic mapping? How does Hibernate implement polymorphic mapping? Apr 17, 2024 pm 12:09 PM

Hibernate polymorphic mapping can map inherited classes to the database and provides the following mapping types: joined-subclass: Create a separate table for the subclass, including all columns of the parent class. table-per-class: Create a separate table for subclasses, containing only subclass-specific columns. union-subclass: similar to joined-subclass, but the parent class table unions all subclass columns.

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

An in-depth analysis of how HTML reads the database An in-depth analysis of how HTML reads the database Apr 09, 2024 pm 12:36 PM

HTML cannot read the database directly, but it can be achieved through JavaScript and AJAX. The steps include establishing a database connection, sending a query, processing the response, and updating the page. This article provides a practical example of using JavaScript, AJAX and PHP to read data from a MySQL database, showing how to dynamically display query results in an HTML page. This example uses XMLHttpRequest to establish a database connection, send a query and process the response, thereby filling data into page elements and realizing the function of HTML reading the database.

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

See all articles