18
Analysing Indexes SQLBits 6 th October 2007 © Colin Leversuch-Roberts Kelem Consulting Limited September 2007

Analysing Indexes SQLBits 6 th October 2007 © Colin Leversuch-Roberts Kelem Consulting Limited September 2007

Embed Size (px)

Citation preview

Page 1: Analysing Indexes SQLBits 6 th October 2007 © Colin Leversuch-Roberts Kelem Consulting Limited September 2007

Analysing Indexes

SQLBits 6th October 2007

© Colin Leversuch-Roberts Kelem Consulting Limited September 2007

Page 2: Analysing Indexes SQLBits 6 th October 2007 © Colin Leversuch-Roberts Kelem Consulting Limited September 2007

Who am I ?

© Colin Leversuch-Roberts Kelem Consulting Limited September 2007

Colin Leversuch-Roberts

aka

Grumpy Old DBA

Kelem Consulting Limited

www.kelemconsulting.co.uk

http://sqlblogcasts.com/blogs/grumpyolddba/default.aspx

Page 3: Analysing Indexes SQLBits 6 th October 2007 © Colin Leversuch-Roberts Kelem Consulting Limited September 2007

Analysing Indexes

 

• How to gather the statistical information about database indexes

• What to do with it

• How to use it to your advantage

• How big is that index

• Missing Indexes

© Colin Leversuch-Roberts Kelem Consulting Limited September 20073

Page 4: Analysing Indexes SQLBits 6 th October 2007 © Colin Leversuch-Roberts Kelem Consulting Limited September 2007

Analysing Indexes

WHY ? •Documentation

•Controlled environments

•Change Requests

•Change Advisory Board Meetings

•Urban Myths and would be DBA’s

© Colin Leversuch-Roberts Kelem Consulting Limited September 20074

Page 5: Analysing Indexes SQLBits 6 th October 2007 © Colin Leversuch-Roberts Kelem Consulting Limited September 2007

Analysing Indexes• No real demo’s

• My laptop can’t process a 400 million row table live

• Background

• ERP System , nearly 8000 indexes

• Would you believe how this application works?

© Colin Leversuch-Roberts Kelem Consulting Limited September 20075

Page 6: Analysing Indexes SQLBits 6 th October 2007 © Colin Leversuch-Roberts Kelem Consulting Limited September 2007

Analysing Indexes

Some typical tables

© Colin Leversuch-Roberts Kelem Consulting Limited September 20076

Page 7: Analysing Indexes SQLBits 6 th October 2007 © Colin Leversuch-Roberts Kelem Consulting Limited September 2007

Analysing Indexes

Some typical indexes

All unique ? I think not !

© Colin Leversuch-Roberts Kelem Consulting Limited September 20077

Page 8: Analysing Indexes SQLBits 6 th October 2007 © Colin Leversuch-Roberts Kelem Consulting Limited September 2007

Where to start• System views

• Data not persisted

• We must capture and store this data

Operational cost on indexes ( assuming no RFI )

** ( No partitioning ) **

8 © Colin Leversuch-Roberts Kelem Consulting Limited September 2007

Page 9: Analysing Indexes SQLBits 6 th October 2007 © Colin Leversuch-Roberts Kelem Consulting Limited September 2007

Tools of the trade

9

dbo.sysindexes This is the SQL 2000 system table taken forward as a view.

This table may not be supported going forward.

sys.dm_db_index_physical_stats This is actually a function and replaces dbcc showcontig

sys.indexes 2005 system table contains property information only for each index.

sys.dm_db_index_usage_stats System view which records the access usage of every index in the database

We can view the number of times this index has been used, the type of access and the last access date

sys.dm_db_index_operational_stats System function which records the operational cost of access to the index

We can view information such as the number of pages, locks, latches and waits.

sys.objects Contains information on database objects

sys.dm_db_missing_index_details System view which stores information on indexes the optimiser considers are missing.

