SlideShare a Scribd company logo
1 of 4
Download to read offline
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]
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)
                                                                                                                   Curiosity's 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 pro's
     SET @i = @i + 1                                                                                                advice
     END
                                                                                                              2     windows 7 won't 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 a


http://www.techrepublic.com/blog/datacenter/filtered-indexes-in-sql-server-2008/490[08/29/2012 3:48:09 PM]
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 NULL




http://www.techrepublic.com/blog/datacenter/filtered-indexes-in-sql-server-2008/490[08/29/2012 3:48:09 PM]
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       Login




http://www.techrepublic.com/blog/datacenter/filtered-indexes-in-sql-server-2008/490[08/29/2012 3:48:09 PM]

More Related Content

Similar to Filtered indexes in sql server 2008 tech republic

How do i... reseed a sql server identity column tech_republic
How do i... reseed a sql server identity column    tech_republicHow do i... reseed a sql server identity column    tech_republic
How do i... reseed a sql server identity column tech_republicKaing Menglieng
 
Speed up sql server apps - visual studio magazine
Speed up sql server apps  - visual studio magazineSpeed up sql server apps  - visual studio magazine
Speed up sql server apps - visual studio magazineKaing Menglieng
 
Analyst View of Data Virtualization: Conversations with Boulder Business Inte...
Analyst View of Data Virtualization: Conversations with Boulder Business Inte...Analyst View of Data Virtualization: Conversations with Boulder Business Inte...
Analyst View of Data Virtualization: Conversations with Boulder Business Inte...Denodo
 
Using sql server 2008's merge statement tech republic
Using sql server 2008's merge statement   tech republicUsing sql server 2008's merge statement   tech republic
Using sql server 2008's merge statement tech republicKaing Menglieng
 
Maximize Big Data ROI via Best of Breed Patterns and Practices
Maximize Big Data ROI via Best of Breed Patterns and PracticesMaximize Big Data ROI via Best of Breed Patterns and Practices
Maximize Big Data ROI via Best of Breed Patterns and PracticesJeff Bertman
 
Why Data Virtualization? An Introduction
Why Data Virtualization? An IntroductionWhy Data Virtualization? An Introduction
Why Data Virtualization? An IntroductionDenodo
 
The Big Picture: Big Data for the New Wave of Analytics
The Big Picture: Big Data for the New Wave of AnalyticsThe Big Picture: Big Data for the New Wave of Analytics
The Big Picture: Big Data for the New Wave of AnalyticsInside Analysis
 
All Grown Up: Maturation of Analytics in the Cloud
All Grown Up: Maturation of Analytics in the CloudAll Grown Up: Maturation of Analytics in the Cloud
All Grown Up: Maturation of Analytics in the CloudInside Analysis
 
Denodo Datafest 2017 London Tekin Mentes Logitech
Denodo Datafest 2017 London Tekin Mentes LogitechDenodo Datafest 2017 London Tekin Mentes Logitech
Denodo Datafest 2017 London Tekin Mentes LogitechTekin Mentes
 
My cool new Slideshow!
My cool new Slideshow!My cool new Slideshow!
My cool new Slideshow!rommel_gagasa
 
Secrets of Enterprise Data Mining: SQL Saturday 328 Birmingham AL
Secrets of Enterprise Data Mining: SQL Saturday 328 Birmingham ALSecrets of Enterprise Data Mining: SQL Saturday 328 Birmingham AL
Secrets of Enterprise Data Mining: SQL Saturday 328 Birmingham ALMark Tabladillo
 
¿Cómo modernizar una arquitectura de TI con la virtualización de datos?
¿Cómo modernizar una arquitectura de TI con la virtualización de datos?¿Cómo modernizar una arquitectura de TI con la virtualización de datos?
¿Cómo modernizar una arquitectura de TI con la virtualización de datos?Denodo
 
Using hash fields in sql server tech republic
Using hash fields in sql server   tech republicUsing hash fields in sql server   tech republic
Using hash fields in sql server tech republicKaing Menglieng
 
Left Brain, Right Brain: How to Unify Enterprise Analytics
Left Brain, Right Brain: How to Unify Enterprise AnalyticsLeft Brain, Right Brain: How to Unify Enterprise Analytics
Left Brain, Right Brain: How to Unify Enterprise AnalyticsInside Analysis
 
Data Virtualization for Data Architects (New Zealand)
Data Virtualization for Data Architects (New Zealand)Data Virtualization for Data Architects (New Zealand)
Data Virtualization for Data Architects (New Zealand)Denodo
 
