Skip to content

Instantly share code, notes, and snippets.

@alivarzeshi
Last active June 21, 2024 22:02
Show Gist options
  • Save alivarzeshi/c47cd9f56bab433e3fe960040c83a363 to your computer and use it in GitHub Desktop.
Save alivarzeshi/c47cd9f56bab433e3fe960040c83a363 to your computer and use it in GitHub Desktop.
Date/Time: 2023-06-22 14:00:00
Log Level: Warning
Source: SQL Server
Message: High CPU usage detected. SQL Server is utilizing 90% of the available CPU resources.
Date/Time: 2023-06-22 14:30:00
Log Level: Warning
Source: SQL Server
Message: SQL Server has detected memory pressure. The buffer cache hit ratio is below the recommended threshold.
Date/Time: 2023-06-22 15:00:00
Log Level: Warning
Source: SQL Server
Message: SQL Server has encountered 5 occurrence(s) of I/O requests taking longer than 15 seconds to complete on file [D:\Data\MyDatabase.mdf]. The OS file handle is 0x0000000000000ABC. The offset of the latest long I/O is: 0x0000001234567890.
Date/Time: 2023-06-22 15:30:00
Log Level: Information
Source: SQL Server
Message: Long-running query detected. The query has been executing for more than 120 seconds.
Date/Time: 2023-06-22 16:00:00
Log Level: Error
Source: SQL Server
Message: Transaction (Process ID 55) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.
@alivarzeshi
Copy link
Author

Tip

In-Depth Analysis of Performance and Resource Events in SQL Server Error Logs

Overview

Performance and resource events in SQL Server error logs capture detailed information about the usage and performance of system resources such as memory, CPU, and disk I/O. These logs are crucial for diagnosing performance bottlenecks, understanding resource utilization, and ensuring the SQL Server environment is optimized for efficient operation.

Structure of Performance and Resource Event Log Entries

Each log entry for performance and resource events typically includes the following components:

  1. Date/Time: The exact timestamp when the event occurred.
  2. Log Level: The severity of the event (e.g., Information, Warning, Error).
  3. Source: The component or module that generated the log entry (e.g., SQL Server, Resource Monitor).
  4. Message: A detailed description of the performance or resource event.
  5. Error Number: A unique identifier for any errors encountered during the event.
  6. Additional Data: Information such as the specific resource affected, metrics related to the event, and thresholds.

Common Performance and Resource Events

1. High CPU Usage

  • Description: Indicates that SQL Server or specific queries are consuming a high percentage of CPU resources.
  • Internal Process:
    • SQL Server monitors CPU usage at regular intervals.
    • High CPU usage can be caused by inefficient queries, blocking, or insufficient indexing.
    • The event is logged when CPU usage exceeds a predefined threshold.
  • Example Log Entry:
    Date/Time: 2023-06-22 14:00:00
    Log Level: Warning
    Source: SQL Server
    Message: High CPU usage detected. SQL Server is utilizing 90% of the available CPU resources.
    

2. Memory Pressure

  • Description: Logs events indicating that SQL Server is experiencing memory pressure.
  • Internal Process:
    • SQL Server continuously monitors memory usage, including buffer cache and procedure cache.
    • Memory pressure can occur due to large data operations, suboptimal query plans, or insufficient physical memory.
    • The event is logged when memory usage approaches or exceeds configured thresholds.
  • Example Log Entry:
    Date/Time: 2023-06-22 14:30:00
    Log Level: Warning
    Source: SQL Server
    Message: SQL Server has detected memory pressure. The buffer cache hit ratio is below the recommended threshold.
    

3. Disk I/O Bottlenecks

  • Description: Indicates that SQL Server is experiencing slow disk I/O operations.
  • Internal Process:
    • SQL Server monitors disk I/O performance, including read/write latencies and throughput.
    • Disk I/O bottlenecks can be caused by hardware issues, overloaded storage subsystems, or inefficient data access patterns.
    • The event is logged when disk I/O performance metrics exceed acceptable thresholds.
  • Example Log Entry:
    Date/Time: 2023-06-22 15:00:00
    Log Level: Warning
    Source: SQL Server
    Message: SQL Server has encountered 5 occurrence(s) of I/O requests taking longer than 15 seconds to complete on file [D:\Data\MyDatabase.mdf]. The OS file handle is 0x0000000000000ABC. The offset of the latest long I/O is: 0x0000001234567890.
    