sys.dm_db_missing_index_group_stats System view which stores usage and access details for the missing indexes similar to sys.dm_db_index_usage_stats

We can also view the improvement that the index is computed to make, this is essentially the same type of output as shown by the Tuning Advisor.

sys.dm_db_missing_index_groups Presumably this view will be of more use in SQL2008

sys.dm_db_missing_index_columns A system function which returns the columns for a missing index

Requires the index handle as a

parameter

© Colin Leversuch-Roberts Kelem Consulting Limited September 2007

Page 10: Analysing Indexes SQLBits 6 th October 2007 © Colin Leversuch-Roberts Kelem Consulting Limited September 2007

sys.dm_db_index_usage_stats

• Tells us how the index is used

• Counts scans, seeks, lookups and writes

• Values are cumulative, so not so useful

• Contains last date of of each however

10 © Colin Leversuch-Roberts Kelem Consulting Limited September 2007

Page 11: Analysing Indexes SQLBits 6 th October 2007 © Colin Leversuch-Roberts Kelem Consulting Limited September 2007

sys.dm_db_index_operational_stats

• Contains a wealth of information

• Page splits, waits, access at each index level

• Data is not maintained

• Not a view

• Need to snapshot the data

© Colin Leversuch-Roberts Kelem Consulting Limited September 200711

Page 12: Analysing Indexes SQLBits 6 th October 2007 © Colin Leversuch-Roberts Kelem Consulting Limited September 2007

Some sample outputs

© Colin Leversuch-Roberts Kelem Consulting Limited September 2007

Page 13: Analysing Indexes SQLBits 6 th October 2007 © Colin Leversuch-Roberts Kelem Consulting Limited September 2007

sys.dm_db_index_physical_stats

• Actually a function

• Find the size of your index

• Use with care on large tables

• Sadly not table valued so difficult to join

• Roll your own

© Colin Leversuch-Roberts Kelem Consulting Limited September 200713

Page 14: Analysing Indexes SQLBits 6 th October 2007 © Colin Leversuch-Roberts Kelem Consulting Limited September 2007

All about granularity

• Finding unused indexes

• Dealing with period end processes

• How often do I collect data and how much

• Little used indexes

• Seeks and lookups

• Making sense of it all

14© Colin Leversuch-Roberts Kelem Consulting Limited September 2007

Page 15: Analysing Indexes SQLBits 6 th October 2007 © Colin Leversuch-Roberts Kelem Consulting Limited September 2007

Index size

• A secondary index adds the clustered index

• Wide clustered indexes make for wide secondary indexes

• Wide indexes take space

• Wide indexes produce more io

• Wide indexes may be considered less cost effective by the optimiser

© Colin Leversuch-Roberts Kelem Consulting Limited September 200715

Page 16: Analysing Indexes SQLBits 6 th October 2007 © Colin Leversuch-Roberts Kelem Consulting Limited September 2007

Missing Indexes

• Also available through the performance dashboard

• Lots of information

• Much as you receive from the DTA

• Simple query to extract basic information

• Don’t believe it all

© Colin Leversuch-Roberts Kelem Consulting Limited September 200716

Page 17: Analysing Indexes SQLBits 6 th October 2007 © Colin Leversuch-Roberts Kelem Consulting Limited September 2007

And to finish

• Queries to check for duplicated indexes coming in part 5 on my blog

• Query / Report to show top indexes in cache

• Query / report to analyse plans in cache

© Colin Leversuch-Roberts Kelem Consulting Limited September 200717

Page 18: Analysing Indexes SQLBits 6 th October 2007 © Colin Leversuch-Roberts Kelem Consulting Limited September 2007

More information

[email protected]

• http://sqlblogcasts.com/blogs/grumpyolddba/default.aspx

• Coming soon www.grumpyolddba.co.uk

• www.sqlserverfaq.com

• Available for consultancy

18

Thankyou for listening

© Colin Leversuch-Roberts Kelem Consulting Limited September 2007