Home Database Mysql Tutorial 理解SQL SERVER中的逻辑读,预读和物理读

理解SQL SERVER中的逻辑读,预读和物理读

Jun 07, 2016 pm 05:36 PM
Training Courses Database query

www.91xueit.com韩老师IT系统培训课程优化SQL性能,你必须理解数据库查询数据的细节,在查询分析器中打开IO统计,setstatisticsioon,就能查看查询过程关于逻辑

韩老师IT系统培训课程

 

       优化SQL性能,你必须理解数据库查询数据的细节,在查询分析器中打开IO统计,set statistics io on ,就能查看查询过程 关于逻辑读,预读和物理读的显示。下面给大家讲解数据库查询数据的细节,你也就明白了什么是关于逻辑读,预读和物理读 要想把这几个概念弄清楚必须了解数据库查询数据的细节。

SQL SERVER数据存储的形式

      在谈到几种不同的读取方式之前,首先要理解SQL SERVER数据存储的方式.SQL SERVER存储的最小单位为页(Page).每一页大小为8k,SQL SERVER对于页的读取是原子性,要么读完一页,要么完全不读,不会有中间状态。而页之间的数据组织结构为B树(请参考我之前的博文).所以SQL SERVER对于逻辑读,预读,和物理读的单位是页.

image

      SQL SERVER一页的总大小为:8K

      但是这一页存储的数据会是:8K=8192字节-96字节(页头)-36字节(行偏移)=8060字节

      所以每一页用于存储的实际大小为8060字节.

      比如上面AdventureWorks中的Person.Address表,通过SSMS看到这个表的数据空间为:

image

      我们可以通过公式大概推算出占用了多少页:2.250*1024*1024/8060(每页的数据容量)≈293 - 表中非数据占用的空间≈290(上图中的逻辑读取数)

SQL SERVER查询语句执行的顺序

      SQL SERVER查询执行的步骤如果从微观来看,那将会非常多。这里为了讲述逻辑读等概念,,我从比较高的抽象层次来看:

image

      图有些粗糙。

      下面我解释一下图。当遇到一个查询语句时,SQL SERVER会走第一步,分别为生成执行计划(占用CPU和内存资源),同步的用估计的数据去磁盘中取得需要取的数据(占用IO资源,这就是预读),注意,两个第一步是并行的,SQL SERVER通过这种方式来提高查询性能.

      然后查询计划生成好了以后去缓存读取数据.当发现缓存缺少所需要的数据后让缓存再次去读硬盘(物理读)

      最后从缓存中取出所有数据(逻辑读)。

      下面我再通过一个简单的例子说明一下:

image

      这个估计的页数数据可以通过这个DMV看到:

image

    当我们第一次查询完成后,再次进行查询时,所有请求的数据这时已经在缓存中,SQL SERVER这时只要对缓存进行读取就行了,也就是只用进行逻辑读:

image

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
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
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
1670
14
PHP Tutorial
1274
29
C# Tutorial
1256
24
Implement error handling mechanism for database queries in React Query Implement error handling mechanism for database queries in React Query Sep 28, 2023 pm 02:40 PM

Implementing the error handling mechanism of database queries in ReactQuery ReactQuery is a library for managing and caching data, and it is becoming increasingly popular in the front-end field. In applications, we often need to interact with databases, and database queries may cause various errors. Therefore, implementing an effective error handling mechanism is crucial to ensure application stability and user experience. The first step is to install ReactQuery. Add it to the project using the following command: n

Laravel middleware: Add database querying and performance monitoring to your application Laravel middleware: Add database querying and performance monitoring to your application Jul 28, 2023 pm 02:53 PM

Laravel Middleware: Adding Database Query and Performance Monitoring to Applications Introduction: Data query and performance monitoring are very important when developing web applications. Laravel provides a convenient way to handle these requirements, namely middleware. Middleware is a technology that handles between requests and responses. It can perform some logic before the request reaches the controller or after the response is returned to the user. This article will introduce how to use Laravel middleware to implement database query and performance monitoring. 1. Create the middle

How to solve the problem of database query number overflow in Java development How to solve the problem of database query number overflow in Java development Jun 29, 2023 pm 06:46 PM

How to solve the problem of database query number overflow in Java development. Title: How to solve the database query number overflow problem in Java development. Abstract: With the development of the Internet and the gradual increase in the amount of data, the number of database queries is also increasing. In Java development, due to memory limitations, you may encounter the problem of overflow in the number of database queries. This article will introduce several ways to solve this problem. Text: Optimizing database query statements First, we can solve this problem from the perspective of optimizing database query statements. we can use

PHP database query tips: How to use the mysqli_query function to perform SQL queries PHP database query tips: How to use the mysqli_query function to perform SQL queries Jul 29, 2023 pm 04:42 PM

PHP database query tips: How to use the mysqli_query function to perform SQL queries When developing PHP applications, interacting with the database is a very important part. For query operations, PHP provides some built-in functions to execute SQL statements. This article will focus on how to use the mysqli_query function to help developers better perform database query operations. 1. Introduction to mysqli_query function The mysqli_query function is a built-in function of PHP.

How to query a database and display the results using PHP How to query a database and display the results using PHP May 02, 2024 pm 02:15 PM

Steps to use PHP to query the database and display the results: connect to the database; query the database; display the results, traverse the rows of the query results and output specific column data.

PHP High Performance: How to Optimize Database Queries PHP High Performance: How to Optimize Database Queries Jun 04, 2023 am 08:40 AM

In the current Internet era, with the explosive growth of data, databases have become the core of a service. The performance and speed of the database directly affect the user experience and usability of the website and its applications. Therefore, how to optimize database queries is an issue that developers need to focus on. In the PHP language, through the optimization of database query statements, the performance of the program can be improved, the burden on the server can be reduced, and the stability of the service can be improved. This article will introduce how to optimize database queries from the following aspects: 1. Using indexes when performing queries

How to perform full-text search in PHP? How to perform full-text search in PHP? May 13, 2023 am 08:00 AM

With the continuous development of Internet technology, the explosive growth of data volume and the widespread application of various text data, full-text retrieval has become a very important technology. Full-text search is a method that can quickly and accurately find text data. It is widely used in application scenarios such as search engines, forums, blogs, and e-commerce websites. How to implement full-text search in PHP programming? 1. What is full-text search? In traditional relational databases, we usually use SQL statements for fuzzy queries. However, when the amount of data is large, this query method will

How to implement database query and result mapping in PHP? How to implement database query and result mapping in PHP? Jul 01, 2023 am 09:04 AM

PHP is a very popular server-side programming language that is very flexible and easy to learn and use. Database querying and result mapping are very important steps when developing web applications. In this article, we will explore how PHP implements database querying and result mapping. First, let's understand what database queries and result mapping are. Database query is the process of retrieving data through a database server. Result mapping is the process of converting the results of a database query into data structures that can be used in the program. PHP provides

See all articles