Data Mining With Excel 2007 And SQL Server 2008
Data Mining With Excel 2007 And SQL Server 2008Data Mining With Excel 2007 And SQL Server 2008
Data Mining With Excel 2007 And SQL Server 2008Mark Tabladillo
 
DBT ELT approach for Advanced Analytics.pptx
DBT ELT approach for Advanced Analytics.pptxDBT ELT approach for Advanced Analytics.pptx
DBT ELT approach for Advanced Analytics.pptxHong Ong
 

Similar to Filtered indexes in sql server 2008 tech republic (20)

How do i... reseed a sql server identity column tech_republic
How do i... reseed a sql server identity column    tech_republicHow do i... reseed a sql server identity column    tech_republic
How do i... reseed a sql server identity column tech_republic
 
Speed up sql server apps - visual studio magazine
Speed up sql server apps  - visual studio magazineSpeed up sql server apps  - visual studio magazine
Speed up sql server apps - visual studio magazine
 
Analyst View of Data Virtualization: Conversations with Boulder Business Inte...
Analyst View of Data Virtualization: Conversations with Boulder Business Inte...Analyst View of Data Virtualization: Conversations with Boulder Business Inte...
Analyst View of Data Virtualization: Conversations with Boulder Business Inte...
 
Using sql server 2008's merge statement tech republic
Using sql server 2008's merge statement   tech republicUsing sql server 2008's merge statement   tech republic
Using sql server 2008's merge statement tech republic
 
Maximize Big Data ROI via Best of Breed Patterns and Practices
Maximize Big Data ROI via Best of Breed Patterns and PracticesMaximize Big Data ROI via Best of Breed Patterns and Practices
Maximize Big Data ROI via Best of Breed Patterns and Practices
 
BigQuery for Beginners
BigQuery for BeginnersBigQuery for Beginners
BigQuery for Beginners
 
Why Data Virtualization? An Introduction
Why Data Virtualization? An IntroductionWhy Data Virtualization? An Introduction
Why Data Virtualization? An Introduction
 
The Big Picture: Big Data for the New Wave of Analytics
The Big Picture: Big Data for the New Wave of AnalyticsThe Big Picture: Big Data for the New Wave of Analytics
The Big Picture: Big Data for the New Wave of Analytics
 
All Grown Up: Maturation of Analytics in the Cloud
All Grown Up: Maturation of Analytics in the CloudAll Grown Up: Maturation of Analytics in the Cloud
All Grown Up: Maturation of Analytics in the Cloud
 
Denodo Datafest 2017 London Tekin Mentes Logitech
Denodo Datafest 2017 London Tekin Mentes LogitechDenodo Datafest 2017 London Tekin Mentes Logitech
Denodo Datafest 2017 London Tekin Mentes Logitech
 
My cool new Slideshow!
My cool new Slideshow!My cool new Slideshow!
My cool new Slideshow!
 
Secrets of Enterprise Data Mining: SQL Saturday 328 Birmingham AL
Secrets of Enterprise Data Mining: SQL Saturday 328 Birmingham ALSecrets of Enterprise Data Mining: SQL Saturday 328 Birmingham AL
Secrets of Enterprise Data Mining: SQL Saturday 328 Birmingham AL
 
¿Cómo modernizar una arquitectura de TI con la virtualización de datos?
¿Cómo modernizar una arquitectura de TI con la virtualización de datos?¿Cómo modernizar una arquitectura de TI con la virtualización de datos?
¿Cómo modernizar una arquitectura de TI con la virtualización de datos?
 
Using hash fields in sql server tech republic
Using hash fields in sql server   tech republicUsing hash fields in sql server   tech republic
Using hash fields in sql server tech republic
 
Left Brain, Right Brain: How to Unify Enterprise Analytics
Left Brain, Right Brain: How to Unify Enterprise AnalyticsLeft Brain, Right Brain: How to Unify Enterprise Analytics
Left Brain, Right Brain: How to Unify Enterprise Analytics
 
IBM Cloud pak for data brochure
IBM Cloud pak for data   brochureIBM Cloud pak for data   brochure
IBM Cloud pak for data brochure
 
Data Virtualization for Data Architects (New Zealand)
Data Virtualization for Data Architects (New Zealand)Data Virtualization for Data Architects (New Zealand)
Data Virtualization for Data Architects (New Zealand)
 
Data Mining With Excel 2007 And SQL Server 2008
Data Mining With Excel 2007 And SQL Server 2008Data Mining With Excel 2007 And SQL Server 2008
Data Mining With Excel 2007 And SQL Server 2008
 
catfx Datasheet_v1
catfx Datasheet_v1catfx Datasheet_v1
catfx Datasheet_v1
 
