SQL Server Transact-SQL DML

Performing UPDATES takes extra resources for SQL Server to perform. When performing an UPDATE, try to do as many of the following recommendations as you can in order to reduce the amount of resources required to perform the UPDATE. The more of the following suggestions you can do, the faster the UPDATE will perform.

  • If you are UPDATing a column of a row that has a unique index, try to update only one row at a time.
  • Try not to change the value of a column that is also the primary key.
  • When updating VARCHAR columns, try to replace the contents with contents of the same length.
  • Try to minimize the UPDATing of tables that have UPDATE triggers.
  • Try to avoid UPDATing columns that will be replicated to other databases.
  • Try to avoid UPDATing heavily indexed columns.
  • Try to avoid UPDATing a column that has a reference in the WHERE clause to the column being updated.

Of course, you may have very little choice when UPDATing your data, but at least give the above suggestions a thought. [2000, 2005, 2008] Updated 2-3-2009

*****

If your application needs to insert a large binary value into an image data column, perform this task using a stored procedure, not using an INSERT statement embedded in your application.

The reason for this is because the application must first convert the binary value into a character string (which doubles its size, thus increasing network traffic and taking more time) before it can be sent to the server. And when the server receives the character string, it then has to convert it back to the binary format (taking even more time).

Using a stored procedure avoids all this because all the activity occurs on the SQL Server, and little data is transmitted over the network. [2000, 2005, 2008] Updated 2-3-2009

*****

Let’s say that you often need to INSERT the same value into a column. For example, perhaps you have to perform 100,000 INSERTs a day into a particular table, and that 90% of the time the data INSERTed into one of the columns of the table is the same value.

If this the case, you can reduce network traffic (along with some SQL Server overhead) by creating this particular column with a default value of the most common value. This way, when you INSERT your data, and the data is the default value, you don’t INSERT any data into this column, instead allowing the default value to automatically be filled in for you. But when the value needs to be different, you will of course INSERT that value into the column. [2000, 2005, 2008] Updated 2-3-2009

*****

Generally, it is better to perform multiple UPDATEs on records in one fell swoop (using one query), instead of running the UPDATE statement multiple times (using multiple queries).

For example, you could accomplish this two different ways:

USE Northwind

UPDATE Products

SET UnitPrice = UnitPrice * 1.06

WHERE UnitPrice > 5

GO

USE Northwind

UPDATE Products

SET UnitPrice = ROUND(UnitPrice, 2)

WHERE UnitPrice > 5

GO

or

USE Northwind

UPDATE Products

SET UnitPrice = ROUND(UnitPrice * 1.06, 2)

WHERE UnitPrice > 5

GO

As is obvious from this example, the first option requires two queries to accomplish the same task as the second query. Running one query instead of two or more usually produces the best performance. [2000, 2005, 2008] Updated 2-3-2009

]]>

Leave a comment

Your email address will not be published.