Query index usage SQL

Source: Internet
Author: User

Select

Sch. Name +
'.' + T. Name
As [Table name],

I. Name as [index name],

I. type_desc,

Isnull (user_updates, 0)
As [total writes],

Isnull (user_seeks
+ User_scans + user_lookups, 0)
As [total reads],

S. last_user_seek,

S. last_user_scan
,

S. last_user_lookup,

Isnull (user_updates, 0)
-Isnull (user_seeks + user_scans
+ User_lookups), 0) as [difference],

P. reserved_page_count
* 8.0/1024 as spaceinmb

From SYS. Indexes
As I with (nolock)

Left outerjoin
SYS. dm_db_index_usage_statsas s
With (nolock)
On S. object_id
= I. object_id
And I. index_id
= S. index_id and S. database_id = db_id ()
And objectproperty (S. object_id, 'isusertable ')
= 1

Inner join
SYS. Tables
As T with (nolock)
On I. object_id
= T. object_id

Inner join
SYS. Schemas
As Sch
(Nolock)
On T. schema_id
= Sch. schema_id

Left outerjoin
SYS. dm_db_partition_stats
As P with (nolock)
On I. index_id
= P. index_id and I. object_id
= P. object_id

Where (1 = 1)

-- And isnull (user_updates, 0)> = isnull (user_seeks + user_scans + user_lookups), 0) -- shows all indexesincluding those that have not been used

-- And isnull (user_updates, 0)-isnull (user_seeks + user_scans + user_lookups), 0)> 0 -- only shows thoseindexes which have been used

-- And I. index_id> 1 -- only non-first indexes (I. e. non-primary key)

-- And I. is_primary_key <> 1 -- only those that are not defined ASA primary key)

-- And I. is_unique_constraint <> 1 -- only those that are not classed as "uniqueconstraints ".

Order by [Table name], [index name]

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.