当前位置:数据库 > SQLServer >>

sql语句执行与直接执行存储过程的效率比较

1.关于拼sql语句执行与直接执行存储过程的效率比较


1)在程序代码中拼sql语句,类似代码如下:
               

 代码如下 复制代码

System.Data.OleDb.OleDbCommand Command=conn.CreateCommand();
                Command.CommandTimeout=0;
                Command.Transaction=trans;
                Command.CommandType=CommandType.Text;
                string sql="";
                for(int i=0;i<IWo_Img.Count;i++)
                {
                    Wo_Img=IWo_Img[i] as EMS_EDI_WO_IMG;
               
                   
                    Wo_ImgDb.Wo_No=Wo_Img.Wo_No;
                    Wo_ImgDb.Comp_No=Wo_Img.Comp_No;
                    Wo_ImgDb.Take_Date_=Wo_Img.Take_Date;
                    Wo_ImgDb.Ems_No= ems_no;
                    Wo_ImgDb.Process_No=Wo_Img.Process_No;
                    Wo_ImgDb.Item_No_=Wo_Img.Item_No;
                    Wo_ImgDb.Take_Type_=Wo_Img.Take_Type;
                    Wo_ImgDb.Qty_=Wo_Img.Qty.ToString();
                    Wo_ImgDb.Unit_=Wo_Img.Unit;
                    Wo_ImgDb.Pcno=pcsum;

                    sql ="insert into EMS_EDI_WO_IMG(WO_NO,COMP_NO,Take_Date,PROCESS_NO,Item_No,Take_Type,Qty,Unit,EMS_NO,PCNO) " +
                          " values('"+Wo_ImgDb.Wo_No +"','"+Wo_ImgDb.Comp_No+"','"+Wo_ImgDb.Take_Date_+"','"+Wo_ImgDb.Process_No+"','"+Wo_ImgDb.Item_No_+"','" +
                          Wo_ImgDb.Take_Type_ +"','"+Wo_ImgDb.Qty_+"','"+Wo_ImgDb.Unit_+"','"+Wo_ImgDb.Ems_No+"','"+Wo_ImgDb.Pcno+"'"+
                          ")";
                    Command.CommandText=sql;
                    Command.ExecuteNonQuery();

                   
                    this.m_Actor.ShowCurrentValue(j+1);
                    j=j+1;
                   
                    if((j%1000==0) && (j!=0))
                    {
                        this.m_Actor.ShowMessage("成功导入1000条工单领料信息数据!");
                    }
                }
                Command.Dispose();
            }


           
执行5次,每次执行时都重启一下数据库,每次导入8000条数据,执行时间为:
第一次:费时0分2秒609毫秒
第二次:费时0分2秒375毫秒
第三次:费时0分2秒421毫秒
第四次:费时0分2秒562毫秒
第五次:费时0分2秒437毫秒

2)使用存储过程来插入数据,代码如下:

 代码如下 复制代码


                System.Data.OleDb.OleDbCommand Command=conn.CreateCommand();
                Command.CommandTimeout=0;
                Command.Transaction=trans;
                Command.CommandType=CommandType.StoredProcedure;
                    Command.CommandText="SP_InsWOImgList";

                System.Data.OleDb.OleDbParameter [] parameters={ 
                                                                   new System.Data.OleDb.OleDbParameter("wono",OleDbType.VarChar,50)              ,
                                                                   new System.Data.OleDb.OleDbParameter("tradeCo",OleDbType.VarChar,10)              ,
                                                                   new System.Data.OleDb.OleDbParameter("takeDate",OleDbType.VarChar,12)              ,
                                                                   new System.Data.OleDb.OleDbParameter("processNo",OleDbType.VarChar,50)              ,
                                                                   new System.Data.OleDb.OleDbParameter("itemNo",OleDbType.VarChar,50)              ,
                                                                   new System.Data.OleDb.OleDbParameter("takeType",OleDbType.VarChar,10)              ,
                                                                   new System.Data.OleDb.OleDbParameter("qty",OleDbType.VarChar,50)              ,
                                                                   new System.Data.OleDb.OleDbParameter("unit",OleDbType.VarChar,10)              ,
                                                                   new System.Data.OleDb.OleDbParameter("pcno",OleDbType.Integer,4)              ,
                                                                   new System.Data.OleDb.OleDbParameter("emsno",OleDbType.VarChar,12)     };       

                for(int i=0;i<IWo_Img.Count;i++)
                {
                    Wo_Img=IWo_Img[i] as EMS_EDI_WO_IMG;
               
                        Command.Parameters.Clear();
                    Wo_ImgDb.Wo_No=Wo_Img.Wo_No;
                    Wo_ImgDb.Comp_No=Wo_Img.Comp_No;
                    Wo_ImgDb.Take_Date_=Wo_Img.Take_Date;
                    Wo_ImgDb.Ems_No= ems_no;
                    Wo_ImgDb.Process_No=Wo_Img.Process_No;
                    Wo_ImgDb.Item_No_=Wo_Img.Item_No;
                    Wo_ImgDb.Take_Type_=Wo_Img.Take_Type;
                    Wo_ImgDb.Qty_=Wo_Img.Qty.ToString();
                    Wo_ImgDb.Unit_=Wo_Img.Unit;
                    Wo_ImgDb.Pcno=pcsum;

               
                        parameters[0].Value=Wo_ImgDb.Wo_No;
                    parameters[1].Value=Wo_ImgDb.Comp_No;
                    parameters[2].Value=Wo_ImgDb.Take_Date_;
                    parameters[3].Value=Wo_ImgDb.Process_No;
                parameters[4].Value=Wo_ImgDb.Item_No_;
                parameters[5].Value=Wo_ImgDb.Take_Type_;
                    parameters[6].Value=Wo_ImgDb.Qty_;
                parameters[7].Value=Wo_ImgDb.Unit_;
                    parameters[8].Value=Wo_ImgDb.Pcno;
                    parameters[9].Value=Wo_ImgDb.Ems_No;
               
                            for (int k=0;k<10;k++)
                {
                    Command.Parameters.Add(parameters[k]);
                }
               
                    Command.ExecuteNonQuery();

                   
                    this.m_Actor.ShowCurrentValue(j+1);
                    j=j+1;
                   
                    if((j%1000==0) && (j!=0))
                    {
                        this.m_Actor.ShowMessage("成功导入1000条工单领料信息数据!");
                    }
                }
                Command.Dispose();
            }


执行5次,每次执行时都重启一下数据库,每次导入8000条数据,执行时间为:
第一次:费时0分1秒531毫秒
第二次:费时0分1秒656毫秒
第三次:费时0分1秒533毫秒
第四次:费时0分1秒542毫秒
第五次:费时0分1秒551毫秒


存储过程执行/程序中拼sql语句执行

7813/12404=62.99%
程序中拼sql语句执行/存储过程执行
12404/7813=158.76%

 

最后得出结论,存储过程的执行效率要比在程序中拼sql语句的执行效率要高。不过如果总的执行时间很长,如10分钟或是20分钟,这样节省的时间
对用户来说可能没什么太大的感觉,例如由原来的20分钟,缩短到12-13分钟,如果不卡表比照,没什么感觉的。只有当原来是2个小时的导入时间,
现在只要1个小时多一点,这时才会有明显的感觉。至于最后使用哪种方式,自己选择,如果导入时间在20分钟以内,如果不要求性能与效率,
哪种方式都差不多。

补充:数据库,Mssql
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,