Deep understanding of file storage pages and Applications

Source: Internet
Author: User
We use databases every day. The most commonly used relational databases in our department include Sqlserver and Oracle. Have you ever wondered how these databases are stored in operating system files? Sometimes, in order to design the optimal table structure, write high-performance SQL server scripts to process massive data concurrency, we must understand the underlying principle. As an individual

We use databases every day. The most commonly used relational databases in our department include Sqlserver and Oracle. Have you ever wondered how these databases are stored in operating system files? Sometimes, in order to design the optimal table structure, write high-performance SQL server scripts to process massive data concurrency, we must understand the underlying principle. As an individual

We use databases every day. The most commonly used relational databases in our department include Sqlserver and Oracle. Have you ever wondered how these databases are stored in operating system files? Sometimes, in order to design the optimal table structure, write high-performance SQL server scripts to process massive data concurrency, we must understand the underlying principle. Due to my personal interest, I recently studied the file storage of Sqlserver. Due to the limited level, I will only explain the minimal storage unit-page of Sqlserver.

What are pages and zones?

What is the concept of a page? We know that for the operating system, files can be considered as a large linear space. If capacity is allocated in the order of address space (that is, segmented storage ), it may cause a lot of external fragments, making it difficult to use a lot of capacity again. Only the mobile merge space can free up more space. For example, if I want to apply for bytes of space in the following table, it is clear that the following two spaces are not enough for a single computing, but they are enough to move the merging space.

8 KB

512B

12KB

512B

8 KB

Allocated space

Idle

Allocated space

Idle

Allocated space

Table 1

To make better use of the disk space, Sqlserver draws on the Virtual Memory concept of the operating system to manually divide the file into N 8 KB buckets, all requests are based on 8 KB space, which solves the problem of external fragments. That is to say, the basic unit of data storage in Sqlserver, the page size is 8 KB, each page starts with a 96-byte header used to store information about the page, including the page number, page type, available space of the page, and the allocation unit ID that owns the Page Object. The distribution of the above example is shown in the following table: This solves the problem of external fragmentation.

Industry Distribution

8 KB

12KB

8 KB

XxKB

Page Unit

8 KB

8 KB

8 KB

8 KB

Idle

Table 2

Why is there a partition concept? Isn't there enough page units? To better manage these spaces, Sqlserver divides every eight pages into one partition (as shown in the following table). Just like a hundred dollar bill represents 100 yuan, when I buy a lot of things, I need to pay a hundred yuan worth of money than I can pay a lot of 1 yuan.

One Partition

Page 1

Page 2

Page 3

Page 4

Page 5

Page 6

Page 7

Page 8

Table 3

To have a more specific understanding of a page, the following table shows the structure of the page header:

Figure-1

How are rows stored on pages?

What kind of data is stored in the database? Sqlserver stores data in the unit of behavior. That is to say, each piece of data in the table (each row of data is a block) is stored on the page sequentially. How can we find rows? That is, the start address and end address of a row? Sqlserver stores the start address of each row in two bytes at the end of each page, so that we can locate the start of the row, we can know the end position of the record through the starting position of the next one, so that we can retrieve this row of data.

Figure 2

If I want to retrieve the second data, I will read all the data on one page to the memory, and then read 2 bytes starting from the last read offset of 3rd, how can I find the start position of Row 2 and read the end position of Row 2.

How are columns stored on pages?

Now that we have read the row and it is in the memory, how can we parse all the columns in a row? That is, how are these columns stored? There are only two types of columns in the database table: fixed-length columns and variable-length columns.

First, assuming that only a fixed-length Column exists, it is easy to think of storing each column in the same order, because it is a fixed-length column, you can store the offset of each column in another place separately. to retrieve a specific column, the position of each column is easy to locate, as shown in the following table:

2 bytes

3 bytes

6 bytes

10 bytes

3 bytes

2 bytes

1

23

55

A

C

D

Table-4

If you want to retrieve the red data

Start position = (row start position) + 2 bytes + 3 bytes + 6 bytes + 10 bytes.

End position = start position + 3 bytes.

The length of each column can be stored in another table.

Column

1

2

3

4

5

6

Length (bytes)

2

3

6

10

3

2

Table-5

The detailed information of the Specific Row structure is as follows:

Figure-3

Assume that the table structure is:

Col1

Col2

Col3

Col4

Char (5) (not null)

Int (null)

Char (3) (null)

Char (6) (not null)

Table-6

The data stored in the database is:

Col1

Col2

Col3

Col4

'Abcde'

'123'

'Null'

'Ccc'

Table-7

The data in the database file exists as follows:

Figure 4

If there are variable-length columns, how can this structure be stored? The biggest difference between variable-length columns is that the length of each column is variable (the length of each column is different), that is, it cannot be stored in another table. We can only put the column length in the row. As mentioned above, sqlserver has a row offset matrix.

If the table structure is defined as follows:

Col1

Col2

Col3

Col4

Col5

Char (2) (not null)

Varchar (250) (not null)

Varchar (5) (null)

Varchar (20) (not null)

Small int (null)

Table-8

Assume that the data in this row is:

Col1

Col2

Col3

Col4

Col5

'Aaa'

RELICATE ('x', 250)

Null

'Abc'

123

Table-9

The actual storage format of data in the database is:


Figure 5

Conclusion:

1. Try not to use the null type in database columns. If the value is null, it does not occupy the location and cannot be used as the index key value. If the where statement contains "is null" or "is not null", a full table scan can only be performed. The null type can also easily lead to null reference exceptions.

2. When designing a column, you must specify the length of the column, for example, ID card. In other cases, the varchar edge length type should be used, which not only saves space, but also saves more data on a page. This reduces the number of pages read from the same data volume, reduces I/O, and improves performance.

3. As shown in-1, clustering indexes are not stored in physical order, but stored in logical and physical order (most people may misunderstand it here .)

4. Normally, do not use varchar (max) because the data in this column is definitely not stored in one page. To solve this problem, sqlserver stores only one pointer in the column. Real data is stored on multiple other pages. Each read row contains at least one I/O operation, affecting performance.

Notes, references:

(1) Microsoft SQL Server 2005 Technical Insider: storage engine (Chinese)

(2) Microsoft MSDN: http://msdn.microsoft.com/zh-cn/library/ms190969 (v = SQL .105). aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.