日期:2014-05-17  浏览次数:20522 次

向存在索引的表插入大数据问题
每月需向某表添加约800万条数据,并且该表还需查询及统计。
由于客户对插入及统计都有时间限制。
现在采取的方案包括
1.对该表按月份字段进行分区。(但分区后插入速度会更慢,但查询统计时应该会快些)
2.插入前删除所有索引(drop index ),插入后再创建索引create index。

但现在的问题时,随着插入的数据越来越多(虽然分区了),以后每次的drop index, create index
将会越来越慢吧?

我的想法是,能否只drop 、create某个分区上的索引(因为每月插入的数据,都肯定是往同一个区的)。

或者大家针对大量数据插入,有什么好的解决方案(要求是能尽快完成插入,当然因为表要做查询统计,不可能不建索引)。

谢谢!

------解决方案--------------------
引用:
@yupeigu
如果不能先停用,只能组织好像就无法满足了。呵呵。


大伙对大数据的插入,都是先删索引,再建索引吗?


给你做了一个例子,其实就是,你导入数据,和当前的分区表没有关系。

首先,你再建立一个分区表,结构和原来的表是一样的,然后把新的一个月数据800w导入到这个临时建立的分区表中,

然后,导入后,要建立相应的索引,这个索引和原来的表一样,

最后,切换,也就是switch,就是把刚才导入的数据,所在的分区表的分区,switch到原来表的新分区中。当然,在swith之前,必须要split出一个新的分区,指定使用的下一个文件组是哪个:

基本上是秒级切换,对当前业务没有任何影响:


alter database wc
set single_user
with rollback immediate

drop database wc




--1.创建数据库  
create database wc  
on primary  
(  
    name = wc_data,  
    filename = 'D:\wc_data.mdf'  
)  
log on  
(  
    name = wc_log1,  
    filename = 'd:\wc_log1.ldf'    
),  
(  
    name = wc_log2,  
    filename = 'd:\wc_log2.ldf'  
)  
  
go


use wc
go

--4.创建分区函数,3个值,但是会有4个分区
create partition function wcLeftRange(char(6))  
as range left for values('201309','201310','201311')  


  
--5.创建分区方案,4个文件组  
create partition scheme wcLeftRangeScheme  
as partition wcLeftRange  
to ([primary],[primary],[primary],[primary])



--6.建表,聚集索引,插入数据
create table temp_UserBill
(
BusinessDate char(6), F1 varchar(100), F2 Varchar(100)
)

create clustered index idx_temp_UserBill on temp_UserBill(BusinessDate)


insert into temp_UserBill
select '201309','aa1','bb1' union all
select '201309','aa2','bb2' union all
select '201310','aa1','bb1' union all
select '201310','aa2','bb2' union all
select '201311','aa1','bb1' union all
select '201311','aa2','bb2' union all
select '201312','aa','bb' 

go


--7.要转化为分区表,必须要删除聚集索引
drop index idx_temp_UserBill on temp_UserBill


--8.然后再重新建立一个聚集索引,并且指定:分区方案和分区字段
create clustered index idx_temp_UserBill on temp_UserBill(BusinessDate)
on wcLeftRangeScheme(BusinessDate)  


-