Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
We insert more than 500000 rows from datatable to sql table, before using for loop it take more time, and we chnage bulk insert still slow and also throws session expiry.

So any other way to insert in one shot to table ?

Before i use this for loop, it takes 30 mins and above

VB
For Each objDR As DataRow In datas.Rows
           sqlcmd.CommandText = "insert into [RRights]([RoleId],[Database],[Item Type],[Email],[Print],[Design Profile],[Check In],[Design File],[Delete/Rename File],[File Output],[Create Version],[View Version],[Reports],[Management],[ShareFile],[Annotation],[RenameAll]) values ('" & stringRoleID & "',N'" & objDR("Path") & "',N'Folder','False','False','False','False','False','False','False','False','False','False','False','False','False','False')"
          sqlcmd.ExecuteNonQuery()
     Next


What I have tried:

VB
cmd.CommandText = "select RoleId ,Path as [Database] ,'Folder' as [Item Type]   from  DBFlow where ([Path] LIKE N'" & stringCabName & "\%') ORDER BY [Path] ASC"
        cmd.Connection = sqlcon
        da.SelectCommand = cmd
        da.Fill(datas)
        Dim sqlcmd As New SqlCommand
        sqlcmd.Connection = sqlcon
        Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings("FTConnectionString").ToString())
            cn.Open()
            Using copy As New SqlBulkCopy(cn)

                copy.ColumnMappings.Add("RoleId", "RoleId")
                copy.ColumnMappings.Add("Database", "Database")
                copy.ColumnMappings.Add("Item Type", "Item Type")

                copy.DestinationTableName = "RRights"
                copy.WriteToServer(datas)
            End Using
            cn.Close()
        End Using
Posted
Updated 25-Mar-18 17:19pm
v4
Comments
F-ES Sitecore 23-Mar-18 5:41am    
You can speed up your code by not using string concatenation, using sqlparams would probably be just as slow though so maybe try building your SQL using a StingBuilder that you initialise with a size big enough to accommodate the text.

Other than that you can remove any indexes, references, constraints etc on the table before you insert and add them back after. But ultimately what you're doing is going to be slow, there is no magic bullet beyond buy a better server.
Aravindba 23-Mar-18 5:58am    
hi, same code work with 100 gb database, but now got 800 gb in that database. so where the problem ? in code or sql or server configuration ?
Ram Nunna 26-Mar-18 6:25am    
You can pass datatable directly into database.
refer below link:
https://stackoverflow.com/questions/9075159/how-to-insert-a-data-table-into-sql-server-database-table
Aravindba 26-Mar-18 6:56am    
pls read my question before reply, i tried both which i mentioned already, using bulk query and single line by line also.Both i got slowness.

1 solution

Building 500000 times the sql string command is a slow process and it is dangerous too.
You should Google about "SQL bulk insert", there is only 1200000 answers, you should find something useful.
-----
VB
sqlcmd.CommandText = "insert into [RRights]([RoleId],[Database],[Item Type],[Email],[Print],[Design Profile],[Check In],[Design File],[Delete/Rename File],[File Output],[Create Version],[View Version],[Reports],[Management],[ShareFile],[Annotation],[RenameAll]) values ('" & stringRoleID & "',N'" & objDR("Path") & "',N'Folder','False','False','False','False','False','False','False','False','False','False','False','False','False','False')"

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
 
Share this answer
 
Comments
Aravindba 23-Mar-18 5:30am    
Yes,
1. That's y we remove For loop
2. i already tried SQL bulk insert, but still slow.
3. And correct even in single quotes, but this data take from one table and insert into another table and in all values we redistricted special characters, single quotes which are the cause SQL injection vulnerability.
Patrice T 23-Mar-18 5:42am    
There is no way issuing 500000 single row commands can be faster than a single bulk insert.
Aravindba 23-Mar-18 5:58am    
hi, same code work with 100 gb database, but now got 800 gb in that database. so where the problem ? in code or sql or server configuration ?
Patrice T 23-Mar-18 6:02am    
Use Improve question to update your question.
So that everyone can pay attention to this information.

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