DBT ELT approach for Advanced Analytics.pptx
DBT ELT approach for Advanced Analytics.pptxDBT ELT approach for Advanced Analytics.pptx
DBT ELT approach for Advanced Analytics.pptx
 

More from Kaing Menglieng

What is your sql server backup strategy tech_republic
What is your sql server backup strategy    tech_republicWhat is your sql server backup strategy    tech_republic
What is your sql server backup strategy tech_republicKaing Menglieng
 
Using object dependencies in sql server 2008 tech republic
Using object dependencies in sql server 2008   tech republicUsing object dependencies in sql server 2008   tech republic
Using object dependencies in sql server 2008 tech republicKaing Menglieng
 
Understand when to use user defined functions in sql server tech-republic
Understand when to use user defined functions in sql server   tech-republicUnderstand when to use user defined functions in sql server   tech-republic
Understand when to use user defined functions in sql server tech-republicKaing Menglieng
 
Sql server indexed views speed up your select queries part 1 - code-projec
Sql server indexed views   speed up your select queries  part 1 - code-projecSql server indexed views   speed up your select queries  part 1 - code-projec
Sql server indexed views speed up your select queries part 1 - code-projecKaing Menglieng
 
Sql server – query optimization – remove bookmark lookup – remove rid lookup
Sql server – query optimization – remove bookmark lookup – remove rid lookupSql server – query optimization – remove bookmark lookup – remove rid lookup
Sql server – query optimization – remove bookmark lookup – remove rid lookupKaing Menglieng
 
Sql server common interview questions and answers
Sql server   common interview questions and answersSql server   common interview questions and answers
Sql server common interview questions and answersKaing Menglieng
 
Sql server common interview questions and answers page 6
Sql server   common interview questions and answers page 6Sql server   common interview questions and answers page 6
Sql server common interview questions and answers page 6Kaing Menglieng
 
Sql server common interview questions and answers page 5
Sql server   common interview questions and answers page 5Sql server   common interview questions and answers page 5
Sql server common interview questions and answers page 5Kaing Menglieng
 
Sql server common interview questions and answers page 4
Sql server   common interview questions and answers page 4Sql server   common interview questions and answers page 4
Sql server common interview questions and answers page 4Kaing Menglieng
 
Sql server common interview questions and answers page 2
Sql server   common interview questions and answers page 2Sql server   common interview questions and answers page 2
Sql server common interview questions and answers page 2Kaing Menglieng
 
Sql server – 2008 – hardware and software requirements for installing sql se
Sql server – 2008 – hardware and software requirements for installing sql seSql server – 2008 – hardware and software requirements for installing sql se
Sql server – 2008 – hardware and software requirements for installing sql seKaing Menglieng
 
Speeding up queries with semi joins and anti-joins
Speeding up queries with semi joins and anti-joinsSpeeding up queries with semi joins and anti-joins
Speeding up queries with semi joins and anti-joinsKaing Menglieng
 
Reviewing sql server permissions tech republic
Reviewing sql server permissions   tech republicReviewing sql server permissions   tech republic
Reviewing sql server permissions tech republicKaing Menglieng
 
Query optimization how to search millions of record in sql table faster -
Query optimization   how to search millions of record in sql table faster  -Query optimization   how to search millions of record in sql table faster  -
Query optimization how to search millions of record in sql table faster -Kaing Menglieng
 
New date datatypes in sql server 2008 tech republic
New date datatypes in sql server 2008   tech republicNew date datatypes in sql server 2008   tech republic
New date datatypes in sql server 2008 tech republicKaing Menglieng
 
Introduction to policy based management in sql server 2008 tech-republic
Introduction to policy based management in sql server 2008   tech-republicIntroduction to policy based management in sql server 2008   tech-republic
Introduction to policy based management in sql server 2008 tech-republicKaing Menglieng
 
Introduction to change data capture in sql server 2008 tech republic
Introduction to change data capture in sql server 2008   tech republicIntroduction to change data capture in sql server 2008   tech republic
Introduction to change data capture in sql server 2008 tech republicKaing Menglieng
 
How to import an excel file into sql server 2005 using integration services
How to import an excel file into sql server 2005 using integration services How to import an excel file into sql server 2005 using integration services
How to import an excel file into sql server 2005 using integration services Kaing Menglieng
 
How do i... query foreign data using sql server's linked servers tech_repu
How do i... query foreign data using sql server's linked servers    tech_repuHow do i... query foreign data using sql server's linked servers    tech_repu
How do i... query foreign data using sql server's linked servers tech_repuKaing Menglieng
 

More from Kaing Menglieng (20)

