tsql查询 - 在tempdb中占用大量空间

时间:2015-12-07 16:10:55

标签: sql-server

如何在SQL 2000版本中查找哪个查询在临时数据库中占用更多空间。我不想使用SQL分析器,因为磁盘上没有太多空间。

问题是它的2000版本。没有信息可以使用dmv来提取。

如何跟踪1-2哪个查询与哪个数据库之间的tempdb大小增长,以及可以在tempdb下分配页面的通知,它已满。

1 个答案:

答案 0 :(得分: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

希望这有帮助!