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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - MaxIE

jQuery选择器和选取方法 为什么Android的图片质量会比iPhone的差? 百度地图计算两坐标点之间距离计算 - MaxIE .net下BerkeleyDB操作封装C#版(附单元测试) SSD在SQLServer中的应用 Speech两种使用方法 让.net程序自动运行在管理员权限下 “请求的操作无法在使用用户映射区域打开的文件上执行”问题处理 C#随机字符串随机性不足的解決方式(随机数重复) json.net处理复杂json 未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序 解决办法 excel中根据单元格背景颜色进行数据筛选(excel2003实现方法) 跨平台加密版 SQLite 3 - wxSQLite3 数据库sql2000错误8908及处理 jQuery2011年年度最佳插件 jQ中文API离线版下载(适用版本1.4.4,1.5,1.5.1,1.5.2,1.6,1.6.1,1.6.2) 方便的CSS和jQuery下拉菜单解决方案 sql2000无法执行查询及未找到提供程序解决办法 - MaxIE SQL Server优化SELECT语句方法
MS SQL SERVER索引优化相关查询
MaxIE · 2014-01-26 · via 博客园 - MaxIE

查找缺失索引

SELECT user_seeks * avg_total_user_cost * ( avg_user_impact * 0.01 ) AS [index_advantage] ,

migs.last_user_seek ,

mid.[statement] AS [Database.Schema.Table] ,

mid.equality_columns ,

mid.inequality_columns ,

mid.included_columns ,

migs.unique_compiles ,

migs.user_seeks ,

migs.avg_total_user_cost ,

migs.avg_user_impact

FROM sys.dm_db_missing_index_group_stats AS migs WITH ( NOLOCK )

INNER JOIN sys.dm_db_missing_index_groups AS mig WITH ( NOLOCK ) ON migs.group_handle = mig.index_group_handle

INNER JOIN sys.dm_db_missing_index_details AS mid WITH ( NOLOCK ) ON mig.index_handle = mid.index_handle

WHERE mid.database_id = DB_ID()

ORDER BY index_advantage DESC

后续说明:

具有较高的 index_advantage 的索引那些 SQL 服务器认为会产生最大的积极影响,减少工作量,基于查询的成本和预期他们会使用索引的次数减少。

查看现有索引的使用情况 

  SELECT OBJECT_NAME(s.[object_id]) AS [ObjectName] ,

  i.name AS [IndexName] , i.index_id ,

  user_seeks + user_scans + user_lookups AS [Reads] ,

  user_updates AS [Writes] ,

  i.type_desc AS [IndexType] ,

  i.fill_factor AS [FillFactor]

  FROM sys.dm_db_index_usage_stats AS s

  INNER JOIN sys.indexes AS i ON s.[object_id] = i.[object_id]

  WHERE OBJECTPROPERTY(s.[object_id], 'IsUserTable') = 1

  AND i.index_id = s.index_id

  AND s.database_id = DB_ID()

  ORDER BY OBJECT_NAME(s.[object_id]) ,

  writes DESC ,

  reads DESC ;

这是一个有用的查询,为更好地了解数据库的工作负荷。它可以帮助您确定某个特定的索引的波动性和写入数据的读取的比率。这可以帮助您改进和优化您的索引策略。例如,如果您有一个表,是相当静态 (很少写入任何索引),你可能会更有信心有关添加更多的索引在你失踪的索引查询中列中。

如果您使用的是 SQL Server 2008 企业版,此查询可以帮助您决定是否会启用数据压缩 (页或行) 的好主意。具有很少写活动的索引很可能是更合适数据压缩比波动性更大的索引。

查询未使用的索引 

SELECT OBJECT_NAME(i.[object_id]) AS [Table Name] ,

i.name

FROM sys.indexes AS i    

INNER JOIN sys.objects AS o ON i.[object_id] = o.[object_id]

WHERE i.index_id NOT IN

(

 SELECT s.index_id

 FROM sys.dm_db_index_usage_stats AS s    

 WHERE s.[object_id] = i.[object_id]

 AND i.index_id = s.index_id

 AND database_id = DB_ID() 

AND

 (

 last_user_seek>='@DateTime' or  

 last_user_scan>='@DateTime' or  

 last_system_seek>='@DateTime' or

 last_system_scan>='@DateTime'   

 )

)

AND o.[type] = 'U'

ORDER BY OBJECT_NAME(i.[object_id]) ASC

查询当前数据库中使用较少的索引或者写入次数大于读取次数的索引 

SELECT OBJECT_NAME(s.[object_id]) AS [Table Name] ,

i.name AS [Index Name] ,

i.index_id ,

user_updates AS [Total Writes] ,

user_seeks + user_scans + user_lookups AS [Total Reads] ,

user_updates - ( user_seeks + user_scans + user_lookups ) AS [Difference]

FROM sys.dm_db_index_usage_stats AS s WITH ( NOLOCK )

INNER JOIN sys.indexes AS i WITH ( NOLOCK ) ON s.[object_id] = i.[object_id] AND i.index_id = s.index_id

WHERE

OBJECTPROPERTY(s.[object_id], 'IsUserTable') = 1

AND s.database_id = DB_ID()

AND user_updates > (user_seeks + user_scans + user_lookups )

AND i.index_id > 1

ORDER BY [Difference] DESC ,

[Total Writes] DESC ,

[Total Reads] ASC ;