我必須編寫一個SQL腳本來比較SQL Server上兩個表的索引之間的差異。我如何通過SQL查詢獲得表上索引的結構?如何編寫查詢來比較兩個SQL Server表上的索引?
4
A
回答
4
如果兩個數據庫位於同一臺服務器上,則可以執行一系列外部連接兩個數據庫上的sys.indexes和sys.index_columns表的查詢。您還需要查看sys.index_columns以檢查列是否相同(也檢查相同的訂單 - 這將影響查詢計劃)。
如果兩個數據庫都位於不同的服務器上,則需要將sys.indexes和sys.index_columns的內容複製到其他服務器上,並對您的表的副本執行類似的查詢。
這樣的查詢的例子可能看起來像(用於替代FOO適當的數據庫和BAR,如果你想看看各個表添加適當的過濾器,以個人查詢):
select *
from
(select s1.name as SchemaName
,t1.name as TableName
,c1.name as ColumnName
,i1.name as IndexName
,i1.index_id
,c1.column_id
,c1.system_type_id
,c1.user_type_id
,ty1.name as ColumnType
,c1.collation_name -- Note this is nullable
,c1.is_nullable
,c1.max_length
,c1.[precision]
,c1.scale
,ic1.index_column_id
,ic1.key_ordinal
,ic1.partition_ordinal
,ic1.is_descending_key
,ic1.is_included_column
from [FOO].sys.schemas s1
join [FOO].sys.tables t1
on t1.schema_id = s1.schema_id
join [FOO].sys.columns c1
on t1.object_id = c1.object_id
join [FOO].sys.types ty1
on ty1.system_type_id = c1.system_type_id
and ty1.user_type_id = c1.user_type_id
join [FOO].sys.index_columns ic1
on ic1.object_id = c1.object_id
and ic1.column_id = c1.column_id
join [FOO].sys.indexes i1
on i1.object_id = ic1.object_id
and i1.index_id = ic1.index_id) r1
full outer join
(select s1.name as SchemaName
,t1.name as TableName
,c1.name as ColumnName
,i1.name as IndexName
,i1.index_id
,c1.column_id
,c1.system_type_id
,c1.user_type_id
,ty1.name as ColumnType
,c1.collation_name -- Note this is nullable
,c1.is_nullable
,c1.max_length
,c1.[precision]
,c1.scale
,ic1.index_column_id
,ic1.key_ordinal
,ic1.partition_ordinal
,ic1.is_descending_key
,ic1.is_included_column
from [BAR].sys.schemas s1
join [BAR].sys.tables t1
on t1.schema_id = s1.schema_id
join [BAR].sys.columns c1
on t1.object_id = c1.object_id
join [BAR].sys.types ty1
on ty1.system_type_id = c1.system_type_id
and ty1.user_type_id = c1.user_type_id
join [BAR].sys.index_columns ic1
on ic1.object_id = c1.object_id
and ic1.column_id = c1.column_id
join [BAR].sys.indexes i1
on i1.object_id = ic1.object_id
and i1.index_id = ic1.index_id) r2
on r1.SchemaName = r2.SchemaName
and r1.TableName = r2.TableName
and r1.ColumnName = r2.ColumnName
and r1.IndexName = r2.IndexName
0
如果您只想要的是可以使用此procedure生成腳本的結構。
然後,您可以比較兩個數據庫之間的文件。
1
sys.indexes包含有關給定數據庫上索引的所有信息。
相關問題
- 1. 比較查詢比較兩個SQL Server表
- 2. 如何編寫查詢查詢來比較Express和MongoDB中的兩個字段?
- 3. SQL Server:如何比較兩個表
- 4. SQL Server:比較兩個表,兩列
- 5. 如何編寫查詢來查找在SQL Server表
- 6. SQL查詢,比較兩個陣列
- 7. 在SQL Server中比較兩個表
- 8. 如何比較兩個查詢?
- 9. 如何編寫此SQL Server查詢?
- 10. SQL Server:比較索引性能
- 11. SQL Server:如何比較存儲在變量中的兩個查詢的結果
- 12. 如何比較兩個表的列,SQL Server?
- 13. Extbase查詢來比較同一個表中的兩個字段
- 14. 如何在SQL Server查詢中連接兩個以上的表
- 15. 比較列表SQL查詢
- 16. 查詢和比較兩個表格
- 17. 比較兩個表來檢查差異
- 18. 比較兩個SP的性能SQL Server
- 19. SQL查詢 - 比較不同項目上的兩個項目 - REVISITED
- 20. SQL Server表上的索引
- 21. 如何編寫T-SQL來比較和複製數據?
- 22. sql比較兩個表格
- 23. SQL如何編寫查詢來檢索最新的帖子
- 24. 如何使用3個表上的連接編寫SQL查詢?
- 25. 如何編寫SQL查詢?
- 26. 如何比較兩個列表並返回值和索引
- 27. SQL查詢比較
- 28. Mysql索引與mysql查詢比較
- 29. 在MS Access SQL查詢中比較兩個表格
- 30. SQL查詢比較並根據兩個表獲取信息
我不明白 - 你想找到你在SQL Server數據庫中有哪些不同的索引?或者什麼數據不同?你需要更精確地詢問.... – 2010-08-18 06:04:17
我的理解是你想比較來自不同的SQL服務器或數據庫的兩個表? – ppshein 2010-08-18 06:38:34
繼續併爲SO的贊助商產品彈出(http://stackoverflow.com/questions/tagged/sql-server)。 *** SQL比較***(http://www.red-gate.com/products/sql_bundles/SQL_Comparison_Bundle.htm)真的會爲這些類型的任務節省金錢和時間。 – 2010-08-18 08:29:17