Google
 

Friday, September 28, 2007

Building SQL Server Indexes in Ascending vs Descending Order

Building SQL Server Indexes in Ascending vs Descending Order

Written By: Edgewood Solutions Engineers -- 9/25/2007

Problem
When building indexes often the default options are used to create an index which creates the index in ascending order. This is usually the most logical way of creating an index, so the newest data or smallest value is at the top and the oldest or biggest value is at the end. Although searching an index works great by creating an index this way, but have you ever thought about the need to always return the most recent data first and ways you can create an index in descending order so the most recent data is always at the top of the index? Let's take a look at how this works and the advantages of creating an index in descending vs ascending order.

Solution
When creating an index you have the option to specify whether the index is created in ascending or descending order. This can be done simply by using the key word ASC or DESC when creating the index as follows. The following examples all use the AdventureWorks sample database.

Create index in descending order:

CREATE NONCLUSTERED INDEX [IX_PurchaseOrderHeader_OrderDate]
ON [Purchasing].[PurchaseOrderHeader]
( [OrderDate] DESC )

Create index in ascending order:

CREATE NONCLUSTERED INDEX [IX_PurchaseOrderHeader_OrderDate]
ON [Purchasing].[PurchaseOrderHeader]
( [OrderDate] ASC )

Let's take a look at a couple of queries and query plans to see how this differs and if there is any advantage.

Example 1

In this example we are using the PurchaseOrderHeader table to select the top 10 records and just the OrderDate column from the table sorted by OrderDate in ascending order. There is no index on the OrderDate column.

This query does a Clustered Index Scan and has a cost of 0.124344.

Example 2

In this example we are using the PurchaseOrderHeader table to select the top 10 records and just the OrderDate column from the table sorted by OrderDate in descending order. There is no index on the OrderDate column.

This query does a Clustered Index Scan and has a cost of 0.124344 which is the same as above.

Example 3

In this example we create an index on the OrderDate in ascending order.

We are using the PurchaseOrderHeader table to select the top 10 records and just the OrderDate column from the table sorted by OrderDate in ascending order.

This query does an Index Scan on the new index and has a cost of 0.0033056 which is much better than our previous value of 0.124344.

So from this we can see that adding the index does make this query much faster.

Example 4

In this example we created an index on the OrderDate in ascending order.

We are using the PurchaseOrderHeader table to select the top 10 records and just the OrderDate column from the table sorted by OrderDate in descending order.

This query does an Index Scan on the new index and has a cost of 0.0033056 which is the same as Example 3.

Take note that though the index was created in ascending order, getting the data in descending order is just as fast as getting the data in ascending order.

Example 5

Even though we already showed that selecting the data in descending order against an ascending index does not make a difference, let's just do a check to make sure.

In this example we created an index on the OrderDate in descending order.

We are using the PurchaseOrderHeader table to select the top 10 records and just the OrderDate column from the table sorted by OrderDate in ascending order.

This query does an Index Scan on the new index and has a cost of 0.0033056 which is the same as above, so there is no difference at all.

Another thing to look at is having the need to sort some of the columns in ascending order and other columns in descending order.

Example 6

In this example we created an index on the OrderDate in ascending order and SubTotal in ascending order.

CREATE NONCLUSTERED INDEX [IX_PurchaseOrderHeader_OrderDate]
ON [Purchasing].[PurchaseOrderHeader]
( [OrderDate] ASC, [SubTotal] ASC )

We are using the PurchaseOrderHeader table to select the top 10 records and the OrderDate and SubTotal column from the table sorted by OrderDate in ascending order and the SubTotal in ascending order.

This query does an Index Scan and has a cost of 0.0033123.

Example 7

In this example we created an index on the OrderDate in ascending order and SubTotal in ascending order..

We are using the PurchaseOrderHeader table to select the top 10 records and the OrderDate and SubTotal column from the table sorted by OrderDate in ascending order and the SubTotal in descending order.

This query does an Index Scan and has a cost of 0.102122. Having this index this way does not help much, since 84% of the work is done in the Sort operation.

Example 8

In this example we created an index on the OrderDate in ascending order and SubTotal in descending order.

CREATE NONCLUSTERED INDEX [IX_PurchaseOrderHeader_OrderDate]
ON [Purchasing].[PurchaseOrderHeader]
( [OrderDate] ASC, [SubTotal] DESC )

We are using the PurchaseOrderHeader table to select the top 10 records and the OrderDate and SubTotal column from the table sorted by OrderDate in ascending order and the SubTotal in descending order.

This query does an Index Scan and has a cost of 0.0033123. Having this index created this way helps out in a a big way. The Sort operation has now disappeared.

Summary
As we have shown creating an index in ascending or descending order does not make a big difference when there is only one column, but when there is a need to sort data in two different directions one column in ascending order and the other column in descending order the way the index is created does make a big difference.

Next Steps

  • Next time you create indexes keep the above in mind. Don't worry so much about creating your single column index in either ascending or descending order
  • If you have the need to create a multi-column index keep in mind the improvements that can be made if you create the index based on the way the data is accessed.

Thursday, September 27, 2007

SQL Server 2008 (katmai) - First Look

Presenter: Chad Boyd Sponsored By: Idera

