Query system views in RDS for SQL Server

Updated at:

This topic describes how to query common system views in ApsaraDB RDS for SQL Server for daily management and maintenance.

Note
  • Before performing risky operations, such as modifying an instance or its data, ensure that the instance has adequate disaster recovery and fault tolerance capabilities.

  • Before you modify the configuration or data of an instance, such as an ECS or RDS instance, create a snapshot or enable log backup.

  • If you have authorized or submitted sensitive information such as account credentials on the Alibaba Cloud platform, we recommend that you change them promptly.

Procedure

  1. Connect to the instance using a client. For more information, see Connect to an ApsaraDB RDS for SQL Server instance.

  2. Run the following SQL statements to view system parameter configurations.

    USE [$DB_Name];
    SELECT * FROM sys.configurations;
    Note

    [$DB_Name] is the name of the database. For detailed parameter descriptions, see sys.configurations.

    The query returns a table of system configuration items. The table includes columns such as configuration_id, name, value, minimum, maximum, value_in_use, description, is_dynamic, and is_advanced. The results show the current values and value ranges for various configuration items, such as recovery interval (min), user connections, and max worker threads.

  3. Run the following SQL statements to view database file information.

    USE [$DB_Name];
    SELECT * FROM sys.sysfiles;

    The output is similar to the following:

    fileid  groupid  size  maxsize    growth  status  perf  name        filename
    1       1        1024  -1         8192    2       0     zht01       E:\SQLDATA\DATA\zht01.mdf
    2       0        1024  268435456  8192    66      0     zht01_log   E:\SQLDATA\DATA\zht01_log.ldf
  4. Run the following SQL statement to view the size of database files.

    SELECT name, CONVERT(float,size) * (8192.0/1024.0)/1024 AS Size_MB,* from [$DB_Name].dbo.sysfiles;

    The query result displays two records for the zht01 database: one for the data file (zht01.mdf, with a Size_MB of 8) and one for the log file (zht01_log.ldf, with a Size_MB of 8). The result includes fields such as name, Size_MB, fileid, groupid, size, maxsize, growth, status, and filename.

  5. Run the following SQL statement to view I/O statistics for database files.

    SELECT * FROM sys.dm_io_virtual_file_stats(DB_ID('[$DB_Name]'), [$File_ID]);
    Note

    [$File_ID] is the fileid obtained in Step 3.

    The query result includes columns such as database_id, file_id, sample_ms, num_of_reads, num_of_bytes_read, io_stall_read_ms, io_stall_queued_read_ms, num_of_writes, num_of_bytes_written, io_stall_write_ms, io_stall_queued_write_ms, io_stall, size_on_disk_bytes, and file_handle. These columns show metrics for the database file, such as the number of reads and writes, bytes read and written, and I/O latency.

  6. Run the following SQL statement to view all open transactions and their corresponding SQL statements.

    SELECT DB_NAME(dbid) AS DBNAME, 
    (SELECT text FROM sys.dm_exec_sql_text(sql_handle)) AS SQLSTATEMENT 
    FROM master..sysprocesses WHERE open_tran > 0

    The query result includes the DBNAME and SQLSTATEMENT columns. In the example output, the values in the SQLSTATEMENT column are begin tran select * from sys.sysfilegroups and begin tran select * from sys.sysfiles. This indicates that open transactions exist in the corresponding database.

  7. Run the following SQL statement to view data and index fragmentation.

    DBCC SHOWCONTIG;

    The command returns output similar to the following, which shows fragmentation information for the data and indexes of the specified table or view. For more information about the parameters, see DBCC SHOWCONTIG.

    DBCC SHOWCONTIG scanning 'ExampleTable' table...
    Table: 'ExampleTable' (581577110); index ID: 1, database ID: 5
    TABLE level scan performed.
    - Pages Scanned................................: 4
    - Extents Scanned..............................: 1
    - Extent Switches..............................: 0
    - Avg. Pages per Extent........................: 4.0
    - Scan Density [Best Count:Actual Count].......: 100.00% [1:1]
    - Logical Scan Fragmentation ..................: 25.00%
    - Extent Scan Fragmentation ...................: 0.00%
    - Avg. Bytes Free per Page.....................: 872.8
    - Avg. Page Density (full).....................: 89.22%
    DBCC SHOWCONTIG scanning 'QueryNotificationErrorsQueue' table...
    Table: 'QueryNotificationErrorsQueue' (1977058079); index ID: 1, database ID: 5
    TABLE level scan performed.
    - Pages Scanned................................: 0
  8. Run the following SQL statement to view index fragmentation in the database.

    SELECT * FROM sys.dm_db_index_physical_stats(DB_ID(N'[$DB_Name]'),NULL,NULL,NULL,DEFAULT);

    The query result returns fragmentation information for each index. Key columns include database_id, object_id, index_id, index_type_desc (index type, such as CLUSTERED INDEX or NONCLUSTERED INDEX), avg_fragmentation_in_percent (average fragmentation percentage), fragment_count (number of fragments), and page_count (number of pages). Pay close attention to the avg_fragmentation_in_percent column. A high value in this column, for example, 25%, indicates a high fragmentation rate, which means you should rebuild or reorganize the index.

  9. Run the following SQL statement to view recently executed statements.

    SELECT
        p.spid, p.status, p.hostname, p.loginame, p.cpu, r.start_time, r.command,
        p.program_name, text
    FROM
        sys.dm_exec_requests AS r,
        master.dbo.sysprocesses AS p
        CROSS APPLY sys.dm_exec_sql_text(p.sql_handle)
    WHERE
        p.status NOT IN ('sleeping', 'background')
    AND r.session_id = p.spid

    The query returns a list of active, non-idle sessions. Each row provides details about a session, including its process ID, running status (such as suspended or runnable), hostname, login name, CPU time, start time, command type, source program name, and the SQL text being executed.