Filtered indexes in sql server 2008 tech republic

303 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
303
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Filtered indexes in sql server 2008 tech republic

  1. 1. Filtered Indexes in SQL Server 2008 | TechRepublic ZDNet Asia SmartPlanet TechRepublic Log In Join TechRepublic FAQ Go Pro! Blogs Downloads Newsletters Galleries Q&A Discussions News Research Library IT Management Development IT Support Data Center Networks Security Home / Blogs / The Enterprise Cloud Follow this blog: The Enterprise Cloud Filtered Indexes in SQL Server 2008 By Tim Chapman December 22, 2008, 8:44 AM PST Takeaway: Filtered indexes are a neat new feature in SQL Server 2008 that allows you to define indexes on subsets of data. In today’s article, database architect Tim Chapman shows how you can take advantage of this useful new feature. A filtered index is a non-clustered index created on a well-defined subset of data in a SQL Server table object. By “well-defined”, I am talking about those sets of data that are used exclusively to satisfy query criteria. For example, if you have a field in a table that contains predominately NULL values, you may benefit from creating a filtered index that only contains those values that are NOT MapR Hadoop NULL. Note that you cannot define a clustered index with a filter. Download Why a filtered index? Most Open, Enterprise-Grade Distribution for Hadoop. Try Now. Filtered indexes can provider performance gains in those scenarios where a majority of queries on www.mapr.com/Free-download a table filter on a specific subset of data. These indexes are likely going to be much smaller than Google Docs For Business an index on the entire field, so there is less index storage involved. Also, filtered indexes are Create & Upload Images, Tables, Equations, likely going to take less work to maintain. Because the filtered index will be smaller, data Drawings, Links & More! manipulation operations will affect smaller portions of the index, making these operations less www.google.com/apps costly in terms of database I/O. Dynamics in Romania Creating a filtered index Microsoft Dynamics NAV Microsoft Dynamics AX Let’s take a look at how to create a filtered index, and how we can see some performance benefits www.llpdynamics.ro from its use. First, run the following script to create the SalesHistory table and populate it. IF OBJECT_ID(’SalesHistory’, ‘U’) IS NOT NULL Keep Up with TechRepublic DROP TABLE SalesHistory GO CREATE TABLE [dbo].[SalesHistory] Five Apps Google in the Enterprise ( [SaleID] [int] IDENTITY(1,1), Subscribe Today [Product] [varchar](10) NULL, Follow us however you choose! [SaleDate] [datetime] NULL,http://www.techrepublic.com/blog/datacenter/filtered-indexes-in-sql-server-2008/490[08/29/2012 3:48:09 PM]
  2. 2. Filtered Indexes in SQL Server 2008 | TechRepublic [SalePrice] [money] NULL, CONSTRAINT PK_SalesHistory_SaleID PRIMARY KEY CLUSTERED (SaleID ASC) ) Media Gallery GO SET NOCOUNT ON BEGIN TRANSACTION DECLARE @i INT PHOTO GALLERY (1 of 15) Curiositys autonomous SET @i = 1 seven minutes of... WHILE (@i =5000) More Galleries » BEGIN INSERT INTO [SalesHistory](Product, SaleDate, SalePrice) VALUES (’Computer’, DATEADD(ww, @i, ‘3/11/1919′), DATEPART(ms, GETDATE()) + (@i + 57)) VIDEO (1 of 13) Cracking Open: HTC Titan II INSERT INTO [SalesHistory](Product, SaleDate, SalePrice) More Videos » VALUES(’BigScreen’, DATEADD(ww, @i, ‘3/11/1927′), DATEPART(ms, GETDATE()) + (@i + 13)) Hot Questions View All INSERT INTO [SalesHistory](Product, SaleDate, SalePrice) 3 SSL redirection VALUES(’PoolTable’, DATEADD(ww, @i, ‘3/11/1908′), DATEPART(ms, GETDATE()) + (@i + 29)) Switching from a Job to a career in 3 the IT field: Need an IT pros SET @i = @i + 1 advice END 2 windows 7 wont shutdown and COMMIT TRANSACTION keeps switching on GO can anyone suggest if any such 2 software exist with similar With some records in my SalesHistory table, I am going to update the SaleDate to NULL for 6 out functionality? of every 7 records in the table. This will give me a pretty sparse distribution of values in the SaleDate field. After the update I will then create a normal nonclustered index on the SaleDate field. Ask a Question UPDATE SalesHistory SET SaleDate = NULL Hot Discussions View All WHERE (SaleID % 7) 0 Should developers be sued for 221 security holes? GO CREATE INDEX idx_SalesHistory_SaleDate 80 The sitting duck that is open source ON SalesHistory(SaleDate) 27 Five fast Windows desktop search Run the following command to run IO statistics on. This allows you to view IO values for each utilities TSQL command ran. SET STATISTICS IO ON 30 Is the death knell sounding for traditional antivirus? The following query returns all rows from the SalesHistory table where the SaleDate contains ahttp://www.techrepublic.com/blog/datacenter/filtered-indexes-in-sql-server-2008/490[08/29/2012 3:48:09 PM]
  3. 3. Filtered Indexes in SQL Server 2008 | TechRepublic value. This query uses the idx_SalesHistory_SaleDate index we created earlier, and uses an Index Seek operation to return 2142 rows. This query requires 8 logical reads from the database Start a Discussion to return the necessary rows. Note that the queries used in this article only return the SaleDate field in the resultset. The reason for this excluding or including more fields in the SELECT list will alter the execution plan. So, for Blog Archive the purposes of this article, I will only return the field for which I am setting criteria. August 2012 December 2011 SELECT SaleDate July 2012 November 2011 FROM SalesHistory June 2012 October 2011 May 2012 September 2011 WHERE SaleDate IS NOT NULL April 2012 August 2011 I can view index related data for my idx_SalesHistory_SaleDate index through querying some March 2012 July 2011 system views. February 2012 June 2011 January 2012 SELECT i.name, p.rows, i.filter_definition FROM sys.partitions p JOIN sys.indexes i ON p.object_id = i.object_id AND p.index_id = i.index_id WHERE OBJECT_NAME(i.object_id) = ‘SalesHistory’ AND i.name = ‘idx_SalesHistory_SaleDate’ The following query is similar to the query specified above, but filters those rows where the SaleDate does contain a NULL value. This query also does an index seek on the index I created above, but this time requires 31 logical reads from the database as the query returns 12858 records. SELECT SaleDate FROM SalesHistory WHERE SaleDate IS NULL Now that I’ve looked a little bit how nonclustered indexes work, I’ll take a look at how you can user fliteres when defining the nonclustered index to index only subsets of data. First, I’ll need to drop the index I created above. DROP INDEX SalesHistory.idx_SalesHistory_SaleDate In the following script I create a filtered nonclustered index on the SaleDate field. This index will contain data pointers for ONLY those records for which the SaleDate IS NOT NULL. This means that for any records where the SaleDate IS NULL, the index will not be considered at all. CREATE INDEX idx_SalesHistory_SaleDate ON SalesHistory(SaleDate) WHERE SaleDate IS NOT NULL In the following query, an index scan of the idx_SalesHistory_SalePrice is used with 7 logical database reads. So, even though an index scan was performed, the operation took less logical reads due to the filtered index. SELECT SaleDate FROM SalesHistory WHERE SaleDate IS NOT NULLhttp://www.techrepublic.com/blog/datacenter/filtered-indexes-in-sql-server-2008/490[08/29/2012 3:48:09 PM]
  4. 4. Filtered Indexes in SQL Server 2008 | TechRepublic I can query the same system table query as before to view the number of records contained in the index. The previous index contained all 15,000 records from the table, whereas the current index contains only records where the SaleDate IS NOT NULL (2142 records in this case). SELECT i.name, p.rows, i.filter_definition FROM sys.partitions p JOIN sys.indexes i ON p.object_id = i.object_id AND p.index_id = i.index_id WHERE OBJECT_NAME(i.object_id) = ‘SalesHistory’ AND i.name = ‘idx_SalesHistory_SaleDate’ In the following query, I look for those records where the SaleDate IS NULL. Remember that the index I defined earlier only contains those records where the SaleDate IS NOT NULL, so it will not be considered for this query. In fact, a clustered index scan is used to find the records where the SaleDate IS NULL, resulting in 79 logical database reads. SELECT SaleDate FROM SalesHistory WHERE SaleDate IS NULL Conclusion The new filtered index feature in SQL Server 2008 is a very useful new feature. It allows you to create indexes on only subsets of frequently used data. However, these types of indexes should be used with care. In almost all circumstances, a normal non-clustered index will be the more useful index to use rather than a filtered on. Only after you are comfortable with the data usage patterns of your database should you consider creating filtered indexes, otherwise you may cause yourself Get IT Tips, news, and reviews delivered directly to your inbox by subscribing to TechRepublic’s free newsletters. About Tim Chapman Full Bio Contact Prerequisites overview for Perils of Adding fields to installing Windows Essential Database Tables Business Server 2008 Join the TechRepublic Community and join the conversation! Signing-up is free and quick, Do it now, we want to hear your opinion. Join Loginhttp://www.techrepublic.com/blog/datacenter/filtered-indexes-in-sql-server-2008/490[08/29/2012 3:48:09 PM]

×