我们准备将许多SQL Server迁移到云端。我想找到一种在新虚拟服务器上重新分发数据库的逻辑方法。我想要考虑的一个因素是数据库的“繁忙”。我的想法是从dm_io_virtual_stats DMV获取信息,如下所示,以获得我可以在服务器之间进行比较的数字。本质上,它需要num_of_bytes_read和sum_of_bytes_written的总和除以sample_ms用于了解自上次启动系统以来每个数据库的平均I / O.我在几个服务器上运行此代码,因此除以sample_ms应该使它成为对不同机器的有效比较,I希望。我已经阅读了几篇关于这个DMV的文章,但没有一篇论述这个特定的用例。
BEGIN
declare @DatabaseId as int
declare @FileId as int
declare @DatabaseStart as datetime2
declare @BytesIn bigint
declare @BytesOut as bigint
declare @BytesOnDisk as bigint
create table #FileInfo (
ServerName varchar(128),
DatabaseName varchar(128),
UpTime bigint,
InputBytes float,
OutputBytes float,
BytesOnDisk bigint )
DECLARE FileCursor CURSOR FOR
select database_id, file_id
from sys.master_files
open FileCursor
FETCH NEXT FROM FileCursor into @DatabaseId, @FileId
WHILE @@FETCH_STATUS = 0
BEGIN
select @BytesIn = num_of_bytes_read,
@BytesOut = num_of_bytes_written,
@BytesOnDisk = size_on_disk_bytes,
@DatabaseStart = sqlserver_start_time
from sys.dm_io_virtual_file_stats(@DatabaseId, @FileId),
sys.dm_os_sys_info
insert into #FileInfo values ( @@SERVERNAME,
db_name(@DatabaseId),
datediff(second, @DatabaseStart, getdate()),
@Bytesin,
@BytesOut,
@BytesOnDisk )
FETCH NEXT FROM FileCursor into @DatabaseId, @FileId
END;
CLOSE FileCursor
DEALLOCATE FileCursor
select ServerName,
DatabaseName,
(SUM((InputBytes + OutputBytes) / UpTime))[AvgBytesPerSec],
SUM(BytesOnDisk)[DatabaseSize]
from #FileInfo
where UpTime > 0
group by ServerName, DatabaseName
order by ServerName, DatabaseName
Drop table #FileInfo
END
GO