4. Long-Running Queries

  • Description: Logs events related to queries that have been running longer than a predefined threshold.
  • Internal Process:
    • SQL Server tracks the execution time of all running queries.
    • Long-running queries can be indicative of inefficient query plans, lack of indexing, or resource contention.
    • The event is logged when a query exceeds the configured execution time threshold.
  • Example Log Entry:
    Date/Time: 2023-06-22 15:30:00
    Log Level: Information
    Source: SQL Server
    Message: Long-running query detected. The query has been executing for more than 120 seconds.
    

5. Blocking and Deadlocks

  • Description: Indicates that blocking or deadlock situations have been detected, affecting performance.
  • Internal Process:
    • SQL Server monitors lock usage and transaction conflicts.
    • Blocking occurs when one transaction holds a lock on a resource that another transaction is trying to access.
    • Deadlocks occur when two or more transactions are waiting on each other to release locks.
    • The event is logged when blocking or deadlock conditions are detected.
  • Example Log Entry:
    Date/Time: 2023-06-22 16:00:00
    Log Level: Error
    Source: SQL Server
    Message: Transaction (Process ID 55) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.
    

Additional Relevant Information

Error Number Details

  • Error: 701 (Memory Pressure)

    • Severity: 19
    • State: Provides additional context about the memory pressure condition.
    • Description: Indicates that SQL Server has encountered an out-of-memory condition.
  • Error: 17883 (Scheduler Yield)

    • Severity: 10
    • State: Provides details about non-yielding scheduler conditions, often related to CPU issues.
    • Description: Indicates that a scheduler has not yielded within the expected time frame, often due to high CPU usage or thread contention.

Importance of Monitoring Performance and Resource Events

  • Proactive Performance Management: Ensures that performance bottlenecks and resource issues are detected and addressed before they impact end users.
  • Capacity Planning: Helps in understanding resource utilization patterns, enabling better capacity planning and resource allocation.
  • Troubleshooting: Provides detailed insights into performance issues, facilitating quicker diagnosis and resolution.

Background Processes and Mechanisms

Resource Monitoring

  • Dynamic Management Views (DMVs): SQL Server uses DMVs to track and report on resource usage, such as sys.dm_os_wait_stats, sys.dm_exec_requests, and sys.dm_io_virtual_file_stats.
  • Resource Governor: Can be configured to manage and limit resource usage by different workloads, helping to prevent resource contention and performance degradation.

Performance Tuning

  • Indexing: Proper indexing strategies help reduce CPU and I/O load by optimizing query execution plans.
  • Query Optimization: SQL Server's query optimizer generates execution plans that aim to minimize resource usage and improve query performance.
  • Monitoring Tools: Tools like SQL Server Profiler, Extended Events, and Performance Monitor provide detailed performance data and insights.

Efficient Reading of Performance and Resource Logs

  1. Using SQL Server Management Studio (SSMS):

    • Log File Viewer: Access and filter logs using the built-in Log File Viewer.
    • Filtering: Filter logs by date, log level, source, or specific keywords (e.g., "CPU usage", "memory pressure").
  2. Using T-SQL Commands:

    • xp_readerrorlog: Use this system stored procedure to query performance and resource events directly.
      EXEC xp_readerrorlog 0, 1, 'memory pressure';
      EXEC xp_readerrorlog 0, 1, 'CPU usage';
  3. Using Extended Events:

    • Setup: Create an Extended Events session to capture performance and resource-related events.
      CREATE EVENT SESSION [PerformanceEvents] ON SERVER
      ADD EVENT sqlserver.error_reported(
          ACTION(sqlserver.sql_text, sqlserver.database_id, sqlserver.username)
          WHERE ([severity] >= 10))
      ADD TARGET package0.event_file(SET filename=N'PerformanceEvents.xel');
      GO
      ALTER EVENT SESSION [PerformanceEvents] ON SERVER STATE = START;
      GO
  4. Automating Log Monitoring:

    • SQL Server Alerts: Configure SQL Server Agent alerts to notify you of critical performance and resource events.
      USE msdb;
      GO
      EXEC sp_add_alert
          @name = N'High CPU Usage',
          @message_id = 17883,
          @severity = 10,
          @enabled = 1,
          @delay_between_responses = 0,
          @include_event_description_in = 1,
          @notification_message = N'High CPU usage detected.',
          @job_name = N'NotifyDBA';
      GO
      EXEC sp_add_notification
          @alert_name = N'High CPU Usage',
          @operator_name = N'DBA',
          @notification_method = 1;

Conclusion

Performance and resource events in SQL Server logs provide critical insights into the health and efficiency of SQL Server instances. Understanding the structure of these logs, common errors, and the internal processes behind them allows administrators to effectively monitor, troubleshoot, and manage SQL Server performance. Regularly reviewing these logs ensures proactive performance management, efficient resource utilization, and optimal server performance, maintaining the overall reliability and efficiency of the SQL Server environment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment