SQL Server
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
単語検索
|
最終更新
|
ヘルプ
]
開始行:
#contents
SELECT name, physical_name, state_desc
FROM sys.master_files
WHERE database_id = DB_ID(N'tempdb');
GO
USE master;
GO
ALTER DATABASE tempdb
MODIFY FILE (NAME = tempdev, FILENAME = 'D:\SQLServer\te...
GO
ALTER DATABASE tempdb
MODIFY FILE (NAME = templog, FILENAME = 'D:\SQLServer\te...
GO
SELECT name, physical_name, state_desc
FROM sys.master_files
WHERE database_id = DB_ID(N'tempdb');
USE <database>
EXEC sp_change_users_login 'Report'
USE <database>
sp_changeobjectowner OBJECT_NAME, OWNER_NAME
SELECT
migs.avg_total_user_cost * migs.avg_user_impact * (migs....
,migs.avg_total_user_cost
,migs.avg_user_impact
,migs.user_seeks
,migs.user_scans
,mid.statement
,mid.equality_columns
,mid.inequality_columns
,mid.included_columns
FROM sys.dm_db_missing_index_group_stats AS migs
INNER JOIN sys.dm_db_missing_index_groups AS mig
ON (migs.group_handle = mig.index_group_handle)
INNER JOIN sys.dm_db_missing_index_details AS mid
ON (mig.index_handle = mid.index_handle)
WHERE migs.group_handle in
(
SELECT TOP 50 group_handle
FROM sys.dm_db_missing_index_group_stats
ORDER BY avg_total_user_cost * avg_user_impact * (user_s...
)
ORDER BY migs.avg_total_user_cost * migs.avg_user_impact...
select
SCHEMA_NAME(st.schema_id) as schema_name
, OBJECT_NAME(dp.object_id) as object_name
, si.name
, used_page_count
, reserved_page_count
, row_count
, user_seeks
, user_scans
, user_lookups
, user_updates
, last_user_seek
, last_user_scan
, last_user_lookup
from
sys.dm_db_partition_stats dp
left join
sys.indexes si
on
si.object_id = dp.object_id
and
si.index_id = dp.index_id
inner join
sys.tables st
on
st.object_id = dp.object_id
and
SCHEMA_NAME(st.schema_id) <> 'sys'
left join
sys.dm_db_index_usage_stats iu
on
iu.object_id = dp.object_id
and
iu.index_id = dp.index_id
order by
schema_name, object_name, si.name
select
i.name as indes_name
, ob.name as table_name
, col.name as column_name
, ix.id
, ix.indid
, ix.keyno
from
sysindexkeys ix
, sysobjects ob
, syscolumns col
, sysindexes i
where
ix.id = ob.id
and ix.id = col.id
and ix.colid = col.colid
and i.id = ix.id
and i.indid = ix.indid
and ob.xtype in ('U','PK')
order by ob.name,ob.xtype,i.name,ix.id,ix.indid,ix.keyno
BEGIN
DECLARE @tablename VARCHAR(256)
declare @id int
declare @rows int, @datasizeused int, @indexsize...
DECLARE tablelist CURSOR FOR
SELECT Name FROM Sys.Tables ORDER BY Name
SET NOCOUNT ON
CREATE TABLE #table_size (
TABLE_NAME VARCHAR(256)
,ROWS INT
,DATA_SIZE INT
,INDEX_SIZE INT
)
select @pagesize = v.low / 1024 from master..spt...
OPEN tablelist
FETCH NEXT FROM tablelist INTO @tablename
WHILE @@FETCH_STATUS = 0
BEGIN
select @id = id from dbo.sysobjects where id = o...
/* rows */
SELECT @rows = convert(int, rowcnt)
FROM dbo.sysindexes
WHERE indid < 2 and id = @id
/* data */
SELECT @datasizeused =
SUM(CASE WHEN a.type <> 1 THEN a.used_pa...
WHEN p.index_id < 2 THEN...
ELSE 0
END)
FROM sys.indexes as i
JOIN sys.partitions as p ON p.object_id = i.obje...
JOIN sys.allocation_units as a ON a.container_id...
where i.object_id = @id
/* index */
SELECT @indexsizeused =
sum(isnull(sidx.used,0)-isnull(sidx.dpag...
FROM dbo.sysindexes sidx
WHERE sidx.indid < 2 and sidx.id = @id
insert into #table_size
values( @tablename, @rows, @datasizeused* @pages...
FETCH NEXT FROM tablelist INTO @tablename
END
SELECT * FROM #table_size
drop table #table_size
DEALLOCATE tablelist
END
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'Agent XPs', 1;
GO
RECONFIGURE
GO
bcp database_name.dbo.table_name in filename.txt -Uuser ...
use <databasename>
go
SET NOCOUNT ON;
DECLARE @objectid int;
DECLARE @indexid int;
DECLARE @partitioncount bigint;
DECLARE @schemaname nvarchar(130);
DECLARE @objectname nvarchar(130);
DECLARE @indexname nvarchar(130);
DECLARE @partitionnum bigint;
DECLARE @partitions bigint;
DECLARE @frag float;
DECLARE @command nvarchar(4000);
SELECT
object_id AS objectid,
index_id AS indexid,
partition_number AS partitionnum,
avg_fragmentation_in_percent AS frag
INTO #work_to_do
FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL...
WHERE avg_fragmentation_in_percent > 10.0 AND index_id >...
DECLARE partitions CURSOR FOR SELECT * FROM #work_to_do;
OPEN partitions;
WHILE (1=1)
BEGIN;
FETCH NEXT
FROM partitions
INTO @objectid, @indexid, @partitionnum, @frag;
IF @@FETCH_STATUS < 0 BREAK;
SELECT @objectname = QUOTENAME(o.name), @scheman...
FROM sys.objects AS o
JOIN sys.schemas as s ON s.schema_id = o.schema_id
WHERE o.object_id = @objectid;
SELECT @indexname = QUOTENAME(name)
FROM sys.indexes
WHERE object_id = @objectid AND index_id = @ind...
SELECT @partitioncount = count (*)
FROM sys.partitions
WHERE object_id = @objectid AND index_id = @inde...
IF @frag < 30.0
SET @command = N'ALTER INDEX ' + @indexname ...
IF @frag >= 30.0
-- SET @command = N'ALTER INDEX ' + @indexnam...
SET @command = N'ALTER INDEX ' + @indexname ...
IF @partitioncount > 1
SET @command = @command + N' PARTITION=' + C...
EXEC (@command);
PRINT N'Executed(' + CAST (@frag as nvarchar) +'...
END;
CLOSE partitions;
DEALLOCATE partitions;
DROP TABLE #work_to_do;
GO
~
~
終了行:
#contents
SELECT name, physical_name, state_desc
FROM sys.master_files
WHERE database_id = DB_ID(N'tempdb');
GO
USE master;
GO
ALTER DATABASE tempdb
MODIFY FILE (NAME = tempdev, FILENAME = 'D:\SQLServer\te...
GO
ALTER DATABASE tempdb
MODIFY FILE (NAME = templog, FILENAME = 'D:\SQLServer\te...
GO
SELECT name, physical_name, state_desc
FROM sys.master_files
WHERE database_id = DB_ID(N'tempdb');
USE <database>
EXEC sp_change_users_login 'Report'
USE <database>
sp_changeobjectowner OBJECT_NAME, OWNER_NAME
SELECT
migs.avg_total_user_cost * migs.avg_user_impact * (migs....
,migs.avg_total_user_cost
,migs.avg_user_impact
,migs.user_seeks
,migs.user_scans
,mid.statement
,mid.equality_columns
,mid.inequality_columns
,mid.included_columns
FROM sys.dm_db_missing_index_group_stats AS migs
INNER JOIN sys.dm_db_missing_index_groups AS mig
ON (migs.group_handle = mig.index_group_handle)
INNER JOIN sys.dm_db_missing_index_details AS mid
ON (mig.index_handle = mid.index_handle)
WHERE migs.group_handle in
(
SELECT TOP 50 group_handle
FROM sys.dm_db_missing_index_group_stats
ORDER BY avg_total_user_cost * avg_user_impact * (user_s...
)
ORDER BY migs.avg_total_user_cost * migs.avg_user_impact...
select
SCHEMA_NAME(st.schema_id) as schema_name
, OBJECT_NAME(dp.object_id) as object_name
, si.name
, used_page_count
, reserved_page_count
, row_count
, user_seeks
, user_scans
, user_lookups
, user_updates
, last_user_seek
, last_user_scan
, last_user_lookup
from
sys.dm_db_partition_stats dp
left join
sys.indexes si
on
si.object_id = dp.object_id
and
si.index_id = dp.index_id
inner join
sys.tables st
on
st.object_id = dp.object_id
and
SCHEMA_NAME(st.schema_id) <> 'sys'
left join
sys.dm_db_index_usage_stats iu
on
iu.object_id = dp.object_id
and
iu.index_id = dp.index_id
order by
schema_name, object_name, si.name
select
i.name as indes_name
, ob.name as table_name
, col.name as column_name
, ix.id
, ix.indid
, ix.keyno
from
sysindexkeys ix
, sysobjects ob
, syscolumns col
, sysindexes i
where
ix.id = ob.id
and ix.id = col.id
and ix.colid = col.colid
and i.id = ix.id
and i.indid = ix.indid
and ob.xtype in ('U','PK')
order by ob.name,ob.xtype,i.name,ix.id,ix.indid,ix.keyno
BEGIN
DECLARE @tablename VARCHAR(256)
declare @id int
declare @rows int, @datasizeused int, @indexsize...
DECLARE tablelist CURSOR FOR
SELECT Name FROM Sys.Tables ORDER BY Name
SET NOCOUNT ON
CREATE TABLE #table_size (
TABLE_NAME VARCHAR(256)
,ROWS INT
,DATA_SIZE INT
,INDEX_SIZE INT
)
select @pagesize = v.low / 1024 from master..spt...
OPEN tablelist
FETCH NEXT FROM tablelist INTO @tablename
WHILE @@FETCH_STATUS = 0
BEGIN
select @id = id from dbo.sysobjects where id = o...
/* rows */
SELECT @rows = convert(int, rowcnt)
FROM dbo.sysindexes
WHERE indid < 2 and id = @id
/* data */
SELECT @datasizeused =
SUM(CASE WHEN a.type <> 1 THEN a.used_pa...
WHEN p.index_id < 2 THEN...
ELSE 0
END)
FROM sys.indexes as i
JOIN sys.partitions as p ON p.object_id = i.obje...
JOIN sys.allocation_units as a ON a.container_id...
where i.object_id = @id
/* index */
SELECT @indexsizeused =
sum(isnull(sidx.used,0)-isnull(sidx.dpag...
FROM dbo.sysindexes sidx
WHERE sidx.indid < 2 and sidx.id = @id
insert into #table_size
values( @tablename, @rows, @datasizeused* @pages...
FETCH NEXT FROM tablelist INTO @tablename
END
SELECT * FROM #table_size
drop table #table_size
DEALLOCATE tablelist
END
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'Agent XPs', 1;
GO
RECONFIGURE
GO
bcp database_name.dbo.table_name in filename.txt -Uuser ...
use <databasename>
go
SET NOCOUNT ON;
DECLARE @objectid int;
DECLARE @indexid int;
DECLARE @partitioncount bigint;
DECLARE @schemaname nvarchar(130);
DECLARE @objectname nvarchar(130);
DECLARE @indexname nvarchar(130);
DECLARE @partitionnum bigint;
DECLARE @partitions bigint;
DECLARE @frag float;
DECLARE @command nvarchar(4000);
SELECT
object_id AS objectid,
index_id AS indexid,
partition_number AS partitionnum,
avg_fragmentation_in_percent AS frag
INTO #work_to_do
FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL...
WHERE avg_fragmentation_in_percent > 10.0 AND index_id >...
DECLARE partitions CURSOR FOR SELECT * FROM #work_to_do;
OPEN partitions;
WHILE (1=1)
BEGIN;
FETCH NEXT
FROM partitions
INTO @objectid, @indexid, @partitionnum, @frag;
IF @@FETCH_STATUS < 0 BREAK;
SELECT @objectname = QUOTENAME(o.name), @scheman...
FROM sys.objects AS o
JOIN sys.schemas as s ON s.schema_id = o.schema_id
WHERE o.object_id = @objectid;
SELECT @indexname = QUOTENAME(name)
FROM sys.indexes
WHERE object_id = @objectid AND index_id = @ind...
SELECT @partitioncount = count (*)
FROM sys.partitions
WHERE object_id = @objectid AND index_id = @inde...
IF @frag < 30.0
SET @command = N'ALTER INDEX ' + @indexname ...
IF @frag >= 30.0
-- SET @command = N'ALTER INDEX ' + @indexnam...
SET @command = N'ALTER INDEX ' + @indexname ...
IF @partitioncount > 1
SET @command = @command + N' PARTITION=' + C...
EXEC (@command);
PRINT N'Executed(' + CAST (@frag as nvarchar) +'...
END;
CLOSE partitions;
DEALLOCATE partitions;
DROP TABLE #work_to_do;
GO
~
~
ページ名: