解析数据页面头部结构: if object_id('test') is not null drop table test go create table test( id int,birth datetime,name char(10)) insert into test select 1,'2009-11-27','aaaa' union all select 2,'2009-11-27','aaaa' exec sp_spaceused 'te
解析数据页面头部结构:
if object_id('test') is not null
drop table test
go
create table test( id int,birth datetime,name char(10))
insert into test
select 1,'2009-11-27','aaaa' union all
select 2,'2009-11-27','aaaa'
exec sp_spaceused 'test'
结构:
name
rows
reserved
Data
index_size
unused
test
2
16 KB
8 KB
8 KB
0 KB
通过dbcc ind (test,test,0) 可以查看到该表有两个页,页号分别为109,和89,其中89为数据页。下面通过dbcc page 我们可以查看到该数据页的头部结构,下面我们就来解析头部结构每一个字段的含义。
dbcc traceon(3604)
dbcc page(test,1,89,1)
m_pageId = (1:89)
数据页号
m_headerVersion = 1
头文件版本号,从7.0以后,一直为1
m_type = 1
页面类型,1为数据页
m_typeFlagBits = 0x4
数据页和索引页为4,其他页为0
m_level = 0
该页在索引页(B树)中的级数
m_flagBits = 0x8000
页面标志
m_objId (AllocUnitId.idObj) = 83
m_indexId (AllocUnitId.idInd) = 256
Metadata: AllocUnitId = 72057594043367424
存储单元的ID
Metadata: PartitionId = 72057594038386688
数据页所在的分区号
Metadata: IndexId = 0
页面的索引号
Metadata: ObjectId = 2089058478
该页面所属的对象的id,可以使用object_id获得
m_prevPage = (0:0)
该数据页的前一页面
m_nextPage = (0:0)
该数据页的后一页面
pminlen = 26
定长数据所占的字节数
m_slotCnt = 2
页面中的数据的行数
m_freeCnt = 8034
页面中剩余的空间
m_freeData = 154
从第一个字节到最后一个字节的空间字节数
m_reservedCnt = 0
活动事务释放的字节数
m_lsn = (30:170:20)
日志记录号
m_xactReserved = 0
最新加入到m_reservedCnt领域的字节数
m_xdesId = (0:0)
添加到m_reservedCnt 的最近的事务id
m_ghostRecCnt = 0
幻影数据的行数
m_tornBits = 0
页的校验位或者被由数据库页面保护形式决定分页保护位取代
注意在头文件中几个重要数据:
1、 pminlen = 26:除了表中固定数据所占的字节数外,还需要加上每行开始的4个字节
的行开销。即:
26=4(行开销)+4(int所占空间)+8(datetime 所占空间)+10(char(10)所占的空间)
2、 m_freeData = 154:页面文件的头结构+(存储每行数据需要的额外空间+数据自身的所占的空间)*(行数)
154=96+(7+22)*2=96+58
3、 m_freeCnt = 8034: 每个页面8K,减去m_freeData,再减去用来记录每行数据行偏移的所需要的空间,(每行2个字节)
8034=8192-154-4
4、 m_slotCnt = 2 该页面中数据的行数
注意下m_freeData这个字段的值,它实际的值是从第一个字节到最后一个字节的空间字节数。假如这个表的结构没有改变过,那么数据的存储是
头部结构(96B)
第一行数据
第二行数据
剩余空间
行的偏移
m_freeData的值是
这三部分数据所占空
间的总和
但是假如修改了表结构,没有进行分页,数据会向后向下移动,那么表的存储情况为变为:
头部结构(96B)
第一行数据

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

Key metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.

Usingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB

MySQL/InnoDB supports four transaction isolation levels: ReadUncommitted, ReadCommitted, RepeatableRead and Serializable. 1.ReadUncommitted allows reading of uncommitted data, which may cause dirty reading. 2. ReadCommitted avoids dirty reading, but non-repeatable reading may occur. 3.RepeatableRead is the default level, avoiding dirty reading and non-repeatable reading, but phantom reading may occur. 4. Serializable avoids all concurrency problems but reduces concurrency. Choosing the appropriate isolation level requires balancing data consistency and performance requirements.

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.