Effective Indexes

10
Effective Indexes For Beginners

description

Effective Indexes for beginners

Transcript of Effective Indexes

Page 1: Effective Indexes

Effective Indexes For Beginners

Page 2: Effective Indexes

Performance is slow

Let’s add another index!

Page 3: Effective Indexes

Ineffective Indexes

• Insert, Update and Delete operations will be slower if you have too many indexes on your table

• Indexes are stored on the disk, the more indexes you have, the more disk space you use

Page 4: Effective Indexes

sys.dm_db_index_usage_stats

• Counts of different types of index operations

• Time each type of operation last performed.

• Requires VIEW SERVER STATE permission

• Counters reset to empty when SQL Server Service is restarted

• If the database is detached or shutdown, all rows associated with that DB are removed

• Information gathered is real-time

Page 5: Effective Indexes

sys.dm_db_index_usage_stats

Column Name Column Name

database_id last_user_update

object_id system_seeks

index_id system_scans

user_seeks system_lookups

user_scans system_updates

user_lookups last_system_seek

user_updates last_system_scan

last_user_seek last_system_lookup

last_user_scan last_system_update

last_user_lookup

Page 6: Effective Indexes

On to the code…

• Pick a table and view its indexes• Take a look at the current usage • Run code that targets each index and

see when the index was last used• Recheck the usage• View index usage for entire database• How to disable an index• How to enable an index• How to drop an index

Page 7: Effective Indexes

Demo...

Page 8: Effective Indexes

Conclusion• Use sys.dm_db_index_usage_stats

DMV to easily identify real-time index usage stats

• Identify indexes that are rarely or never used

• Disable for a time before dropping

• If disabled indexes aren’t missed – drop them to save disk space

Page 9: Effective Indexes

Resources• Click on the Word icon to the right to access the SQL

code used in the practical element of this session.

• Download AdventureWorks2008 DBs for free here: http://tinyurl.com/c6y6a3

• BOL sys.dm_db_index_usage_stats information page: http://tinyurl.com/qpfa4d

• Useful blog article on managing indexes at a more detailed level: http://tinyurl.com/38fb5v

• Blog: http://nzgirlgeek.blogspot.com• Web: www.aucklandsql.com

www.girlgeekdinners.co.nz• Email: [email protected]

Microsoft Office Word Document