Date: Wednesday, Sept 26, 2007
Time: 4:00 PM - 5:00 PM EDT

Join us for an overview of Microsoft SQL Server 2008, code name "Katmai", the next generation of SQL Server. "Katmai" provides an enterprise data platform for your mission-critical applications, while providing a dynamic development environment and comprehensive business intelligence (BI) platform. We will discuss the key features available in the next version of SQL Server including exciting new additions like Change Data Capture, Table Valued Parameters, Declarative Management Framework, MERGE T-SQL statements, the new FILESTREAM attribute, Data compression (both for data and backups), Database Mirroring enhancements, Performance Studio, Spatial enhancements, Extended Event Tracing and handling, Entity Framework Model discussions, and much more.
Our presenter, Chad Boyd, is an Advisory Support Engineer / Premier Engineer for SQL Server and Clustered systems responsible for providing onsite and remote support, guidance, and advice with SQL Server products to some of the foremost enterprise customers running the largest, most complex SQL Server installations and configurations in the world. This includes all SQL Server products and versions, including SQL Server 7.0, 2000, and 2005.

Space is limited. Reserve your Webinar seat now at:
https://www.gotomeeting.com/register/145428910

Wednesday, September 12, 2007

Applying SQL Server Service Packs

Problem
As with most applications there are hot fixes, updates and/or service packs that are often released. Some of these updates are automatic while others require you to take action and apply the update. SQL Server is one of those applications that requires you to take action versus having the updates automatically applied. This is a good thing, but it requires the DBA to take some type of action. In addition, applying the service packs to the database engine is pretty much understood, but did you also know that these updates also need to be applied on the clients as well?

Solution
As mentioned already, the SQL Server service packs are usually applied to the server and you can easily tell what version of the software you are running by either issuing a T-SQL command or by using the GUI such as the following.

SQL Server Management Studio

This is shown in the the object explorer.

SQL Server Enterprise Manager

This is shown if you right click on a server and select properties.

T-SQL Command

Or you can execute SELECT @@VERSION to get this info.

To tell what version you have installed refer to this tip: How to tell what SQL Server version you are running.

Client Tools

In addition, the client tools also need to be updated as well to ensure you have the latest release and also to ensure that all features still work properly. There are some cases where older versions of the tools will not work properly, so you will need to make sure you also update the client tools.

The install process works the same way as the server updates where you apply the service pack, but you only need to update the client tools which should be the only option when installing the service pack update.

The following shows you how you can determine what version of the client tools you have installed.

SQL Server 2005

From within management studio, select Help -> About and you will get a screen like the following. From this screen you can see what version of the Management Studio is installed as well as other components of SQL Server.

SQL Server 2000

With SQL Server 2000 it is not as easy. If you select Help -> About Microsoft SQL Enterprise Manger you will get the following screen. From this screen you can not tell what service pack updates have been installed only that is it is version 8.0, so this is not very helpful.

Another way to find out is to browse to the "C:\Program Files\Microsoft SQL Server\80\Tools\Binn" folder or whichever folder you have the SQL Server tools installed. You could then right click on one of the applications and select Properties and then look at the Version tab. Here we can see that the version of SQL Profiler is 2000.80.2039.0 which is the same as the database engine listed above.

Enterprise Manager is an MMC snap in, so you can not see the actual version of the file by using the same technique. When you try to look at the Properties for file "SQL Server Enterprise Manager.MSC" there is no version tab. There are other applications in this folder that you can look at which should show you the same version number, so you can pretty much be sure this is the version that has been installed.

Next Steps

  • Next time you apply your service packs to your servers, don't forget about the client tools on your desktops
  • Check out your existing client tools installations to make sure you have the latest versions installed or at least the equivalent of what is installed on your servers.
  • Refer to this tip How to tell what SQL Server version you are running to find out what the version number corresponds to.

Sunday, September 2, 2007

How to tell what SQL Server version you are running

There are two ways to determine the version of SQL Server that is installed.

The first is by using either Enterprise Manager or Management Studio and right clicking on the database instance name and selecting properties. In the general section you will see information such as on the following screenshots. The "Product version" or "Version" gives you a number of the version that is installed. As you can see with the SQL Server 2000 screenshot it also shows you the service pack that is installed.

SQL Server 2000

SQL Server 2005

Another option is to run the T-SQL command to give you this information. As you can see the output from both versions is pretty much the same. The one thing that you do not get when you do this is the Service Pack name like you do in Enterprise Manager.

SELECT @@VERSION

When this is run it will give you information such as the following:

SQL Server 2000

Microsoft SQL Server 2000 - 8.00.760 (Intel X86)
Dec 17 2002 14:22:05
Copyright (c) 1988-2003 Microsoft Corporation
Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 2)

SQL Server 2005

Microsoft SQL Server 2005 - 9.00.1399.06 (Intel X86)
Oct 14 2005 00:33:37
Copyright (c) 1988-2005 Microsoft Corporation
Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 2)

So now that you have this number such as 9.00.1399.06 or 8.00.760 what do these even mean? Basically the 8.0 is for SQL Server 2000 and the 9.0 is for SQL Server 2005. Here are a couple of links to other articles that give you the details of what is installed for SQL Server 6.0, 6.5, 7.0, 2000 and 2005.