|
原帖由 〇〇 于 2009-7-3 10:41 发表 ![]()
5分钟内如何插入40万条以上的数据?
用sqlldr
再show一个专用加载程序,把另一个系统的车站表加载到我们的车站表,二者内容格式不完全相同,你怎么用sqlldr加载,而且是增量加载。
每秒5000个,150万/5分钟:
- #include <stdio.h>
- #include <kpapp.h>
- #include <libgen.h>
- T_PkgType T_sta_tpl[]={
- {CH_CHAR,4,"station_code",0,-1},
- {CH_CHAR,11,"station_name"},
- {CH_INT,sizeof(int),"statis_code"},
- {CH_CHAR,4,"spell"},
- {CH_CHAR,4,"sta_code"},
- {CH_CHAR,2,"JM"},
- {CH_DATE,9,"beg_date","YYYYMMDD"},
- {CH_DATE,9,"end_date","YYYYMMDD"},
- {-1,0,0,0}
- };
- typedef struct {
- char station_code[4];
- char station_name[11];
- int statis_code;
- char spell[4];
- char sta_code[4];
- char JM[2];
- char beg_date[9];
- char end_date[9];
- } T_sta_stu;
- int loadfile(T_SQL_Connect *SQL_Connect,char *buf,int buflen)
- {
- char *p,tabn[512];
- DAU _DAU;
- int rows,ret;
- int upd,loss;
- char *tabname;
- int num;
- FILE *ifd;
- SRM srm;
- STATION_stu station;
- T_sta_stu sta;
- ifd=fopen("DF02.bcp","r"); //车站表
- if(!ifd) {
- perror("DF02.bcp");
- return -1;
- }
- ShowLog(5,"loadfile:entry,DF02.bcp opened");
- num=0;
- upd=loss=0;
- SRM_init(&srm,0,&sta,T_sta_tpl);
- DAU_init(&_DAU,SQL_Connect,0,&station,STATION_tpl);
- data_init(&station,STATION_tpl);
- for(rows=0;!ferror(ifd);rows++) {
- fgets(buf,buflen,ifd);
- if(feof(ifd)) break;
- TRIM(buf);
- if(!*buf) {
- rows--;
- continue;
- }
- if(!(++num %10000)) {
- trans_commit(SQL_Connect);
- ShowLog(5,"loadfile:num=%d,rows=%d",num,rows);
- }
- SRM_pkg_dispack(&srm,buf,'\t');
- strlower(sta.spell);
- SRM_copy(&_DAU.srm,&srm,"station_code,station_name,spell,statis_code");
- station.beg_date=rstrfmttojul(sta.beg_date,"YYYYMMDD");
- station.end_date=rstrfmttojul(sta.end_date,"YYYYMMDD");
- ret=DAU_insert(&_DAU,buf);
- if(ret) {
- if(SQL_Connect->Errno==DUPKEY) {
- *buf=0;
- ret=update_by_PK(&_DAU,buf);
- if(ret==1) upd++;
- else {
- ShowLog(1,"update err=%d,%s",
- SQL_Connect->Errno,
- SQL_Connect->ErrMsg
- );
- loss++;
- }
- } else {
- ShowLog(1,"insert:err=%d,%s,buf=%s",
- SQL_Connect->Errno,
- SQL_Connect->ErrMsg,
- buf);
- loss++;
- }
- rows--;
- }
- }
- trans_commit(SQL_Connect);
- DAU_free(&_DAU);
- fclose(ifd);
- ShowLog(2,"loadfile:rows=%d,upd=%d,loss=%d",rows,upd,loss);
- return rows;
- }
复制代码
注意这几句:
//人家的数据加载到人家的结构里
SRM_pkg_dispack(&srm,buf,'\t');
//spell字段变小写
strlower(sta.spell);
//把人家的结构的某些字段拷贝到我的结构
SRM_copy(&_DAU.srm,&srm,"station_code,station_name,spell,statis_code");
//修改几个字段的格式
station.beg_date=rstrfmttojul(sta.beg_date,"YYYYMMDD");
station.end_date=rstrfmttojul(sta.end_date,"YYYYMMDD");
//我的数据插入数据库
ret=DAU_insert(&_DAU,buf);
如果你一个一个字段的处理,不仅繁琐,还非常容易飞掉,因为二者的数据根本就是不同的,我用了不同的模板约束之。
这几句所有函数都是DAU系统提供的,它保证了程序又快又可靠。基本半傻的程序员都很容易写出这程序(事先要读读DAU手册)。
在专用加载程序里,被特别处理的列是点了列名的。但在存取这些表时仍然不点列名。
模板写了一次,用了好多次。还可以在别的程序中反复使用。
这程序,没用一丁点怪异语句,都是最简单最基本的C语言。
[ 本帖最后由 yulihua49_cu 于 2009-7-3 17:15 编辑 ] |
|