本文介绍了插入记录需要花费很多时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 对于insert 4000记录,我创建了XML查询并将其发送到sql server, 我在sql server的程序看起来像这样 创建 过程 InsertUpdate_MSLFrequencyMaster_XML ( @ xml As Xml ) 作为 开始 插入 进入 MSLFrequencyMaster(cMSLClass,nSpecialtyNo,nValue,iYear,iMonth,nCreatedUserNo,dCreatedOn,nUserNo,dModifyOn) 选择标签.col.value(' ./ cmslclass [1]',' char(1)')作为 cMSLClass,tab.col.value( ' ./ nspecialtyno [1]',' numeric(18,0)') As nSpecialtyNo, tab.col.value(' ./ nvalue [1]',' numeric(18,2)') As nValue,tab.col.value(' ./ iyear [1]',' int') As iYear, tab.col.value(' ./ imonth [1]',' int')作为 iMonth,tab.col.value(' ./ nuserno [1]',' numeric(18,0)') As nCreatedUserNo, getdate()作为 dCreatedOn,tab.col.value(' ./ nuserno [1]',' numeric(18,0)') As nUserNo,getdate() As dModifyOn 来自 @ xml .nodes(' // root / mslfrequencymaster')作为标签(col) 其中 tab.col.value(' ./ dataopmode [1]',' int')= 1 结束 现在需要大约40分钟去完成。我能把时间缩短吗?怎么样? 请帮帮我们! 现在我的方法有点像这样.... 公共 功能 SaveXmlInDb( ByVal ProcedureName_1 作为 字符串,_ ByVal tbl4Save As Data.DataTable,_ ByRef eStr_Retu 作为 字符串,_ 可选 ByVal TimeOut_1 As 整数 = 0 )作为 布尔 Dim XmlRow As StringBuilder Dim Xml As StringBuilder Dim sqlCmd As SqlCommand Dim Row1 As 整数 Dim Col1 As 整数 Dim valStr As String 尝试 Xml = 新 StringBuilder 对于 Row1 = 0 tbl4Save.Rows。计数 - 1 XmlRow = 新 StringBuilder 对于 Col1 = 0 tbl4Save.Columns.Count - 1 如果 不 IsDBNull(tbl4Save.Columns( Col1))然后 valStr = Convert.ToString(tbl4Save.Rows(Row1)(Col1))valStr = valStr.Replace( ', '')。替换( &, & amp;)。替换( <, & lt;)。替换( >, & gt;) XmlRow.Append( < + tbl4Save .Columns(Col1).ColumnName.ToLower()+ >) XmlRow .Append(valStr) XmlRow.Append( < / + tbl4Save.Columns (Col1).ColumnName.ToLower()+ >) 结束 如果 下一步 Col1 Xml.Append( < + tbl4Save.TableName.ToLower()+ > + XmlRow.ToString()+ < / + tbl4Save .TableName.ToLower()+ >) 下一步 Row1 ProcedureName_1 + = N + ' + < root> + Xml.ToString()+ < / root> + ' sqlCmd = 新 SqlCommand(ProcedureName_1,_objDtLogic.Connection,_objDtLogic.Transaction) 如果 TimeOut_1<> 0 然后 sqlCmd.CommandTimeout = TimeOut_1 结束 如果 sqlCmd.ExecuteNonQuery() 返回 True Catch ex As 例外 SaveXmlInDb = 错误 eStr_Retu = ex.Message 结束 尝试 这是我在此方法中生成的xml < root > < mslfrequencymaster > < nmslfrequencyno > ; 1 < / nmslfrequencyno > < cmslclass > < / cmslclass > < nspecialtyno > 8667 < / nspecialtyno > < nvalue > 0.83 < / nvalue > < iyear > 2013 < / iyear > < imonth > 1 < / imonth > < ncreateduserno > 0 < / ncreateduserno > ; < dcreatedon > < / dcreated on > < nuserno > 7514 < / nuserno > < dmodifyon > 26-07-2013 20:14:27 < / dmodifyon > < dataopmode > 1 < / dataopmode > < / mslfrequencymaster > < mslfrequencymaster > < nmslfrequencyno > 2 < / nmslfrequencyno > < cmslclass > < / cmslclass > < nspecialtyno > 8667 < / nspecialtyno > < nvalue > 0.83 < / nvalue > < iyear > 2013 < / iyear > < imonth > 2 < / imonth > < ncreateduserno > 0 < / ncreateduserno > < dcreatedon > < / dcreatedon > < nuserno > 7514 < / nuserno > < dmodifyon > 26-07-2013 20:14:27 < / dmodifyon > < dataopmode > 1 < / dataopmode > < / mslfrequencymaster > < / mslfrequencymaster > < / root > 解决方案 XML对于这种用法来说真的很慢。 一次发送一个数据并以正常方式将它们插入表中,您应该从SQL Server获得大约500条记录/秒。 尽可能减少XMl String。我从代码端删除了额外的列,然后生成Xml字符串。我在程序中添加了所有其他额外记录。 For insert 4000 record, i created XML query and send it to sql server,and my procedure at sql server is look like thisCreate Procedure InsertUpdate_MSLFrequencyMaster_XML( @xml As Xml)AsBegin Insert Into MSLFrequencyMaster(cMSLClass, nSpecialtyNo, nValue, iYear, iMonth, nCreatedUserNo, dCreatedOn, nUserNo, dModifyOn) Select tab.col.value('./cmslclass[1]','char(1)') As cMSLClass, tab.col.value('./nspecialtyno[1]','numeric(18,0)') As nSpecialtyNo, tab.col.value('./nvalue[1]','numeric(18, 2)') As nValue, tab.col.value('./iyear[1]','int') As iYear, tab.col.value('./imonth[1]','int') As iMonth, tab.col.value('./nuserno[1]','numeric(18, 0)') As nCreatedUserNo, getdate() As dCreatedOn, tab.col.value('./nuserno[1]','numeric(18, 0)') As nUserNo, getdate() As dModifyOn From @xml.nodes('//root/mslfrequencymaster') As tab(col) Where tab.col.value('./dataopmode[1]','int') = 1Endnow it's taking around the 40 minute to complete. can i minimize the time ? how?please help us!Now my method is some sort like this....Public Function SaveXmlInDb(ByVal ProcedureName_1 As String, _ ByVal tbl4Save As Data.DataTable, _ ByRef eStr_Retu As String, _ Optional ByVal TimeOut_1 As Integer = 0) As Boolean Dim XmlRow As StringBuilder Dim Xml As StringBuilder Dim sqlCmd As SqlCommand Dim Row1 As Integer Dim Col1 As Integer Dim valStr As String Try Xml = New StringBuilder For Row1 = 0 To tbl4Save.Rows.Count - 1 XmlRow = New StringBuilder For Col1 = 0 To tbl4Save.Columns.Count - 1 If Not IsDBNull(tbl4Save.Columns(Col1)) Then valStr = Convert.ToString(tbl4Save.Rows(Row1)(Col1)) valStr = valStr.Replace("'", "''").Replace("&", "&").Replace("<", "<").Replace(">", ">") XmlRow.Append("<" + tbl4Save.Columns(Col1).ColumnName.ToLower() + ">") XmlRow.Append(valStr) XmlRow.Append("</" + tbl4Save.Columns(Col1).ColumnName.ToLower() + ">") End If Next Col1 Xml.Append("<" + tbl4Save.TableName.ToLower() + ">" + XmlRow.ToString() + "</" + tbl4Save.TableName.ToLower() + ">") Next Row1 ProcedureName_1 += " N" + "'" + "<root>" + Xml.ToString() + "</root>" + "'" sqlCmd = New SqlCommand(ProcedureName_1, _objDtLogic.Connection, _objDtLogic.Transaction) If TimeOut_1 <> 0 Then sqlCmd.CommandTimeout = TimeOut_1 End If sqlCmd.ExecuteNonQuery() Return True Catch ex As Exception SaveXmlInDb = False eStr_Retu = ex.Message End Tryhere is my xml which is generate during this method<root><mslfrequencymaster><nmslfrequencyno>1</nmslfrequencyno><cmslclass>A</cmslclass><nspecialtyno>8667</nspecialtyno><nvalue>0.83</nvalue><iyear>2013</iyear><imonth>1</imonth><ncreateduserno>0</ncreateduserno><dcreatedon></dcreatedon><nuserno>7514</nuserno><dmodifyon>26-07-2013 20:14:27</dmodifyon><dataopmode>1</dataopmode></mslfrequencymaster><mslfrequencymaster><nmslfrequencyno>2</nmslfrequencyno><cmslclass>A</cmslclass><nspecialtyno>8667</nspecialtyno><nvalue>0.83</nvalue><iyear>2013</iyear><imonth>2</imonth><ncreateduserno>0</ncreateduserno><dcreatedon></dcreatedon><nuserno>7514</nuserno><dmodifyon>26-07-2013 20:14:27</dmodifyon><dataopmode>1</dataopmode></mslfrequencymaster></mslfrequencymaster></root> 解决方案 XML is really slow for this kind of usage.Send your data one at a time and insert them into tables in the normal fashion, you should get around 500 records/sec from your SQL Server.Make less XMl String as much as possible. I remove extra column from the code side and then make Xml string. All other extra record i added in the procedure. 这篇关于插入记录需要花费很多时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-23 11:53