SNAHi
任何人都请在sql中建议查询未使用的表。
我有一个遗留应用程序从coldfusion迁移到.net。但现在很多表都没有使用
从数据库中查找所有未使用对象的最佳方法是什么。 (sql 2005)
感谢 SNA
答案 0 :(得分:4)
在SQL Server中,实际表数据 IS 是聚簇索引。在SQL Server 2005及更高版本的动态管理视图(DMV)上使用此查询,您可以找到未使用的索引 - 如果发现任何聚簇索引(index_id = 1)在较长时间内未使用,则表未被使用了:
DECLARE @dbid INT
SELECT @dbid = DB_ID(DB_NAME())
SELECT
OBJECTNAME = OBJECT_NAME(I.OBJECT_ID),
INDEXNAME = I.NAME,
I.INDEX_ID
FROM
SYS.INDEXES I
JOIN
SYS.OBJECTS O ON I.OBJECT_ID = O.OBJECT_ID
WHERE
OBJECTPROPERTY(O.OBJECT_ID, 'IsUserTable') = 1
AND I.INDEX_ID NOT IN
(SELECT S.INDEX_ID
FROM SYS.DM_DB_INDEX_USAGE_STATS S
WHERE S.OBJECT_ID = I.OBJECT_ID
AND I.INDEX_ID = S.INDEX_ID
AND DATABASE_ID = @dbid)
ORDER BY
OBJECTNAME,
I.INDEX_ID,
INDEXNAME ASC
另一种选择是,如果您怀疑某个表未被使用,则暂时重命名该表,然后查看您的应用是否仍按预期工作。如果他们这样做的话30天左右,你很确定你不再需要那张桌了。
马克
答案 1 :(得分:1)
- 查询以查找任何存储过程,函数和视图未使用的表 - 使用SQL 2005系统表,依赖项的所有编程对象,以及一个和唯一的查询:
select tables.name, progr.name
from sys.objects tables (nolock)
left join sys.syscomments comm (nolock) on comm.text like '%' + tables.name +'%'
left join sys.objects progr (nolock) on progr.object_id = comm.id and progr.type in ('P', 'FN', 'TR', 'V' )
where tables.type = 'U'
and comm.id is null
答案 2 :(得分:0)
这是我写的一个查询,用于查找任何商店程序未使用的表格。 .................................................. ....................................
Declare @tablename nvarchar(40)
Declare tablecursor cursor for
Select name from sysobjects where xtype = 'U'
DECLARE @sqlCommand nvarchar(1000)
declare @rowCount int
DECLARE @searchstring varchar(50)
DECLARE @ParmDefinition nvarchar(500);
create table #temp
(
UnusedTables nvarchar(40)
)
open tablecursor
fetch next from tablecursor into @tablename
while @@fetch_status = 0
begin
set @searchstring='p'
SET @sqlCommand = N'SELECT @rows = count(o.name) from sysobjects o ,
syscomments c where o.type='+char(39)+@searchstring + char(39)+' and
o.id=c.id and c.text like '+ char(39)+'%' + @tablename +'%'+char(39);
SET @ParmDefinition = N'@rows int OUTPUT';
EXECUTE sp_executesql @sqlCommand, @ParmDefinition,@rows=@rowCount OUTPUT;
if @rowCount = 0
begin
insert into #temp values (@tablename)
end
fetch next from tablecursor into @tablename
end
close tablecursor
deallocate tablecursor
select UnusedTables from #temp
drop table #temp
感谢 SA
答案 3 :(得分:0)
试试下面的方法
DECLARE @TableNameTemp TABLE
(
id INT IDENTITY (1, 1),
tablename VARCHAR(1000)
)
INSERT INTO @TableNameTemp
SELECT table_name
FROM information_schema.tables
WHERE table_type = 'BASE TABLE'
ORDER BY table_name
DECLARE @CursorTestID INT = 1;
DECLARE @TotalCount INT = (SELECT Count(1)
FROM @TableNameTemp)
DECLARE @FinalResult TABLE
(
unsedtables VARCHAR(max)
)
DECLARE @TemExecInsert TABLE
(
testvalue VARCHAR(max),
type VARCHAR(max)
)
DECLARE @TableaName VARCHAR(max) = ''
WHILE @CursorTestID <= @TotalCount
BEGIN
DELETE FROM @TemExecInsert
SET @TableaName = (SELECT tablename
FROM @TableNameTemp
WHERE id = @CursorTestID)
INSERT INTO @TemExecInsert
EXEC Sp_depends
@objname = @TableaName
SET @CursorTestID = @CursorTestID + 1
IF ( (SELECT Count(1)
FROM @TemExecInsert) = 0 )
BEGIN
INSERT INTO @FinalResult
VALUES (@TableaName)
END
END
SELECT *
FROM @FinalResult
PS:抱歉,我不确定如何使答案成形。希望这有帮助