Sql Server 索引使用情况及优化的相关Sql语句分享

2020-07-10 08:14:05易采站长站整理

IF @frag >= 30.0
SET @command = N’ALTER INDEX ‘ + @indexname + N’ ON ‘ + @schemaname + N’.’ + @objectname + N’ REBUILD’;
IF @partitioncount > 1
SET @command = @command + N’ PARTITION=’ + CAST(@partitionnum AS nvarchar(10));
EXEC (@command);
PRINT N’Executed: ‘ + @command;
END;
— Close and deallocate the cursor.
CLOSE partitions;
DEALLOCATE partitions;
— Drop the temporary table.
DROP TABLE #work_to_do;
GO
— 查看当前数据库索引的使用率
— 非常的有用
SELECT
object_name(object_id) as table_name,
(
select name
from sys.indexes
where object_id = stats.object_id and index_id = stats.index_id
) as index_name,
*
FROM sys.dm_db_index_usage_stats as stats
WHERE database_id = DB_ID()
order by table_name
— 指定表的索引使用情况
declare @table as nvarchar(100)
set @table = ‘t_name’;
SELECT
(
select name
from sys.indexes
where object_id = stats.object_id and index_id = stats.index_id
) as index_name,
*
FROM sys.dm_db_index_usage_stats as stats
where object_id = object_id(@table)
order by user_seeks, user_scans, user_lookups asc
–End Index 分析优化的相关 Sql

您可能感兴趣的文章:数据库SQL语句优化总结(收藏)SQL语句性能优化(续)如何优化SQL语句(全)sql语句优化之SQL Server(详细整理)SQL Server中的SQL语句优化与效率问题MySQL SQL语句优化的10条建议如何优化SQL语句的心得浅谈浅谈MySQL中优化sql语句查询常用的30种方法SQL语句优化方法30例(推荐)SQLServer 优化SQL语句 in 和not in的替代方案oracle下一条SQL语句的优化过程(比较详细)SQL SERVER 的SQL语句优化方式小结常用SQL语句优化技巧总结【经典】

相关文章 大家在看