惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
CERT Recently Published Vulnerability Notes
U
Unit 42
T
The Blog of Author Tim Ferriss
H
Hackread – Cybersecurity News, Data Breaches, AI and More
B
Blog RSS Feed
Microsoft Azure Blog
Microsoft Azure Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
S
Securelist
L
Lohrmann on Cybersecurity
Blog — PlanetScale
Blog — PlanetScale
Recorded Future
Recorded Future
D
DataBreaches.Net
Spread Privacy
Spread Privacy
T
Threat Research - Cisco Blogs
I
Intezer
P
Palo Alto Networks Blog
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
I
InfoQ
宝玉的分享
宝玉的分享
Security Latest
Security Latest
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Threatpost
Cisco Talos Blog
Cisco Talos Blog
P
Proofpoint News Feed
博客园 - 司徒正美
H
Hacker News: Front Page
Y
Y Combinator Blog
爱范儿
爱范儿
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
NISL@THU
NISL@THU
月光博客
月光博客
有赞技术团队
有赞技术团队
Cloudbric
Cloudbric
酷 壳 – CoolShell
酷 壳 – CoolShell
G
Google Developers Blog
A
Arctic Wolf
博客园 - 【当耐特】
W
WeLiveSecurity
V
Visual Studio Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
V
V2EX
C
Cyber Attacks, Cyber Crime and Cyber Security
S
SegmentFault 最新的问题
The GitHub Blog
The GitHub Blog
The Cloudflare Blog
Stack Overflow Blog
Stack Overflow Blog

博客园 - 1werwerfer

使用ASP.NET 2.0新增加的SetFocus和MaintainScrollPositionOnPostback增强用户体验 - 1werwerfer - 博客园 ADO.NET中的Transaction asp.net 2.0在使用了mater page的情况下Form defaultbutton无法设置的问题 - 1werwerfer SQL Server 索引结构及其使用(四) SQL Server 索引结构及其使用(三) SQL Server 索引结构及其使用(二) SQL Server 索引结构及其使用(一) 如何作正确的询价--不可忽视的国际贸易中的"贸易条件" - 1werwerfer - 博客园 智猪博弈与企业技术创新决策 为 IIS 创建证书需要安装证书服务吗? MSI文件制作全过程 如何将Windows Server 2003映像安装到Windows Server 2003 RIS服务器 Asp.Net中使用水晶报表 经典正则表达式 使用System.DirectoryServices.Protocols实现对AD的简单操作 备份Exchange server存储组有效减少LOG文件 A Simple Tip on SQL Server Stored Procedure 无盘网络技术详解 如何面对呼叫中心的风险?
如何将SQL Server表驻留内存和检测
1werwerfer · 2004-12-15 · via 博客园 - 1werwerfer

SQL Server数据表驻留内存是SQL Server提供的一项功能,在一般小型系统的开发过程中估计很少会涉及到。这里整理了相关文档资料,演示如何把SQL Server中一个表的所有数据都放入内存中,实现内存数据库,提高实时性。

1, DBCC PINTABLE

Marks a table to be pinned, which means Microsoft SQL Server does not flush the pages for the table from memory.

Syntax

DBCC PINTABLE ( database_id , table_id )

To determine the database ID, use the DB_ID function.

To determine the table ID, use the OBJECT_ID function.

注释

DBCC PINTABLE 不会导致将表读入到内存中。当表中的页由普通的 Transact-SQL 语句读入到高速缓存中时,这些页将标记为内存驻留页。当 SQL Server 需要空间以读入新页时,不会清空内存驻留页。SQL Server 仍然记录对页的更新,并且如有必要,将更新的页写回到磁盘。然而,在使用 DBCC UNPINTABLE 语句使该表不驻留之前,SQL Server 在高速缓存中一直保存可用页的复本。

DBCC PINTABLE 最适用于将小的、经常引用的表保存在内存中。将小表的页一次性读入到内存中,将来对其数据的所有引用都不需要从磁盘读入。

注意  DBCC PINTABLE 可以提供性能改进,但是使用时务必小心。如果驻留大表,则该表在开始时会使用一大部分高速缓存,而不为系统中的其它表保留足够的高速缓存。如果所驻留的表比高速缓存大,则该表会填满整个高速缓存。sysadmin 固定服务器角色的某个成员必须关闭而后重新启动 SQL Server,然后使表不驻留。驻留太多的表和驻留比高速缓存大的表会产生同样的问题。

示例:

Declare @db_id int, @tbl_id int

Use DATABASE_NAME

Set @db_id = DB_ID('DATABASE_NAME')

Set @tbl_id = Object_ID('Department')

DBCC pintable (@db_id, @tbl_id)

可将表Department设置为驻留内存。

Declare @db_id int, @tbl_id int

Use DATABASE_NAME

Set @db_id = DB_ID('DATABASE_NAME')

Set @tbl_id = Object_ID('Department')

DBCC UNpintable (@db_id, @tbl_id)

可将表Department取消设置为驻留内存。

可以使用如下的SQL指令来检测执行情况:

Select ObjectProperty(Object_ID('Department'),'TableIsPinned')

如果返回结果为1:则表示该表已经设置为驻留内存;0:则表示没有设置为驻留内存。

2, SP_TableOption

Sets option values for user-defined tables. sp_tableoption may be used to turn on the text in row feature on tables with text, ntext, or image columns.

Syntax

sp_tableoption [ @TableNamePattern = ] 'table'
    , [ @OptionName = ] 'option_name'
    , [ @OptionValue = ] 'value'

其中,'option_name' 有如下用法:

pintable  -- When disabled (the default), it marks the table as no longer RAM-resident. When enabled, marks the table as RAM-resident. (可将指定的表驻留内存)

另外,table lock on bulk load, insert row lock, text in row等等可选值,因不涉及将表驻留内存,具体用法可以查询SQL Server Books Online.

Value有如下用法:

the option_name is enabled (true, on, or 1) or disabled (false, off, or 0)

示例:

EXEC sp_tableoption 'Department','pintable', 'true'

将数据表Department驻留内存

EXEC sp_tableoption 'Department','pintable', 'false'

取消数据表Department驻留内存

可以使用如下的SQL指令来检测执行情况:

Select ObjectProperty(Object_ID('Department'),'TableIsPinned')

如果返回结果为1:则表示该表已经设置为驻留内存;0:则表示没有设置为驻留内存。

3. Conclusions

将数据表设置为驻留内存时,并没有实际将表读入内存中,直到该表从被检索。因此,可以使用如下SQL指令进一步将数据表Department驻留内存:

Select * From Department

另外,可以使用如下SQL指令方便显示/检测数据库Database中所有设置为驻留内存的表:

SELECT * FROM INFORMATION_SCHEMA.Tables

WHERE TABLE_TYPE = 'BASE TABLE'

          AND OBJECTPROPERTY(object_id(TABLE_NAME), 'TableIsPinned') > 0