Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
For insert 4000 record, i created XML query and send it to sql server,
and my procedure at sql server is look like this

SQL
Create Procedure InsertUpdate_MSLFrequencyMaster_XML
(
    @xml As Xml
)
As
Begin
        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') = 1
End



now 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....
VB
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("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;")
                        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 Try

here is my xml which is generate during this method
XML
<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>
Posted
Updated 26-Jul-13 2:17am
v3
Comments
ArunRajendra 25-Jul-13 0:50am    
Can you post the sample extract from xml file?
_Asif_ 25-Jul-13 2:39am    
Try debuging by turning off the insertion. Just check how much time is taken by the select query. This will help isolating the problem
7045Jeegnesh 26-Jul-13 8:20am    
wonder! it's just taking 6 second to select !
7045Jeegnesh 26-Jul-13 8:19am    
ok now i added both, Xml string and method. you can analyze it!

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.
 
Share this answer
 
Comments
7045Jeegnesh 27-Jul-13 0:20am    
i tried that. but it also taking to much time
Mehdi Gholam 27-Jul-13 0:25am    
Don't use the xml parsing stored procedure.
7045Jeegnesh 5-Aug-13 23:34pm    
then what should i try?
Mehdi Gholam 6-Aug-13 2:11am    
You can try "bulk insert"
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900