Deleting large amounts of rows from SQL 2005 (2008) database tables
2017-11-02 18:08
579 查看
我是搬运工。。。。原文
Each time you perform an insert, update or delete operation on a database table it also affects the indexes for that particular table. If you need to bulk load or delete a mass of data from a highly indexed table, your best choice is to disable all the indexes on a table, perform the insert/delete operation, and then rebuild all the indexes.
Below I will demonstrate a quick way to delete a lot of data from a highly indexed database table in MS SQL 2005 or MS SQL 2008. I used this method to delete 20 million rows from a table on a super active MS SQL database server.
Each time you perform an insert, update or delete operation on a database table it also affects the indexes for that particular table. If you need to bulk load or delete a mass of data from a highly indexed table, your best choice is to disable all the indexes on a table, perform the insert/delete operation, and then rebuild all the indexes.
Below I will demonstrate a quick way to delete a lot of data from a highly indexed database table in MS SQL 2005 or MS SQL 2008. I used this method to delete 20 million rows from a table on a super active MS SQL database server.
-- 1) Identify all existing indexes on a table sp_helpindex [tableName] -- 2) Disable each nonclustered index in the list ALTER INDEX [indexName1] ON [dbo].[tableName] DISABLE GO ALTER INDEX [indexName2] ON [dbo].[tableName] DISABLE GO -- Important: Do not disable the clustered index because it will prevent -- access to table data until you drop or rebuild it. -- 3) Use new SQL 2005 (2008) DELETE TOP syntax to delete dynamically -- declared number of rows in batches in order -- to prevent extensive table locking: Declare @BatchSize int Set @BatchSize = 100000 -- select this number based on your context Declare @RowsAffected int Set @RowsAffected = 1 While (@RowsAffected > 0) begin delete top (@BatchSize) from [tableName] where [your condition] set @RowsAffected = @@rowcount end -- 4) After the above operation completes its execution, rebuild -- all the indexes you disabled in step 2: ALTER INDEX [indexName1] ON [dbo].[tableName] REBUILD GO ALTER INDEX [indexName2] ON [dbo].[tableName] REBUILD GO -- Etc.
相关文章推荐
- 转载:Character data is represented incorrectly when the code page of the client computer differs from the code page of the database in SQL Server 2005
- Stored procedures to implement paging for large tables or queries in SQL Server 2005 and SQL Server 2008
- get all rows count of all tables in a mysql database.
- System tables of a geodatabase in SQL Server
- Finding Total Number Of Rows, Columns In A Database And Tables Sizes
- Display the size of all tables in Sql Server 2005
- How to Downgrade a Database from SQL Server 2005 to SQL Server 2000
- Import a large sql dump file to a MySQL database from command line
- System tables of a geodatabase in SQL Server
- Copy a table from one database to another in SQL Server 2005
- Get all database from SQL Server 2005
- 【原】获取数据库(SQL SERVER 2005)的所有信息 Get all database information from SQL Server 2005 测试通过【想写基于数据库代码生成器可参考】
- 【原】获取数据库(SQL SERVER 2005)的所有信息 Get all database information from SQL Server 2005 测试通过
- Microsoft SQL Server 2008 R2 XML处理 One solution is to increase the number of characters retrieved from the server for XML data.
- Deleting a large number of items from a list in SharePoint
- sql code for deleting a batch of tables
- Migration from SQL server 2005/2008 to 2012
- How to: Send and Receive Large Amounts of Data to and from a Web Service
- SQL SERVER – 2008 – Configure Database Mail – Send Email From SQL Database
- Restore SQL Server 2005 error : "The tail of the log for the database "TEST1" has not been backed up."