Skip to content

Instantly share code, notes, and snippets.

@alivarzeshi
Created June 21, 2024 21:35
Show Gist options
  • Save alivarzeshi/daf0981dbb31baded4f5cfb69f705de1 to your computer and use it in GitHub Desktop.
Save alivarzeshi/daf0981dbb31baded4f5cfb69f705de1 to your computer and use it in GitHub Desktop.
Date/Time: 2023-03-03 09:00:00
Log Level: Information
Source: SQL Server
Message: Database 'MyNewDatabase' created successfully.
Database Name: MyNewDatabase
Event ID: 1800
Date/Time: 2023-03-03 10:00:00
Log Level: Information
Source: SQL Server
Message: Database 'OldDatabase' deleted successfully.
Database Name: OldDatabase
Event ID: 3701
Date/Time: 2023-03-03 11:00:00
Log Level: Information
Source: SQL Server
Message: Database 'CriticalDatabase' is transitioning from OFFLINE to ONLINE.
Database Name: CriticalDatabase
Event ID: 9001
Date/Time: 2023-03-03 12:00:00
Log Level: Information
Source: Backup
Message: Backup of database 'ImportantDB' completed successfully. Backup file: '
@alivarzeshi
Copy link
Author

alivarzeshi commented Jun 21, 2024

Tip

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

Overview

Error and Warning Events in SQL Server logs are crucial for identifying issues that need immediate attention to maintain the smooth operation of SQL Server. These logs capture detailed information about problems encountered during SQL Server operations, allowing administrators to troubleshoot and resolve issues effectively.

Structure of Error and Warning Log Entries

Each log entry for errors and warnings typically includes the following components:

  1. Date/Time: The exact timestamp when the error or warning occurred.
  2. Log Level: The severity of the event (Error or Warning).
  3. Source: The component or module that generated the log entry.
  4. Message: A detailed description of the error or warning.
  5. Error Number: A unique identifier for the specific error or warning.
  6. Severity Level: Indicates the seriousness of the error (ranges from 0 to 25).
  7. State: Provides additional context about the error's condition.
  8. Procedure: The name of the stored procedure or batch that was running when the error occurred.
  9. Line Number: The line number within the SQL script where the error occurred.

Common Error and Warning Events

1. Login Failures

  • Description: Occurs when a user fails to authenticate with the SQL Server.
  • Internal Error: Error: 18456
  • Message Example:
    Date/Time: 2023-06-22 10:23:54.32
    Log Level: Error
    Source: SQL Server
    Message: Login failed for user 'username'. Reason: Password did not match that for the login provided. [CLIENT: <local machine>]
    Error Number: 18456
    Severity: 14
    State: 8
    
  • Notes:
    • This error indicates an authentication failure, commonly due to incorrect username or password.
    • The error state provides additional details about the failure reason (e.g., State 8 for incorrect password).

2. Deadlock Alerts

  • Description: Occurs when two or more sessions are waiting for each other to release locks, creating a deadlock situation.
  • Internal Error: Error: 1205
  • Message Example:
    Date/Time: 2023-06-22 10:24:12.45
    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.
    Error Number: 1205
    Severity: 13
    State: 45
    
  • Notes:
    • Deadlocks are automatically resolved by SQL Server by choosing a deadlock victim.
    • This error indicates a need to optimize query patterns or indexing strategies to prevent deadlocks.

3. Severity Level Warnings

  • Description: Indicates potential issues that might not immediately affect SQL Server operations but need attention.
  • Message Example:
    Date/Time: 2023-06-22 10:25:03.78
    Log Level: Warning
    Source: SQL Server
    Message: The SQL Server performance counter 'Buffer cache hit ratio' is below the recommended threshold.
    
  • Notes:
    • Warnings are often less critical than errors but indicate areas that could lead to future problems if not addressed.
    • Regular monitoring of these warnings can help in maintaining optimal performance.

