|
本帖最后由 yulihua49 于 2013-4-7 22:22 编辑
update做好了,结果:
5 ./loadsth:29915 12/18 11:52'33 ___SQL_OpenDatabase__: Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bit Production
With the Partitioning, OLAP and Data Mining options
5 ./loadsth:29915 12/18 11:52'33 DB=TICKET
5 ./loadsth:29915 12/18 11:52'33 loadfile:entry
5 ./loadsth:29915 12/18 11:52'33 mkpk:tjdate|unit|tabname|flg|
5 ./loadsth:29915 12/18 11:52'33 insrec:bind=54,sth=0,stmt=INSERT INTO TICKET.TJRB (tjdate,unit,tabname,flg,dat1,dat2,dat3,dat4,dat5,dat6,dat7,dat8,dat9,dat10,dat11,dat12,dat13,dat14,dat15,dat16,dat17,dat18,dat19,dat20,dat21,dat22,dat23,dat24,dat25,dat26,dat27,dat28,dat29,dat30,dat31,dat32,dat33,dat34,dat35,dat36,dat37,dat38,dat39,dat40,dat41,dat42,dat43,dat44,dat45,dat46,dat47,dat48,dat49,dat50) VALUES(to_date(:1,'YYYY-MM-DD'), :2, :3, :4, :5, :6, :7, :8, :9, :10, :11, :12, :13, :14, :15, :16, :17, :18, :19, :20, :21, :22, :23, :24, :25, :26, :27, :28, :29, :30, :31, :32, :33, :34, :35, :36, :37, :38, :39, :40, :41, :42, :43, :44, :45, :46, :47, :48, :49, :50, :51, :52, :53, :54 )
5 ./loadsth:29915 12/18 11:52'33 updaterec:bind=58,sth=1,stmt=UPDATE TICKET.TJRB SET(tjdate,unit,tabname,flg,dat1,dat2,dat3,dat4,dat5,dat6,dat7,dat8,dat9,dat10,dat11,dat12,dat13,dat14,dat15,dat16,dat17,dat18,dat19,dat20,dat21,dat22,dat23,dat24,dat25,dat26,dat27,dat28,dat29,dat30,dat31,dat32,dat33,dat34,dat35,dat36,dat37,dat38,dat39,dat40,dat41,dat42,dat43,dat44,dat45,dat46,dat47,dat48,dat49,dat50)=(SELECT to_date(:1,'YYYY-MM-DD'), :2, :3, :4, :5, :6, :7, :8, :9, :10, :11, :12, :13, :14, :15, :16, :17, :18, :19, :20, :21, :22, :23, :24, :25, :26, :27, :28, :29, :30, :31, :32, :33, :34, :35, :36, :37, :38, :39, :40, :41, :42, :43, :44, :45, :46, :47, :48, :49, :50, :51, :52, :53, :54 FROM DUAL) WHERE tjdate=to_date(:55,'YYYY-MM-DD') AND unit=:56 AND tabname=:57 AND flg=:58
2 ./loadsth:29915 12/18 11:52'36 loadfile:rows=0,upd=3776,loss=0
2 ./loadsth:29915 12/18 11:52'36 loadasc:load 0 rec's time=3,buf=0
3秒,有点对劲了,但,loss还是140多秒,为什么?
程序全文发表如下:
- #include <stdio.h>
- #define SERVER
- #include <DAU_utility.h>
- #include <ctype.h>
- #define trans_begin(SQL_Connect) ___SQL_Transaction__(SQL_Connect,TRANBEGIN)
- #define trans_rollback(SQL_Connect) ___SQL_Transaction__(SQL_Connect,TRANROLLBACK)
- #define trans_commit(SQL_Connect) ___SQL_Transaction__(SQL_Connect,TRANCOMMIT)
- char * mkcondi(char *vp,T_PkgType *typ,int *argc)
- {
- vp+=sprintf(vp,"%s=",typ->name);
- switch(typ->type) {
- case CH_DATE:
- case CH_JUL:
- case CH_CJUL:
- case CH_TIME:
- case CH_CTIME:
- case CH_MINUTS:
- case CH_CMINUTS:
- vp += sprintf(vp,"to_date(:%d,'%s')",++*argc,typ->format);
- break;
- default:
- vp+=sprintf(vp,":%d",++*argc);
- break;
- }
- return vp;
- }
- static int mkwhere(SRM *srmp,char *where,int *argc)
- {
- char *pks,*p,*p1;
- char kbuf[31];
- int i,j;
- T_PkgType Char_Type[2];
- if(!srmp||!where) return 0;
- if(srmp->pkn<=0 || !srmp->pks) {
- return 0;
- }
- *where=0;
- pks=strdup(srmp->pks);
- p=pks;
- Char_Type[0].type=CH_CHAR;
- Char_Type[0].len=-1;
- Char_Type[0].offset=0;
- Char_Type[1].type=-1;
- Char_Type[1].len=0;
- p1=where;
- p1+=sprintf(p1,"WHERE ");
- for(i=0;i<srmp->pkn;i++) {
- p+=net_dispack(kbuf,p,Char_Type);
- j=pkg_getnum(kbuf,srmp->tp);
- if(srmp->tp[j].type<0) continue;
- if(i>0) p1+=sprintf(p1," AND ");
- p1 = mkcondi(p1,&srmp->tp[j],argc);
- }
- free(pks);
- //ShowLog(5,"mkwere:%s",where);
- return strlen(where);
- }
- static char *mk_val(char *values,void *data, T_PkgType *tp,int *num)
- {
- T_PkgType *typ;
- char *vp,tmp[4096];
- int i;
- if(!values || !data || !tp ->type<0) return values;
- vp=values;
- *vp=0;
- if(tp->offset<0) set_offset(tp);
- for(i=0,typ=tp;typ->type != -1;i++,typ++) {
- if(typ->type == CH_CLOB || !strcmp(typ->name,"ROWID")) {
- /* can not insert CLOB *p and ROWID into database,skip it*/
- continue;
- }
- switch(typ->type) {
- case CH_DATE:
- case CH_JUL:
- case CH_CJUL:
- case CH_TIME:
- case CH_CTIME:
- case CH_MINUTS:
- case CH_CMINUTS:
- vp += sprintf(vp,"to_date(:%d,'%s'),",++*num,typ->format);
- break;
- default:
- vp+=sprintf(vp," :%d,",++*num);
- break;
- }
- }
- if(*(vp-1) == ',') *(--vp)=0;
- return vp;
- }
- static int updaterec(DAU *DP,char *buf)
- {
- int ret,bindnum,i;
- int argc=0;
- char **argv;
- char *p,tmp[4096];
- static sqlo_stmt_handle_t upd_sth=SQLO_STH_INIT;
- int coln,pkn;
- if(!buf) { //任务结束,关闭游标
- if(upd_sth>=0) ret=___SQL_Close__(DP->SQL_Connect,upd_sth);
- upd_sth=SQLO_STH_INIT;
- return ret;
- }
- coln=abs(DP->srm.Aflg);
- pkn=DP->srm.pkn;
- argv=(char **)malloc((coln+pkn+1) * sizeof(char *));
- if(!argv) {
- sprintf(buf,"updaterec:argv #存储分配错误#");
- return MEMERR;
- }
- /* 绑定列值,有多少要绑定的?*/
- for(bindnum=0;bindnum<coln;bindnum++) {
- if(DP->srm.tp[bindnum].type == CH_CLOB || !strcmp(DP->srm.tp[bindnum].name,"ROWID")) {
- /* can not insert CLOB *p and ROWID into database,skip it*/
- bindnum--;
- continue;
- }
- get_one(buf,DP->srm.rec,DP->srm.tp,bindnum,0); //取值,转char类型
- argv[bindnum]=strdup(buf); //值加入argv
- }
- if(pkn > 0) { // 绑定键值
- char *pks,*p2;
- int n;
- pks=strdup(DP->srm.pks);
- p2=pks;
- for(ret=0;ret<pkn;ret++) {
- p2+=net_dispack(tmp,p2,CharType);
- n=pkg_getnum(buf,DP->srm.tp);
- get_one(tmp,DP->srm.rec,DP->srm.tp,n,0); //取值,转char类型
- argv[bindnum++]=strdup(tmp); //值加入argv
- }
- free(pks);
- }
- if(upd_sth==SQLO_STH_INIT) {
- p=buf;
- p+=sprintf(p,"UPDATE %s.%s SET(%s)=(SELECT ",DP->SQL_Connect->DBOWN, DP->srm.tabname,
- mkset(tmp,DP->srm.tp));
- p=mk_val(p,DP->srm.rec,DP->srm.tp,&argc);
- p+=sprintf(p," FROM DUAL) ");
- p+=mkwhere(&DP->srm,p,&argc);
- upd_sth=sqlo_open(DP->SQL_Connect->dbh, (CONST char *)buf,bindnum,(CONST char **)argv);
- ShowLog(5,"updaterec:bind=%d,sth=%d,stmt=%s",bindnum,upd_sth,buf);
- if(upd_sth<0) ret=upd_sth;
- else ret=0;
- } else {
- ret=sqlo_reopen(upd_sth,bindnum,(CONST char **)argv);
- }
- if(ret) {
- ___SQL_GetError(DP->SQL_Connect);
- ShowLog(1,"updaterec:sqlo_open=%d,errmsg=%s",ret,DP->SQL_Connect->ErrMsg);
- for(i=0;i<bindnum;i++) free(argv[i]);
- free(argv);
- return -1;
- }
- while (SQLO_STILL_EXECUTING == (ret = sqlo_execute(upd_sth, 1))) {
- usleep(1000);
- }
- if(ret) {
- char *p;
- ___SQL_GetError(DP->SQL_Connect);
- p=buf;
- for(i=0;i<bindnum;i++) p+=sprintf(p,"%s,",argv[i]);
- ShowLog(1,"updaterec:sqlo_execute=%d,err=%d,%s,values=%s",ret,
- DP->SQL_Connect->Errno,
- DP->SQL_Connect->ErrMsg,buf);
- }
- for(i=0;i<bindnum;i++) free(argv[i]);
- free(argv);
- return ret;
- }
- static int insrec(DAU *DP,char *buf)
- {
- int ret,bindnum;
- int argc=0;
- char **argv;
- char *p,tmp[4096];
- static sqlo_stmt_handle_t ins_sth=SQLO_STH_INIT;
- int coln,i;
- if(!buf) { //任务结束,关闭游标
- if(ins_sth>=0) ret=___SQL_Close__(DP->SQL_Connect,ins_sth);
- ins_sth=SQLO_STH_INIT;
- return ret;
- }
- coln=abs(DP->srm.Aflg);
- argv=(char **)malloc((coln+1) * sizeof(char *));
- if(!argv) {
- sprintf(buf,"insrec argv #存储分配错误#");
- return MEMERR;
- }
- /* 绑定列值,有多少要绑定的?*/
- for(bindnum=0;bindnum<coln;bindnum++) {
- if(DP->srm.tp[bindnum].type == CH_CLOB || !strcmp(DP->srm.tp[bindnum].name,"ROWID")) {
- /* can not insert CLOB *p and ROWID into database,skip it*/
- bindnum--;
- continue;
- }
- get_one(tmp,DP->srm.rec,DP->srm.tp,bindnum,0); //取值,转char类型
- argv[bindnum]=strdup(tmp); //值加入argv
- }
- if(ins_sth==SQLO_STH_INIT) {
- p=buf;
- p+=sprintf(p,"INSERT INTO %s.%s (%s) VALUES(",DP->SQL_Connect->DBOWN, DP->srm.tabname,
- mkset(tmp,DP->srm.tp));
- p=mk_val(p,DP->srm.rec,DP->srm.tp,&argc);
- p+=sprintf(p," )");
- ins_sth=sqlo_open(DP->SQL_Connect->dbh, (CONST char *)buf,bindnum,(CONST char **)argv),
- ShowLog(5,"insrec:bind=%d,sth=%d,stmt=%s",bindnum,ins_sth,buf);
- if(ins_sth<0) ret=ins_sth;
- else {
- ret=0;
- }
- } else {
- ret=sqlo_reopen(ins_sth,bindnum,(CONST char **)argv);
- }
- if(ret) {
- ___SQL_GetError(DP->SQL_Connect);
- ShowLog(5,"insrec:sqlo_open=%d,errmsg=%s",ret,DP->SQL_Connect->ErrMsg);
- for(i=0;i<bindnum;i++) free(argv[i]);
- free(argv);
- return -1;
- }
- while (SQLO_STILL_EXECUTING == (ret = sqlo_execute(ins_sth, 1))) {
- usleep(1000);
- }
- if(ret) {
- ___SQL_GetError(DP->SQL_Connect);
- sprintf(buf,"insrec:sqlo_execute=%d,err=%d,%s",ret,
- DP->SQL_Connect->Errno,
- DP->SQL_Connect->ErrMsg);
- }
- for(i=0;i<bindnum;i++) free(argv[i]);
- free(argv);
- return ret;
- }
- int loadfile(T_SQL_Connect *SQL_Connect,char *tabname,FILE *ifd,FILE *ofd,int Pflg,char *buf,int buflen)
- {
- char *p,tabn[512];
- DAU _DAU;
- int rows,ret;
- int upd,loss;
- ShowLog(5,"loadfile:entry");
- if(tabname) {
- ret=DAU_init(&_DAU,SQL_Connect,tabname,0,0);
- if(ret) {
- ShowLog(1,"loadfile:DAU_init tabname=%s,ret=%d",tabname,ret);
- return -1;
- }
- }
- upd=loss=0;
- for(rows=0;!ferror(ifd);rows++) {
- fgets(buf,buflen,ifd);
- if(feof(ifd)) break;
- TRIM(buf);
- if(!*buf) {
- rows--;
- continue;
- }
- if(rows==0 && !tabname) {
- p=skipblk(buf);
- ret=sscanf(p,"TABNAME=%s",tabn);
- if(ret<1) {
- ShowLog(1,"Can Not find TABLE Name!");
- return FORMATERR;
- }
- ret=DAU_init(&_DAU,SQL_Connect,tabn,0,0);
- if(ret) {
- ShowLog(1,"loadfile:DAU_init tabname=%s",tabn);
- return -1;
- }
- continue;
- }
- DAU_dispack(&_DAU,buf);
- ret=insrec(&_DAU,buf);
- if(ret) {
- if(SQL_Connect->Errno == DUPKEY) {
- if(!Pflg) {//如果没有-P选项
- ret=updaterec(&_DAU,buf);
- if(ret==0) upd+=1;
- else loss++;
- }
- else loss++;
- } else {
- DAU_pack(&_DAU,buf);
- fprintf(ofd,"%s\n",buf);
- ShowLog(1,"loadfile:%s",buf);
- loss++;
- }
- rows--;
- continue;
- }
- }
- DAU_free(&_DAU);
- ShowLog(2,"loadfile:rows=%d,upd=%d,loss=%d",rows,upd,loss);
- return rows;
- }
- static char my_showid[200];
- main(int argc,char *argv[])
- {
- int ret,i;
- T_SQL_Connect SQL_Connect;
- int Pflg=0;
- char *tabname=0;
- FILE *ifd,*ofd;
- INT64 now;
- char buf[4096];//最大数据长度有限
- tzset();
- sprintf(my_showid,"%s:%d",
- argv[0],getpid());
- Showid=my_showid;
- ifd=0;
- ofd=0;
- /*******************************************************************
- * get Opt
- *******************************************************************/
- for(i=1;i<argc;i++) {
- if(*argv[i]=='-') {
- switch(argv[i][1]) {
- case 'f':
- if(argv[i][2]) ret=envcfg(argv[i]+2);
- else {
- i++;
- ret=envcfg(argv[i]);
- }
- continue;
- case 'P': //输出 不能加载的记录
- Pflg=1;
- if(argv[i][2]) {
- ofd=fopen(argv[i]+2,"w");
- } else {
- i++;
- if(i<argc) ofd=fopen(argv[i],"w");
- }
- continue;
- default:
- fprintf(stderr,"no know option:%s",argv[i]);
- fprintf(stderr,"Usage:%s -f 配置文件 [-P] 输出文件名 ",
- argv[0]);
- continue;
- }
- }
- tabname=argv[i];
- }
- ShowLog(5,"tabname=%s",tabname);
- ret=db_open(&SQL_Connect);
- if(ret) {
- ShowLog(1,"Open Database err=%d.%s",
- SQL_Connect.Errno,
- SQL_Connect.ErrMsg);
- return 1;
- }
- ShowLog(5,"DB=%s",SQL_Connect.DBOWN);
- if(!ofd) ofd=stdout;
- ifd=stdin;
- now=now_sec();
- ret=loadfile(&SQL_Connect,tabname,ifd,ofd,Pflg,buf,sizeof(buf));
- ShowLog(2,"loadasc:load %d rec's time=%d,buf=%s",ret,(int)(now_sec()-now),buf);
- trans_commit(&SQL_Connect);
- if(ofd && ofd != stdout) fclose(ofd);
- ret=___SQL_CloseDatabase__(&SQL_Connect);
- return 0;
- }
复制代码
[ 本帖最后由 yulihua49 于 2008-12-18 12:01 编辑 ]
|
|