What is your sql server backup strategy tech_republic
What is your sql server backup strategy    tech_republicWhat is your sql server backup strategy    tech_republic
What is your sql server backup strategy tech_republic
 
Using object dependencies in sql server 2008 tech republic
Using object dependencies in sql server 2008   tech republicUsing object dependencies in sql server 2008   tech republic
Using object dependencies in sql server 2008 tech republic
 
Understand when to use user defined functions in sql server tech-republic
Understand when to use user defined functions in sql server   tech-republicUnderstand when to use user defined functions in sql server   tech-republic
Understand when to use user defined functions in sql server tech-republic
 
Sql server indexed views speed up your select queries part 1 - code-projec
Sql server indexed views   speed up your select queries  part 1 - code-projecSql server indexed views   speed up your select queries  part 1 - code-projec
Sql server indexed views speed up your select queries part 1 - code-projec
 
Sql server – query optimization – remove bookmark lookup – remove rid lookup
Sql server – query optimization – remove bookmark lookup – remove rid lookupSql server – query optimization – remove bookmark lookup – remove rid lookup
Sql server – query optimization – remove bookmark lookup – remove rid lookup
 
Sql server common interview questions and answers
Sql server   common interview questions and answersSql server   common interview questions and answers
Sql server common interview questions and answers
 
Sql server common interview questions and answers page 6
Sql server   common interview questions and answers page 6Sql server   common interview questions and answers page 6
Sql server common interview questions and answers page 6
 
Sql server common interview questions and answers page 5
Sql server   common interview questions and answers page 5Sql server   common interview questions and answers page 5
Sql server common interview questions and answers page 5
 
Sql server common interview questions and answers page 4
Sql server   common interview questions and answers page 4Sql server   common interview questions and answers page 4
Sql server common interview questions and answers page 4
 
Sql server common interview questions and answers page 2
Sql server   common interview questions and answers page 2Sql server   common interview questions and answers page 2
Sql server common interview questions and answers page 2
 
Sql server – 2008 – hardware and software requirements for installing sql se
Sql server – 2008 – hardware and software requirements for installing sql seSql server – 2008 – hardware and software requirements for installing sql se
Sql server – 2008 – hardware and software requirements for installing sql se
 
Speeding up queries with semi joins and anti-joins
Speeding up queries with semi joins and anti-joinsSpeeding up queries with semi joins and anti-joins
Speeding up queries with semi joins and anti-joins
 
Speed up sql
Speed up sqlSpeed up sql
Speed up sql
 
Reviewing sql server permissions tech republic
Reviewing sql server permissions   tech republicReviewing sql server permissions   tech republic
Reviewing sql server permissions tech republic
 
Query optimization how to search millions of record in sql table faster -
Query optimization   how to search millions of record in sql table faster  -Query optimization   how to search millions of record in sql table faster  -
Query optimization how to search millions of record in sql table faster -
 
New date datatypes in sql server 2008 tech republic
New date datatypes in sql server 2008   tech republicNew date datatypes in sql server 2008   tech republic
New date datatypes in sql server 2008 tech republic
 
Introduction to policy based management in sql server 2008 tech-republic
Introduction to policy based management in sql server 2008   tech-republicIntroduction to policy based management in sql server 2008   tech-republic
Introduction to policy based management in sql server 2008 tech-republic
 
Introduction to change data capture in sql server 2008 tech republic
Introduction to change data capture in sql server 2008   tech republicIntroduction to change data capture in sql server 2008   tech republic
Introduction to change data capture in sql server 2008 tech republic
 
How to import an excel file into sql server 2005 using integration services
How to import an excel file into sql server 2005 using integration services How to import an excel file into sql server 2005 using integration services
How to import an excel file into sql server 2005 using integration services
 
How do i... query foreign data using sql server's linked servers tech_repu
How do i... query foreign data using sql server's linked servers    tech_repuHow do i... query foreign data using sql server's linked servers    tech_repu
How do i... query foreign data using sql server's linked servers tech_repu
 

Filtered indexes in sql server 2008 tech republic

  • 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. 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) Curiosity's 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 pro's SET @i = @i + 1 advice END 2 windows 7 won't 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 a http://www.techrepublic.com/blog/datacenter/filtered-indexes-in-sql-server-2008/490[08/29/2012 3:48:09 PM]
  • 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 NULL http://www.techrepublic.com/blog/datacenter/filtered-indexes-in-sql-server-2008/490[08/29/2012 3:48:09 PM]
  • 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 Login http://www.techrepublic.com/blog/datacenter/filtered-indexes-in-sql-server-2008/490[08/29/2012 3:48:09 PM]