SQL Server Transact-SQL General Tips

Suppose you have data in your table that represents the logical information of “Yes” and “No” and you want to give the results of a query to someone who isn’t working all day with computers. For such people, they may not know that a 1 is the logical representation of TRUE while a 0 represents FALSE. Sure, you can do this at the presentational layer. But what if someone comes to your desk, begging for immediate help? Here’s a little trick to make BITs (or any other 0 and 1 data) look a bit more intuitive:

id          bool  YesNo

———– —– —–

1           0     No

2           1     Yes

(2 row(s) affected)

How does this work? The trick happens inside the SUBSTRING function. Precisely, when calculating the start value for the SUBSTRING. If our column “bool” contains a 0, the calculation looks like SUBSTRING(‘YesNo’, 4 – 3 * 0, 3), which resolves to SUBSTRING(‘YesNo’, 4, 3) and therefore, correctly returns ‘No’. We actually use here another feature of SUBSTRING. If the string is shorter than our requested length, SUBSTRING simply returns the shorter string without filling up the missing spaces. Finally, in case a 1 is in our “bool” column, the calculation goes like SUBSTRING(‘YesNo’, 4 – 3 * 1, 4), which is SUBSTRING(‘YesNo, 1, 3) and that is ‘Yes’. [2000, 2005, 2008] Updated 1-30-2009

*****

Instead of using temporary tables, consider using a derived table instead. A derived table is the result of using a SELECT statement in the FROM clause of an existing SELECT statement. By using derived tables instead of temporary tables, you can reduce I/O and often boost your application’s performance. [2000, 2005, 2008] Updated 1-30-2009

*****

Sometimes, it is handy to be able to perform some calculation on one or more columns of a record, and then take the result of that calculation and then add it to similar calculations performed on other related records to find a grand total.

For example, let’s say you want to find the total dollar cost of an invoice. An invoice will generally involve a header record and one or more detail records. Each detail record will represent a line item on the invoice. In order to calculate the total dollar cost of an invoice, based on two or more line items, you would need to multiply the quantity of each item sold times the price of each item. Then, you would need to add the total price of each line item together in order to get the total dollar cost of the entire invoice. To keep this example simple, let’s ignore things like discounts, taxes, shipping, etc.

One way to accomplish this task would be to use a cursor; like we see below (we are using the Northwind database for this example code):

DECLARE @LineTotal MONEY –Declare variables

DECLARE @InvoiceTotal MONEY

SET @LineTotal = 0 –Set variables to 0

SET @InvoiceTotal = 0

DECLARE Line_Item_Cursor CURSOR FOR –Declare the cursor

SELECT UnitPrice*Quantity –Multiply unit price times quantity ordered

FROM [order details]

WHERE orderid = 10248 –We are only concerned with invoice 10248

OPEN Line_Item_Cursor –Open the cursor

FETCH NEXT FROM Line_Item_Cursor INTO @LineTotal –Fetch next record

WHILE @@FETCH_STATUS = 0

BEGIN

SET @InvoiceTotal = @InvoiceTotal + @LineTotal –Summarize line items

FETCH NEXT FROM Line_Item_Cursor INTO @LineTotal

END

CLOSE Line_Item_Cursor –Close cursor

DEALLOCATE Line_Item_Cursor –Deallocate cursor

SELECT @InvoiceTotal InvoiceTotal –Display total value of invoice

The result for invoice number 10248 is $440.00.

What the cursor does is to select all of the line items for invoice number 10248, then multiply the quantity ordered times the price to get a line item total, and then it takes each of the line item totals for each record and then adds them all up in order to calculate the total dollar amount for the invoice.

This all works well, but the code is long and hard to read, and performance is not great because a cursor is used. Ideally, for best performance, we need to find another way to accomplish the same goal as above, but without using a cursor.

Instead of using a cursor, let’s rewrite the above code using set-based Transact-SQL instead of a cursor. Here’s what the code looks like:

DECLARE @InvoiceTotal money

SELECT @InvoiceTotal = sum(UnitPrice*Quantity)

FROM [order details]

WHERE orderid = 10248

SELECT @InvoiceTotal InvoiceTotal

The result for invoice number 10248 is $440.00.

Right away, it is obvious that this is a lot less code and that is it more readable. What may not be obvious is that it uses less server resources and performs faster. In our example–with few rows–the time difference is very small, but if many rows are involved, the time difference between the techniques can be substantial.

