sqlserver大資料歸檔

2021-08-26 21:09:44 字數 1230 閱讀 1496

昨天做了個日常大資料歸檔,歸檔700w資料,表字段130左右,字段比較多,分享下!

----先禁用表的index

1.先獲取需要禁用的索引

declare @tname varchar(100)

set @tname='orders'

select 'alter index '+' '+c.indexname+' '+'on'+' '+@tname+' '+'disable'

from

(select * from

(select

object_name(i.object_id) as tablename,

i.name as indexname,

i.index_id as indexid,

8 * sum(a.used_pages)/1024 as 'indexsize(mb)'

from sys.indexes as i

join sys.partitions as p on p.object_id = i.object_id and p.index_id = i.index_id

join sys.allocation_units as a on a.container_id = p.partition_id

group by i.object_id,i.index_id,i.name

)awhere a.tablename=@tname

--order by [indexsize(mb)] desc

)cgo

--2.禁止上面語句獲得索引,但是主鍵和clustered index別禁用,切記!

----刪除資料

dbcc dropcleanbuffers

dbcc freeproccache

goset nocount on

begin transaction

while 1=1

begin

delete top(20000) from dbo.orders with(tablock)

where ordertime <'2010-1-1'

if @@rowcount<20000

break

end

commit

go----索引重建

alter index all on orders rebuild

go基本上很短時間搞定,為了效能,需要完成索引rebuild和統計資訊更新!

SQL Server 資料歸檔方案

sql server 資料歸檔方案 方案一 方案介紹 bcp匯出資料到本地目錄目錄後,遍歷目錄檔案bcp匯入到臨時表,再迴圈刪除源表資料。通過insert into left join 通過主鍵關聯臨時表和歸檔表排除存在的資料。或通過2008及後續版本的merge語句,不存在插入,存在更新 方案優缺...

SQL Server 資料歸檔方案

本文旨在從資料庫管理方面,提供將sql server大資料表歸檔的解決方案。可以作為新業務上線時進行方案設計的參考。方案一 方案介紹 bcp匯出資料到本地目錄目錄後,遍歷目錄檔案bcp匯入到臨時表,再迴圈刪除源表資料。通過insert into left join 通過主鍵關聯臨時表和歸檔表排除存在...

SQL Server 快速刪除 歸檔資料方法小結

最近遇到了清理歷史資料的需求,整理一下不同場景及對應處理方法 這是最簡單的,truncate drop table即可 這種情況是,會不斷往表裡插入新資料但是並不會去查詢,一般是系統異常時開發手動去查。這種情況可以停業務將原表重新命名為bak表,再按原有表結構建立乙個新錶讓系統插入。bak表根據業務...