Skip to main content
Our Tech Ideas

Building SQL Server Database Inventory

As a Database Administrator (DBA), maintaining a well-documented inventory of your SQL Server databases is crucial for effective management, security, and performance optimization. In this blog, we’ll discuss the essential information to include in your SQL Server database inventory and how to gather this data efficiently.

Why Create a Database Inventory?

A comprehensive database inventory serves several purposes:

  • Operational Efficiency: Streamline management tasks by having critical information at your fingertips.
  • Performance Monitoring: Identify performance bottlenecks and trends over time.
  • Compliance and Security: Ensure adherence to regulations and maintain data integrity.
  • Disaster Recovery Planning: Prepare for unexpected outages with a clear understanding of your database environment.

Essential Components of a SQL Server Database Inventory

1. Server Information

What to Include:

  • Server Name: FQDN and aliases.
  • Operating System: Version and edition.
  • Physical/Virtual Status: Indicate if the server is physical or virtual.

How to Obtain: You can use the following SQL command to gather server details:

SELECT SERVERPROPERTY('MachineName') AS ServerName,
       SERVERPROPERTY('Edition') AS Edition,
       SERVERPROPERTY('ProductVersion') AS ProductVersion,
       SERVERPROPERTY('IsClustered') AS IsClustered;
SQL

2. Database Instance Details

What to Include:

  • SQL Server Version: Version number and edition (e.g., Standard, Enterprise).
  • Instance Name: Default vs. named instances.
  • Service Packs and Updates: Installed updates and their dates.

How to Obtain: Run:

SELECT @@VERSION AS Version;
SQL

3. Database Information

What to Include:

  • Database Name: Names of all databases.
  • Size and Growth: Current size, growth trends, and auto-growth settings.

How to Obtain: Use:

SELECT db.name AS DatabaseName,
       SUM(mf.size) / 128.0 AS SizeMB,
       db.state_desc AS State
FROM sys.databases db
JOIN sys.master_files mf ON db.database_id = mf.database_id
GROUP BY db.name, db.state_desc;
SQL

4. Security and Access

What to Include:

  • User Accounts and Roles: List of users and permissions.
  • Authentication Mode: Windows or SQL Server Authentication.

How to Obtain: Run:

SELECT name AS UserName,
       type_desc AS UserType
FROM sys.database_principals
WHERE type IN ('S', 'U');
SQL

5. Backup and Recovery

What to Include:

  • Backup Strategy: Frequency and type of backups.
  • Last Backup Date: Last successful backups for each database.

How to Obtain: Check:

SELECT database_name,
       backup_finish_date,
       type
FROM msdb.dbo.backupset
ORDER BY backup_finish_date DESC;
SQL

6. Performance Metrics

What to Include:

  • CPU and Memory Usage: Average utilization metrics.
  • Index Usage Statistics: Fragmentation levels and last rebuild date.

How to Obtain: For index statistics:

SELECT object_name(i.object_id) AS TableName,
       i.name AS IndexName,
       dm_ius.last_user_seek,
       dm_ius.last_user_scan,
       dm_ius.user_seeks,
       dm_ius.user_scans
FROM sys.indexes AS i
JOIN sys.dm_db_index_usage_stats AS dm_ius
ON i.object_id = dm_ius.object_id;
SQL

7. Maintenance Plans

What to Include:

  • Scheduled Jobs: Details of all SQL Server Agent jobs.
  • Maintenance Task History: History of maintenance task execution.

How to Obtain: Run:

SELECT j.name AS JobName,
       j.enabled,
       MAX(h.run_date) AS LastRunDate,
       MAX(h.run_status) AS LastRunOutcome
FROM msdb.dbo.sysjobs j
LEFT JOIN msdb.dbo.sysjobhistory h ON j.job_id = h.job_id
GROUP BY j.name, j.enabled;
SQL

8. Networking Information

What to Include:

  • Network Protocols: Enabled protocols (TCP/IP, Named Pipes).
  • Firewall Rules: Any configured firewall settings impacting connectivity.

How to Obtain: Check SQL Server Configuration Manager for protocols and rules.

9. Monitoring and Alerts

What to Include:

  • Monitoring Tools: Tools used for monitoring (e.g., SolarWinds, Redgate).
  • Alert Configuration: Configured alerts for critical events.

How to Obtain: Check:

SELECT name AS AlertName,
       enabled
FROM msdb.dbo.sysalerts;
SQL

10. Hardware Information

What to Include:

  • CPU Count and Type: Number of CPUs and specifications.
  • RAM and Storage Configuration: Total memory and storage usage.

How to Obtain: Use:

EXEC sp_configure 'show advanced options', 1;  
RECONFIGURE;  
EXEC sp_configure;
SQL

11. Change History

What to Include:

  • Schema Changes: Log of significant schema changes.
  • Configuration Changes: Changes to server or database configurations.

How to Obtain: Maintain a change log manually or through a version control system.

12. Licensing Information

What to Include:

  • License Type and Key: Details about the license type and key.
  • Support Contracts: Information about support contracts or agreements.

How to Obtain: Refer to your licensing documentation or vendor agreements.

13. Compliance and Auditing

What to Include:

  • Compliance Standards: Standards adhered to (GDPR, HIPAA, etc.).
  • Audit Trail: Mechanisms in place for auditing changes and access.

How to Obtain: Review compliance documentation and audit policies.

14. Future Planning

What to Include:

  • Capacity Planning: Estimates for future growth and resource requirements.
  • Upgrade Plans: Scheduled or planned upgrades to SQL Server versions.

How to Obtain: Conduct regular assessments and gather input from stakeholders.

15. Documentation

What to Include:

  • Architecture Diagrams: Visual representation of your database architecture.
  • Runbooks: Procedures for common tasks and troubleshooting steps.

How to Obtain: Create and maintain documentation through team collaboration tools.

Conclusion

Maintaining a comprehensive SQL Server database inventory is a vital aspect of effective database administration. By systematically collecting and organizing the necessary information, you can ensure better performance, security, and compliance for your SQL Server environment. Regularly review and update your inventory to adapt to changes in your database landscape.

By leveraging SQL queries and utilizing appropriate tools, you can streamline the process of gathering and maintaining this critical information. Remember, an organized inventory not only saves time but also enhances your ability to manage your databases efficiently.