4. Query Timeout Errors

  • Description: Occurs when a query takes longer than the specified timeout period to execute.
  • Internal Error: Error: 1222
  • Message Example:
    Date/Time: 2023-06-22 10:26:54.89
    Log Level: Error
    Source: SQL Server
    Message: Lock request time out period exceeded.
    Error Number: 1222
    Severity: 16
    State: 51
    
  • Notes:
    • Timeout errors can indicate performance bottlenecks or resource contention.
    • Investigating the root cause, such as long-running queries or insufficient resources, is essential.

5. Data Integrity Errors

  • Description: Occurs when data integrity checks fail, such as during DBCC CHECKDB operations.
  • Internal Error: Error: 8966
  • Message Example:
    Date/Time: 2023-06-22 10:27:45.12
    Log Level: Error
    Source: SQL Server
    Message: Table error: Object ID 12345678, index ID 1, partition ID 123456789012345, alloc unit ID 123456789012345678 (type In-row data). The off-row data node at page (1:123456), slot 0, text ID 1234567890123456789 is referenced by page (1:234567), slot 0, but was not seen in the scan.
    Error Number: 8966
    Severity: 16
    State: 1
    
  • Notes:
    • Data integrity errors are critical and can indicate corruption in the database.
    • Running DBCC CHECKDB regularly helps in early detection and remediation of such issues.

6. Hardware and Resource Warnings

  • Description: Warnings about hardware and resource issues, such as low disk space or high CPU usage.
  • Message Example:
    Date/Time: 2023-06-22 10:28:37.54
    Log Level: Warning
    Source: SQL Server
    Message: SQL Server has encountered 2 occurrence(s) of I/O requests taking longer than 15 seconds to complete on file [D:\Data\MyDatabase.mdf] in database [MyDatabase] (123). The OS file handle is 0x0000000000000ABC. The offset of the latest long I/O is: 0x0000001234567890.
    
  • Notes:
    • Such warnings indicate potential issues with hardware or storage subsystems.
    • Proactive monitoring and maintenance of hardware resources are crucial to avoid performance degradation.

Background Processes and Mechanisms

Error Detection and Logging

  • Error Detection: SQL Server continuously monitors various operations for errors and warnings using internal validation and checking mechanisms.
  • Logging Mechanism: When an error or warning is detected, SQL Server generates a log entry and writes it to the error log file. This is handled by the SQL Server logging subsystem.
  • Error Severity: Each error is assigned a severity level, which determines its criticality and the appropriate response.

Handling Errors

  • Retry Logic: For certain transient errors, SQL Server may implement retry logic to automatically attempt the operation again.
  • Alerting: Critical errors and warnings can trigger alerts, which notify administrators through email, SNMP, or other notification systems.
  • Automatic Remediation: In some cases, SQL Server can automatically take corrective actions, such as restarting services or reallocating resources.

Efficient Reading of Error 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 to quickly find relevant entries.
  2. Using T-SQL Commands:

    • xp_readerrorlog: Use this system stored procedure to query error logs directly.
      EXEC xp_readerrorlog 0, 1, 'keyword';
  3. Using Extended Events:

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

    • SQL Server Alerts: Configure SQL Server Agent alerts to notify you of critical events.
      USE msdb;
      GO
      EXEC sp_add_alert
          @name = N'Severity 16 Errors',
          @message_id = 0,
          @severity = 16,
          @enabled = 1,
          @delay_between_responses = 0,
          @include_event_description_in = 1,
          @database_name = N'ALL',
          @notification_message = N'Severity 16 error occurred',
          @job_name = N'NotifyDBAJob';
      GO
      EXEC sp_add_notification
          @alert_name = N'Severity 16 Errors',
          @operator_name = N'DBA',
          @notification_method = 1;
  5. Using Third-Party Tools:

    • Advanced Monitoring: Utilize third-party tools for enhanced log analysis and alerting capabilities.
    • Examples: SentryOne, SolarWinds Database Performance Analyzer, Redgate SQL Monitor.

Conclusion

Error and Warning Events in SQL Server logs provide vital insights into the health and performance of SQL Server instances. Understanding the structure of these logs, common errors and warnings, and the internal processes behind them allows administrators to effectively troubleshoot and manage their SQL Server environments.

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