Table of Contents
前言:
准备工作:
步骤:
分析:
Home Database Mysql Tutorial 第十七章配置SQLServer(3)配置“对即时负载的优化”

第十七章配置SQLServer(3)配置“对即时负载的优化”

Jun 07, 2016 pm 03:18 PM
sqlserver optimization Preface load Configuration

前言: 在第一次执行查询或者存储过程时,会创建执行计划并存储在SQLServer的过程缓存内存中。在很多时候,我们会执行一些简单的程序,仅仅执行一次,而为这些查询创建存储过程是非常浪费内存资源的。由于内存不足,可能会导致你的缓存溢出,从而影响性能。

前言:

        在第一次执行查询或者存储过程时,会创建执行计划并存储在SQLServer的过程缓存内存中。在很多时候,我们会执行一些简单的程序,仅仅执行一次,而为这些查询创建存储过程是非常浪费内存资源的。由于内存不足,可能会导致你的缓存溢出,从而影响性能。在2005之前,这是一个大问题,为了纠正这个问题。微软在SQLServer 2008中引入了对即时查询负载的优化功能。这个功能在2012也依旧可用。是基于实例级别的。

        很多开发人员直接在生产环境运行和测试查询,如果没有得到期望的结果,会更改查询然后再次执行,这会对过程缓存造成很大压力。所以尽量不要这样做。

 

准备工作:

在开始之前,在测试服务器清空缓存,但是切记不要在生产环境这样做:

1、 先看看有多少数据保存在缓存中:


SELECT  CP.usecounts AS CountOfQueryExecution ,
        CP.cacheobjtype AS CacheObjectType ,
        CP.objtype AS ObjectType ,
        ST.text AS QueryText
FROM    sys.dm_exec_cached_plans AS CP
        CROSS APPLY sys.dm_exec_sql_text(plan_handle) AS ST
WHERE   CP.usecounts > 0
GO
Copy after login
Copy after login



结果如下:


第十七章配置SQLServer(3)配置“对即时负载的优化”

2、 清空缓存和缓冲池:

DBCC FREEPROCCACHE 
GO
Copy after login
Copy after login


3、 如果想检查是否清空成功,可以再次执行步骤1中的语句: 

 第十七章配置SQLServer(3)配置“对即时负载的优化”

步骤:

1、 执行下面语句:


USE AdventureWorks
GO
SELECT  *
FROM    Sales.SalesOrderDetail
WHERE   SalesOrderDetailID = 43659
GO
Copy after login
Copy after login
Copy after login


2、 检查在运行了上面语句后是否有计划缓存,再次执行之前查询计划缓存的语句: 

SELECT  CP.usecounts AS CountOfQueryExecution ,
        CP.cacheobjtype AS CacheObjectType ,
        CP.objtype AS ObjectType ,
        ST.text AS QueryText
FROM    sys.dm_exec_cached_plans AS CP
        CROSS APPLY sys.dm_exec_sql_text(plan_handle) AS ST
WHERE   CP.usecounts > 0
GO
Copy after login
Copy after login



3、 下面是结果,当然,也可以在where条件中用like来减少查找的数据量:也可以使用ctrl+alt+a来开启活动监视器来查找运行时间长的查询。

第十七章配置SQLServer(3)配置“对即时负载的优化”

4、 现在来把Optimize for Ad hoc Workloads设为1:

 EXEC sp_configure 'optimize for ad hoc workloads', 1
RECONFIGURE
GO
Copy after login



5、 然后再次清空缓存: 

DBCC FREEPROCCACHE 
GO
Copy after login
Copy after login


6、 再次执行语句:


USE AdventureWorks
GO
SELECT  *
FROM    Sales.SalesOrderDetail
WHERE   SalesOrderDetailID = 43659
GO
Copy after login
Copy after login
Copy after login


7、 可以执行下面的语句检查是否有新的缓存进入: 

SELECT  CP.usecounts AS CountOfQueryExecution ,
        CP.cacheobjtype AS CacheObjectType ,
        CP.objtype AS ObjectType ,
        ST.text AS QueryText
FROM    sys.dm_exec_cached_plans AS CP
        CROSS APPLY sys.dm_exec_sql_text(plan_handle) AS ST
WHERE   CP.usecounts > 0
        AND ST.text LIKE '%SELECT  *  FROM    Sales.SalesOrderDetail  WHERE   SalesOrderDetailID = 43659  %'
        AND CP.cacheobjtype = 'Compiled Plan'
GO
Copy after login
Copy after login



8、 你会发现里面没有数据,现在再次执行下面语句: 


USE AdventureWorks
GO
SELECT  *
FROM    Sales.SalesOrderDetail
WHERE   SalesOrderDetailID = 43659
GO
Copy after login
Copy after login
Copy after login


9、 使用以下查询检查: 


SELECT  CP.usecounts AS CountOfQueryExecution ,
        CP.cacheobjtype AS CacheObjectType ,
        CP.objtype AS ObjectType ,
        ST.text AS QueryText
FROM    sys.dm_exec_cached_plans AS CP
        CROSS APPLY sys.dm_exec_sql_text(plan_handle) AS ST
WHERE   CP.usecounts > 0
        AND ST.text LIKE '%SELECT  *  FROM    Sales.SalesOrderDetail  WHERE   SalesOrderDetailID = 43659  %'
        AND CP.cacheobjtype = 'Compiled Plan'
GO
Copy after login
Copy after login



10、这次就出现了下面的截图: 

 第十七章配置SQLServer(3)配置“对即时负载的优化”

 

分析:

        当新查询执行时,query_hash值会在内存中生成,而不是整个执行计划,当相同的查询第二次执行的时候,SQLServer会查找是否已经存在这个query_hash,如果不存在,执行计划将保存在缓存中。这样就使得仅执行一次的查询将不会保存执行计划到缓存中。所以强烈建议打开这个配置。这个配置不造成任何负面影响,但是可以节省计划缓存的空间。

        一般情况下,当你执行查询,将会产生执行计划并保存在过程缓存中,所以当你执行步骤1的查询是,会看到服务器有很多计划缓存,但是当执行第六步后的查询是,就发现没有。对于即席查询,如果只执行一次,何必需要缓存呢?

        有些系统的计划缓存达到GB以上,开启后可能减少一半空间。另外,如果你好奇即席查询占用了多少空间,可以使用下面的语句:

SELECT  SUM(size_in_bytes) AS TotalByteConsumedByAdHoc
FROM    sys.dm_exec_cached_plans
WHERE   objtype = 'Adhoc'
        AND usecounts = 1
Copy after login


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 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
1664
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
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.

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

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.

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

How to change sqlserver English installation to Chinese How to change sqlserver English installation to Chinese Apr 05, 2024 pm 10:21 PM

SQL Server English installation can be changed to Chinese by following the following steps: download the corresponding language pack; stop the SQL Server service; install the language pack; change the instance language; change the user interface language; restart the application.

What should I do if sqlserver cannot be deleted and cannot be reinstalled? What should I do if sqlserver cannot be deleted and cannot be reinstalled? Apr 05, 2024 pm 11:30 PM

The problem that SQL Server cannot be reinstalled due to incomplete deletion can be solved by following the following steps: manually delete files and registry entries; use SQL Server installation and uninstall tools; use third-party uninstall tools; check Windows Event Viewer; restart the computer; reinstall SQL Server.

See all articles