2015-12-07 52 views
0

如何查找哪個查詢在SQL 2000版本的臨時數據庫中佔用更多空間。我不想使用SQL分析器,因爲磁盤上沒有太多空間。tsql查詢 - 在tempdb中佔用大量空間

問題是它的2000版本。沒有信息可以使用dmv來拉取。

如何跟蹤1-2哪些查詢以及哪個數據庫正在使tempdb大小增長以儘可能多地增加可以在tempdb下分配頁面的通知,它已滿。

+0

您可以將數據庫複製到有足夠空間的測試環境並在那裏運行分析器?另外,你爲什麼不想使用sql server profiler? – user1666620

+0

不可以,因爲在環境中有許多數據庫和沒有測試服務器 –

+0

其他可能的事情可以完成。什麼是sqltext? –

回答

0

您可以在下面的腳本中創建一個可以由作業執行的proc。或多或少,這是一個模板,可以改變,但你認爲合適。

編輯:添加下面的附加註釋。

--Create temp table for sysprocesses records 
IF OBJECT_ID('tempdb.dbo.#SYSPROC') IS NOT NULL 
    DROP TABLE #SYSPROC; 
BEGIN 
    CREATE TABLE #SYSPROC 
    (
     spid smallint NOT NULL, 
     dbid smallint NOT NULL, 
     blocked smallint NOT NULL, 
     lastwaittype nchar(32) NOT NULL, 
     cpu int NOT NULL, 
     physical_io int NOT NULL, 
     memusage int NOT NULL, 
     login_time datetime NOT NULL, 
     Last_batch datetime NOT NULL, 
     status nchar(30) NOT NULL, 
     cmd nchar(16) NOT NULL, 
     loginame nchar(128) NOT NULL, 
     sql_handle binary(20) NOT NULL, 
     sh_text text NULL, 
     snapshot_dt datetime NULL 
    ) 
END; 


--Insert sysprocesses records into temp #SYSPROC 
INSERT INTO #SYSPROC 
(
    spid, 
    dbid, 
    blocked, 
    lastwaittype, 
    cpu, 
    physical_io, 
    memusage, 
    login_time, 
    Last_batch, 
    status, 
    cmd, 
    loginame, 
    sql_handle, 
    snapshot_dt 
) 
SELECT 
    sp.spid, 
    sp.dbid, 
    sp.blocked, 
    sp.lastwaittype, 
    sp.cpu, 
    sp.physical_io, 
    sp.memusage, 
    sp.login_time, 
    sp.Last_batch, 
    sp.status, 
    sp.cmd, 
    sp.loginame, 
    sp.sql_handle, 
    GETDATE() 
FROM master.dbo.sysprocesses sp 
WHERE sp.spid > 50; 


/* 
Update temp #SYSPROC with sql text. Since CROSS APPLY is not 
available in SQL Server 2000, a loop is required to interate 
through each record. 
*/ 
DECLARE @min_spid smallint = (SELECT MIN(spid) FROM #SYSPROC); 
DECLARE @max_spid smallint = (SELECT MAX(spid) FROM #SYSPROC); 
DECLARE @sql_handle binary(20); 
DECLARE @sql_text varchar(max); 

WHILE @min_spid <= @max_spid 
BEGIN 

    --Set @sql_handle variable to be evaluated by the fn_get_sql function 
    SELECT 
     @sql_handle = S.sql_handle 
    FROM #SYSPROC S 
    WHERE spid = @min_spid 

    --Identify the sql_text for the session by passing the @sql_handle variablet through fn_get_sql 
    SELECT 
     @sql_text = H.text 
    FROM fn_get_sql(@sql_handle) H 

    --Update #SYSPROC with the sql_text. 
    UPDATE S 
    SET sh_text = @sql_text 
    FROM #SYSPROC S 
    WHERE spid = @min_spid 

    SET @min_spid = @min_spid + 1 

END; 


INSERT INTO <SOMETABLE YOU CREATE> 
(
    spid, 
    dbid, 
    blocked, 
    lastwaittype, 
    cpu, 
    physical_io, 
    memusage, 
    login_time, 
    Last_batch, 
    status, 
    cmd, 
    loginame, 
    sql_handle, 
    snapshot_dt 
) 
SELECT 
    spid, 
    dbid, 
    blocked, 
    lastwaittype, 
    cpu, 
    physical_io, 
    memusage, 
    login_time, 
    Last_batch, 
    status, 
    cmd, 
    loginame, 
    sql_handle, 
    snapshot_dt 
FROM #SYSPROC 

希望這會有所幫助!