简体   繁体   中英

How to make table updating more efficient in C#?

in the following code I am trying to go through a list of customer IDs and update the Confirmed field to match sysdate in the given records. The problem is, if I do this for around 40k customers separately, it will take between 15-20 minutes to run, witch would be a bit too much for such an operation. Could you please give me suggestions on how I can improve my code to run faster and/or reduce the number of database requests?

foreach (int i in confirmCustomers)
{
    queryString = @"
        UPDATE TABLENAME
        SET CONFIRMED = @datenow
        WHERE ID = @id";

    command = new SqlCommand(queryString, connection, transaction);
    command.Parameters.AddWithValue("@id", i);
    command.Parameters.AddWithValue("@datenow", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
    command.ExecuteNonQuery();
}

For SQL Server 2016+ you can append all the IDs in a JSON array like

"[1,2,3,4,5,6,7,8,9]"

and pass that as a string parameter to the command, and run:

queryString = @"
    UPDATE TABLENAME
    SET CONFIRMED = @datenow
    WHERE ID in (select cast(value as int) from openjson( @ids ) )";

Try this:

        string queryString = @"UPDATE TABLENAME SET CONFIRMED = @datenow WHERE ID = @id";
        SqlCommand command = new SqlCommand(queryString, connection, transaction);
        command.Parameters.Add(new SqlParameter("@datenow", SqlDbType.DateTime));
        command.Parameters.Add(new SqlParameter("@id", SqlDbType.Int));
        command.Prepare();
        foreach (int i in confirmCustomers)
        {
            command.Parameters["@id"].Value = i;
            command.Parameters["@datenow"].Value = DateTime.Now;
            command.ExecuteNonQuery();
        }

You can use tabular approach by using string_split function as following..

/* init query string */
string queryString = @"
    UPDATE tbl
        SET tbl.CONFIRMED = getdate()
    FROM TABLENAME tbl
    INNER JOIN (
        SELECT ID FROM string_split(@ids, ',')
    ) ids
    WHERE tbl.ID = ids.ID";

/* init command */
SqlCommand command = new SqlCommand(queryString, connection, transaction);
command.Parameters.Add(new SqlParameter("@ids", SqlDbType.Text, 4000));
command.Prepare();

/* init list of id */
List<int> ids = new List<int>();

/* update table for each confirm customers */
foreach (int i in confirmCustomers)
{
    /* add id to list */
    ids.add(i);

    /* update 100 customer ids */
    if (ids.Count == 100)
    {
        command.Parameters[0].Value = String.Join(",", ids);
        command.ExecuteNonQuery();

        /* reset list of id */
        ids.Clear();
    }
}

/* update remains customer id */
if (ids.Count > 0)
{
    command.Parameters[0].Value = String.Join(",", ids);
    command.ExecuteNonQuery();
}

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM