Note that there are some explanatory texts on larger screens.

plurals
  1. POAny way to speed up this UPDATE? C#, SQL, T-SQL
    text
    copied!<p>SQL:</p> <pre><code>CREATE FUNCTION dbo.fnRandomForeNames () RETURNS VARCHAR(50) AS BEGIN RETURN ( SELECT TOP 1 [FirstName] FROM [tmp_ForeNames] ORDER BY (SELECT new_id from GetNewID) ) END GO </code></pre> <p>Similar functions for dbo.fnRandomSurNames() etc.</p> <pre><code>UPDATE Table1 SET firstname = dbo.fnRandomForeNames(), lastname = dbo.fnRandomSurNames(), address1 = dbo.fnRandomAddress1(), address2 = dbo.fnRandomAddress2(), address3 = dbo.fnRandomAddress3(), birthdate = DATEADD(DAY, ABS(CHECKSUM(NEWID()) % 3650), '1990-01-01') </code></pre> <p>My C# Code: </p> <pre><code> private void RunThis(string connString, StreamReader sr) { sr.BaseStream.Position = 0; string sqlQuery = sr.ReadToEnd(); using (SqlConnection connection = new SqlConnection(connString)) { Server server = new Server(new ServerConnection(connection)); server.ConnectionContext.StatementTimeout = 4200; server.ConnectionContext.ExecuteNonQuery(sqlQuery); } sr.Close(); } </code></pre> <p>........ </p> <pre><code> RunThis(e.Argument.ToString(), _updateClaim); </code></pre> <p>Where <code>e.Argument.ToString()</code> is the connection string.</p> <p>The <code>CREATE FUNCTION</code> scripts are run earlier, take very little time to run. Also, names are stored in tmp databases, these are entered in C# via arrays. These also take very little time to run.</p> <p>Table1 contains approx 140,000 rows and takes approx. 14 mins to complete.</p> <p>I have also tried using parameterised SQL queries, skipping the tmp tables and SQL functions and instead creating the SQL query and executing it from the code, such as the following:</p> <pre><code>UPDATE Table1 SET lastname = '{0}', firstname = '{1}', birthdate = DATEADD(DAY, ABS(CHECKSUM(NEWID()) % 3650), '1990-01-01'), address1 = '{2}', address2 = '{3}', address3 = '{4}' WHERE u_id = '{6}' </code></pre> <p>And some C#: </p> <pre><code> using (SqlConnection connection = new SqlConnection(connString)) { connection.Open(); for (int i = 0; i &lt; arraySize; ++i) { string updateString = string.Format(updateString2, GetRandomSurname(), GetRandomForeName(), GetRandomAddress1(), GetRandomAddress2(), GetRandomAddress3(), "", ids[i]); SqlCommand cmd = new SqlCommand(updateString, connection); cmd.CommandType = CommandType.Text; cmd.ExecuteNonQuery(); } } </code></pre> <p>The latter method also taking upwards of 14 minutes.</p> <p>Any ideas on how to cut down the time it takes to update the table?</p> <p>Thanks.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload