Home > Database > Mysql Tutorial > 重建当前数据库中的所有索引

重建当前数据库中的所有索引

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-07 14:58:13
Original
1257 people have browsed it

重建当前数据库中的所有索引 无 DECLARE @sql nvarchar(500) DECLARE RebuildAllTableIndexes CURSORREAD_ONLYFOR SELECT QUOTENAME(SCHEMA_NAME(tbl.schema_id)) + '.' + QUOTENAME(tbl.NAME) tb_name,QUOTENAME(i.NAME) idx_nameFROM sys.tables AS tblINNE

重建当前数据库中的所有索引
DECLARE @sql nvarchar(500)
 
DECLARE RebuildAllTableIndexes CURSOR
READ_ONLY
FOR 
SELECT QUOTENAME(SCHEMA_NAME(tbl.schema_id)) + '.' + QUOTENAME(tbl.NAME) tb_name,
	QUOTENAME(i.NAME) idx_name
FROM sys.tables AS tbl
INNER JOIN sys.indexes AS i ON (
		i.index_id > 0
		AND i.is_hypothetical = 0
		)
	AND (i.object_id = tbl.object_id)
 
DECLARE @tb_name nvarchar(255), @idx_name nvarchar(255)
OPEN RebuildAllTableIndexes
 
FETCH NEXT FROM RebuildAllTableIndexes INTO @tb_name, @idx_name
WHILE (@@fetch_status <> -1)
BEGIN
	IF (@@fetch_status <> -2)
	BEGIN
		SET @sql = N'ALTER INDEX ' + @idx_name + N' ON ' + @tb_name + N' REBUILD PARTITION = ALL WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90)'
		EXEC sp_executesql @sql
	END
	FETCH NEXT FROM RebuildAllTableIndexes INTO @tb_name, @idx_name
END
 
CLOSE RebuildAllTableIndexes
DEALLOCATE RebuildAllTableIndexes
GO
Copy after login
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template