SQL查询操作以及如何在执行查询时管理内存

时间:2018-02-16 13:40:36

标签: sql sql-server sql-server-2008 sql-server-2008-r2

写了一个Sql Query,它列出了表名和列名匹配的列数据。它正在从数据库中搜索所有表列(表类型基本表和列数据类型为varchar或nvarchar)。

静: 数据库大小:37 GB / 150 GB / 650 GB(具有不同的数据库) 表数:467(列数:1220)

Sql Query:

DECLARE
    @search_string  VARCHAR(100),
     @sql_string     nvarchar(max)


SET @search_string = 'XXXXXXXX'--string only 


SELECT @sql_string='IF EXISTS (SELECT '+ISC.COLUMN_NAME+' 
FROM ' + QUOTENAME(IST.TABLE_SCHEMA) + '.' + QUOTENAME(IST.TABLE_NAME)  
+ 'with (nolock) WHERE ' + QUOTENAME(ISC.COLUMN_NAME) + 
' ='''+@search_string+''') PRINT ''' + QUOTENAME(IST.TABLE_SCHEMA) + '.' 
+ QUOTENAME(IST.TABLE_NAME)  + QUOTENAME(ISC.COLUMN_NAME) + 
'''' FROM INFORMATION_SCHEMA.TABLES IST with (nolock) 
inner join INFORMATION_SCHEMA.COLUMNS ISC with (nolock) 
on IST.TABLE_NAME= ISC.TABLE_NAME
WHERE IST.TABLE_TYPE = 'BASE TABLE' 
and IST.TABLE_SCHEMA='dbo' 
and ISC.COLLATION_NAME IS NOT NULL 
and (ISC.DATA_TYPE='nvarchar' or ISC.DATA_TYPE='varchar' )


while @sql_string is not null 
begin

 EXECUTE(@sql_string)

end

Wrote same Query by using cursor as well

DECLARE
    @search_string  VARCHAR(100),
    @table_name     SYSNAME,
    @table_schema   SYSNAME,
    @column_name    SYSNAME,
    @table_type     SysName,
    @sql_string     VARCHAR(2000)    


SET @search_string = 'xxxxx@hotmail.com' -- custom/agent data

DECLARE tables_cur CURSOR FOR SELECT TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE FROM INFORMATION_SCHEMA.TABLES with (nolock) WHERE TABLE_TYPE = 'BASE TABLE'

OPEN tables_cur

FETCH NEXT FROM tables_cur INTO @table_schema, @table_name,@table_type

WHILE (@@FETCH_STATUS = 0)
BEGIN
    DECLARE columns_cur CURSOR FOR SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS with (nolock)  WHERE TABLE_SCHEMA = @table_schema AND TABLE_NAME = @table_name AND COLLATION_NAME IS NOT NULL and (DATA_TYPE='nvarchar' or DATA_TYPE='varchar' )  -- Only strings have this and they always have it

    OPEN columns_cur

    FETCH NEXT FROM columns_cur INTO @column_name
    WHILE (@@FETCH_STATUS = 0)
    BEGIN
        SET @sql_string = 'IF EXISTS (SELECT '+QUOTENAME(@column_name)+' FROM ' + QUOTENAME(@table_schema) + '.' + QUOTENAME(@table_name)  + 'with (nolock) WHERE ' + QUOTENAME(@column_name) + ' ='''+@search_string+''') PRINT ''' + QUOTENAME(@table_schema) + '.' + QUOTENAME(@table_name) + ', '+ QUOTENAME(@table_type) +',' + QUOTENAME(@column_name) + ''''
        --select @sql_string
        EXECUTE(@sql_string)

        FETCH NEXT FROM columns_cur INTO @column_name
    END

    CLOSE columns_cur

    DEALLOCATE columns_cur

    FETCH NEXT FROM tables_cur INTO @table_schema, @table_name, @table_type
END

CLOSE tables_cur

DEALLOCATE tables_cur

ISSUE: 1.在37 GB数据库执行时花了太多时间(大约3个小时)。 2.某些时候面临“System.OutOfMemoryException”异常,执行中间。

意见: 1.它需要太多时间才能完成执行。 2. Web应用程序被挂起/在后端执行查询时获取sql超时异常。

还有其他方法可以缩短执行时间吗? 如何在不增加内存的情况下解决异常?

任何建议都会有很大的帮助

由于

0 个答案:

没有答案