HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Can I find out which database is queried on the most?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
canthedatabasequeriedfindwhichoutmost

Problem

We have a number of database which all have the same schema for each customer.
I was wondering if there was a query which could select count(*),db_name from somesysview group by db_name;

From a different angle, the same query broken down by user?

Solution

You can use sys.dm_exec_sessions. This will only give you the count based on the current activity. You would need to store this historically if you wanted to trend it. Keep in mind that session ids of 50 or less are system sessions.

--By Database
select 
    db_name(database_id) DatabaseName
    ,count(session_id) as Session_count 
from sys.dm_exec_sessions
where
    session_id > 50
group by
    database_id

--By Login
select 
    login_name
    ,count(session_id) as Session_count 
from sys.dm_exec_sessions
where
    session_id > 50
group by
    login_name

Code Snippets

--By Database
select 
    db_name(database_id) DatabaseName
    ,count(session_id) as Session_count 
from sys.dm_exec_sessions
where
    session_id > 50
group by
    database_id

--By Login
select 
    login_name
    ,count(session_id) as Session_count 
from sys.dm_exec_sessions
where
    session_id > 50
group by
    login_name

Context

StackExchange Database Administrators Q#120748, answer score: 5

Revisions (0)

No revisions yet.