Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

Search

XML Feeds

Google Ads

« Good SQL Articles To Read If You Can't Afford BooksFind the last Backup taken in SQL Server »
comments
Rate Post:
submit to reddit Digg!FacebookDotnetkicks

The selectivity of an index is extremely important. If your index is not selective enough then the optimizer will simply have to do a scan. This is also a reason why creating an index on a gender column does not make a lot of sense.

First create this table

  1. USE tempdb
  2. go
  3.  
  4. CREATE TABLE TestCompositeIndex (STATE CHAR(2),Zip CHAR(5))
  5. INSERT TestCompositeIndex VALUES('NJ','08540')
  6. INSERT TestCompositeIndex VALUES('NJ','08540')
  7. INSERT TestCompositeIndex VALUES('NY','10028')
  8. INSERT TestCompositeIndex VALUES('NY','10021')
  9. INSERT TestCompositeIndex VALUES('NY','10021')
  10. INSERT TestCompositeIndex VALUES('NY','10021')
  11. INSERT TestCompositeIndex VALUES('NY','10001')
  12. INSERT TestCompositeIndex VALUES('NJ','08536')
  13. INSERT TestCompositeIndex VALUES('NJ','08540')

If you have a composite index (composite means the index contains more than one column) you need to run this code.

  1. DECLARE @COUNT INT
  2.  
  3. SELECT DISTINCT STATE, Zip
  4. FROM TestCompositeIndex;
  5.  
  6. SET @COUNT = @@ROWCOUNT;
  7.  
  8.  
  9.  
  10. SELECT (@COUNT*1.0) / COUNT(*) AS IndexSelectivity,
  11. COUNT(*)AS TotalCount,
  12. @COUNT AS DistinctCount
  13. FROM TestCompositeIndex;

Result
--------
IndexSelectivity TotalCount DistinctCount
.555555555555 9 5

If you have a one column index you can use this code

  1. SELECT (COUNT(DISTINCT STATE)* 1.0) / COUNT(*) AS IndexSelectivity,
  2. COUNT(*) AS TotalCount,
  3. COUNT(DISTINCT STATE) AS DistinctCount
  4. FROM TestCompositeIndex;

Result
--------
IndexSelectivity TotalCount DistinctCount
.222222222222 9 2

About the Author

User bio imageDenis has been working with SQL Server since version 6.5. Although he worked as an ASP/JSP/ColdFusion developer before the dot com bust, he has been working exclusively as a database developer/architect since 2002. In addition to English, Denis is also fluent in Croatian and Dutch, but he can curse in many other languages and dialects (just ask the SQL optimizer) He lives in Princeton, NJ with his wife and three kids.
Social SitingsTwitterFacebookLinkedInHomePageLTD RSS Feed
730 views
submit to reddit Digg!FacebookDotnetkicks

Comments and Feedback

No feedback yet

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)