Home Database Mysql Tutorial 擦亮自己的眼睛去看SQLServer之简单Insert

擦亮自己的眼睛去看SQLServer之简单Insert

Jun 07, 2016 pm 03:39 PM
sqlserver Eye Simple my own

写事务日志 :数据修改事务中唯一一个总是需要写入磁盘的操作。并不是修改查询语句的清单,而是修改操作发生之后数据页面的具体变化。是由日志管理器完成。看到写入磁盘,我们应该立刻联想到性能问题,因为这个操作是总是写入磁盘。如果一条语句的操作的数据

  写事务日志:数据修改事务中唯一一个总是需要写入磁盘的操作。并不是修改查询语句的清单,而是修改操作发生之后数据页面的具体变化。是由日志管理器完成。看到写入磁盘,我们应该立刻联想到性能问题,因为这个操作是总是写入磁盘。如果一条语句的操作的数据很大的话,这个耗时是十分可怕的。

  举个例子:如果想知道这个差距,你可以在百万或者千万的表中执行以下两条语句体会以下:truncate table Test以及delete from Test。当然严谨的同学会说truncate是针对区操作,delete是针对页操作,truncate的锁消耗也比delete的锁消耗少。这些是会导致truncate比delete快的原因。但是这些原因不是主要原因,主要原因就是这里说的写事务日志,delete是每次删除一行,并在事务日志中为所删除的每行记录一项,而truncate是通过释放存储表数据所用的数据页来删除数据,并且只在事务日志中记录页的释放。既然事务日志会影响性能,为什么还记录呢?主要解决保护数据以及数据一致性的问题。

  接收写请求:一旦访问方法接收到写事务日志成功的确认信息,就会接收写请求,将写请求发送缓存区管理器。注意了,这里是把请求交给缓存区管理器,缓存区管理器只是操作缓存跟物理文件没有任何关系。这里强调的目的是,如果没有理解这里说的原理的话。你可能会为自己做了大量的插入操作,而数据文件的大小没有任何变化而感到匪夷所思。访问方法表面上起了请求传递的作用,其实它很智能,有一些比较复杂的算法来预测执行情况。

  插入缓冲池:缓冲区管理器在内存中插入数据,插入成功后将确认结果发送给访问方法,最终确认结果到达客户端。

  写入数据文件:这个步骤可以由两个组件任何一个完成。惰性写入器线程定期检查SQL Server空闲缓冲列表的大小,当这个值过低的时候,惰性写入器会扫描整个数据缓存,将所有一段时间没被使用的页面老化。如果找到一段时间没有被使用的脏页,惰性写入器则将其写入磁盘并且删除,然后将这个页面的内存空间标记为空闲空间。惰性写入器还会监测服务器上的空闲物理内存,如果内存很少它会将SQL Server的空闲缓冲列表释放给Windows,在SQL Server负载很重时,它还会在服务器有空闲物理内存且已给SQL Server分配的内存还没有达到我们配置的最大服务器内存(max server memory)时增加SQL Server的空闲缓冲列表以适应负载。

  检查点是检查点线程创建的一个时间点,将保证脏页都写入磁盘,并且在页面头将缓存中的这个页面标记为干净的页面,注意检查点是不删除脏页的。至于检查点的执行时间是要分几种情况的:如果你配置了recovery interval(min),就以这个为准。如果没有配置,并且这上一次检查点结束后写入的事务日志数据超过10MB,则大约每分钟启动一次。还比如,我们人为执行checkpoint执行,或者执行备份重启命令都会触发检查点。抛开我们人为操作,这个具体时间确实无法确定,SQL Server有内部启发算法控制这个值。不过我们可以开启一个跟踪标志3502能查看。这个跟踪标志在错误日志中记录了检查点的开始与结束为止。sql语句为:dbcc traceon(3502) 。

  三、结尾

  今天主要就是介绍了插入语句的执行过程,内容不多。你从这个过程中你会发现SQL Server真的很智能。比如这里的预写日志来保护数据,延迟将数据写入磁盘、预测SQL执行情况、监控负载调整内存等等。设计的都是那么巧妙,大家可以想想如果我们在设计自己的软件时是否可以参考和借鉴呢?

  今天分析就到此结束,文中如有描述不当的地方,欢迎指出。共同进步才是硬道理。

擦亮自己的眼睛去看SQLServer之简单Select

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)

How to solve the problem that the object named already exists in the sqlserver database How to solve the problem that the object named already exists in the sqlserver database Apr 05, 2024 pm 09:42 PM

For objects with the same name that already exist in the SQL Server database, the following steps need to be taken: Confirm the object type (table, view, stored procedure). IF NOT EXISTS can be used to skip creation if the object is empty. If the object has data, use a different name or modify the structure. Use DROP to delete existing objects (use caution, backup recommended). Check for schema changes to make sure there are no references to deleted or renamed objects.

How to import mdf file into sqlserver How to import mdf file into sqlserver Apr 08, 2024 am 11:41 AM

The import steps are as follows: Copy the MDF file to SQL Server's data directory (usually C:\Program Files\Microsoft SQL Server\MSSQL\DATA). In SQL Server Management Studio (SSMS), open the database and select Attach. Click the Add button and select the MDF file. Confirm the database name and click the OK button.

The easiest way to query the hard drive serial number The easiest way to query the hard drive serial number Feb 26, 2024 pm 02:24 PM

The hard disk serial number is an important identifier of the hard disk and is usually used to uniquely identify the hard disk and identify the hardware. In some cases, we may need to query the hard drive serial number, such as when installing an operating system, finding the correct device driver, or performing hard drive repairs. This article will introduce some simple methods to help you check the hard drive serial number. Method 1: Use Windows Command Prompt to open the command prompt. In Windows system, press Win+R keys, enter "cmd" and press Enter key to open the command

What to do if the sqlserver service cannot be started What to do if the sqlserver service cannot be started Apr 05, 2024 pm 10:00 PM

When the SQL Server service fails to start, here are some steps to resolve: Check the error log to determine the root cause. Make sure the service account has permission to start the service. Check whether dependency services are running. Disable antivirus software. Repair SQL Server installation. If the repair does not work, reinstall SQL Server.

How to check sqlserver port number How to check sqlserver port number Apr 05, 2024 pm 09:57 PM

To view the SQL Server port number: Open SSMS and connect to the server. Find the server name in Object Explorer, right-click it and select Properties. In the Connection tab, view the TCP Port field.

How to recover accidentally deleted database in sqlserver How to recover accidentally deleted database in sqlserver Apr 05, 2024 pm 10:39 PM

If you accidentally delete a SQL Server database, you can take the following steps to recover: stop database activity; back up log files; check database logs; recovery options: restore from backup; restore from transaction log; use DBCC CHECKDB; use third-party tools. Please back up your database regularly and enable transaction logging to prevent data loss.

Where is the sqlserver database? Where is the sqlserver database? Apr 05, 2024 pm 08:21 PM

SQL Server database files are usually stored in the following default location: Windows: C:\Program Files\Microsoft SQL Server\MSSQL\DATALinux: /var/opt/mssql/data The database file location can be customized by modifying the database file path setting.

How to delete sqlserver if the installation fails? How to delete sqlserver if the installation fails? Apr 05, 2024 pm 11:27 PM

If the SQL Server installation fails, you can clean it up by following these steps: Uninstall SQL Server Delete registry keys Delete files and folders Restart the computer

See all articles