The secret here is to use the Transact-SQL “sum” function to summarize the line item totals for you, instead of relying on a cursor. You can use this same technique to help reduce your dependency on using resource-hogging cursors in much of your Transact-SQL code. [2000, 2005, 2008] Updated 1-30-2009

*****

When using the WHILE statement, don’t avoid the use of BREAK just because some people consider it bad programming form. Often when creating Transact-SQL code using the WHILE statement, you can avoid using BREAK by moving a few lines of code around. If this works in your case, then by all means don’t use BREAK. But if your efforts to avoid using BREAK require you to add additional lines of code that makes your code run slower, then don’t do that. Sometimes, using BREAK can speed up the execution of your WHILE statements. [2000, 2005, 2008] Updated 1-30-2009

*****

One of the advantages of using SQL Server for n-tier applications is that you can offload much of the data processing work from the other tiers and place it on SQL Server. The more work you can perform within SQL Server, the fewer the network roundtrips that need to be made between the various tiers and SQL Server. And generally the fewer the network roundtrips, the more scalable and faster the application becomes.

But in some applications, such as those than involve complex math, SQL Server has traditionally been weak. In these cases, complex math often could not be performed within SQL Server; instead it had to be performed on another tier, causing more network roundtrips than desired.

By using user-defined functions (UDFs), this is becoming less of a problem. UDFs allow developers to perform many complex math functions from within SQL Server, functions that previously could only be performed outside of SQL Server. By taking advantage of UDFs, more work can stay with SQL Server instead of being shuttled to another tier, reducing network roundtrips, and potentially boosting your application’s performance.

Obviously, boosting your application’s performance is not as simple as moving math functions to SQL Server, but it is one feature of SQL Server that developers can take advantage of in order to boost their application’s scalability and performance. [2000, 2005, 2008] Updated 1-30-2009

*****

SQL Server offers a data type called “table.” Its main purpose is for the temporary storage of a set of rows. A variable, of type “table,” behaves as if it is a local variable. And like local variables, it has a limited scope, which is within the batch, function, or stored procedure in which it was declared. In most cases, a table variable can be used like a normal table. SELECTs, INSERTs, UPDATEs, and DELETEs can all be made against a table variable.

For better performance, if you need a temporary table in your Transact-SQL code, consider using a table variable instead of creating a conventional temporary table. Table variables are often faster, but not always. In addition, table variables found in stored procedures result in fewer compilations (than when using temporary tables), and transactions using table variables only last as long as the duration of an update on the table variable, requiring less locking and logging resources. [2000, 2005, 2008] Updated 1-30-2009

*****

Don’t repeatedly reuse the same function to calculate the same result over and over within your Transact-SQL code. For example, if you need to reuse the value of the length of a string over and over within your code, perform the LEN function once on the string, and this assign the result to a variable, and then use this variable, over and over, as needed in your code. Don’t recalculate the same value over and over again by reusing the LEN function each time you need the value, as it wastes SQL Server resources and hurts performance. [2000, 2005, 2008] Updated 1-30-2009

*****

Many developers choose to use an identify column at their primary key. By design, an identity column does not guarantee that that each newly created row will be consecutively numbered. This means there will most likely be occasional gaps in the identity column numbering scheme. For most applications, occasional gaps in the identity column present no problems.

On the other hand, some developers don’t like these occasional gaps, trying to avoid them. With some clever use of INSTEAD OF triggers in SQL Server, it is possible prevent these numbering gaps. But at what cost?

The problem with trying to force an identify column to number consecutively without gaps can lead to locking and scalability problems, hurting performance. So the recommendation is not to try to get around the identify column’s built-in method of working. If you do, expect performance problems. [2000, 2005, 2008] Updated 1-30-2009

*****

If you use the BULK INSERT to import data into SQL Server, consider using the TABLOCK hint along with it. This will prevent SQL Server from running out of locks during vary large imports, and also boost performance due to the reduction of lock contention. [2000, 2005, 2008] Updated 1-30-2009

Pages: 1 2




Related Articles :

  • No Related Articles Found

No comments yet... Be the first to leave a reply!

Software Reviews | Book Reviews | FAQs | Tips | Articles | Performance Tuning | Audit | BI | Clustering | Developer | Reporting | DBA | ASP.NET Ado | Views tips | | Developer FAQs | Replication Tips | OS Tips | Misc Tips | Index Tuning Tips | Hints Tips | High Availability Tips | Hardware Tips | ETL Tips | Components Tips | Configuration Tips | App Dev Tips | OLAP Tips | Admin Tips | Software Reviews | Error | Clustering FAQs | Performance Tuning FAQs | DBA FAQs |