Introduction

Updated at:

Distributed Relational Database Service PolarDB-X 1.0 integrates with Log Service to offer the SQL audit and analysis feature. This feature not only allows you to audit historical SQL records but also provides real-time diagnostic analysis of SQL execution status, performance metrics, and potential security issues.

You can enable the SQL audit and analysis feature in the PolarDB-X 1.0 console to analyze logs in real time. Additionally, PolarDB-X 1.0 uses Log Service to provide a ready-to-use report center that offers comprehensive insights into your database's execution status, performance, and potential security issues.

Benefits

  • Easy to use: Enable the feature with a simple configuration to start real-time auditing and analysis of SQL logs.
  • No performance impact: The feature pulls SQL log files from PolarDB-X 1.0 nodes and uploads them to Log Service in real time, without affecting your instance's performance.
  • Historical troubleshooting: Import historical SQL logs to investigate past issues.
  • Real-time analysis: Powered by Log Service, this feature offers real-time SQL analysis, a ready-to-use report center, and support for custom reports and drill-down analysis, providing deep insights into your database's execution status, performance, and potential security issues.
  • Real-time alerts: Set up near-real-time alerts based on specific metrics to respond promptly to critical service anomalies.
  • Cost-effective: Storage costs are lower than traditional solutions, with prices as low as CNY 0.58 per GB per month. For more information, see billing method.

Limitations and notes

  • To use the SQL audit and analysis feature, you must first enable Log Service.
  • Shared instances do not support the SQL audit and analysis feature.
  • By default, SQL audit logs are retained for 45 days. You can change the retention period as needed.
  • Do not delete or modify the default Project, Logstore, index, or dashboard settings that Log Service creates for you. Log Service periodically updates and upgrades the SQL audit and analysis feature, and Log Service also automatically updates the index and default reports for the dedicated Logstore.
  • To allow a sub-account to use the SQL audit and analysis feature, you must grant it the necessary Log Service permissions.
  • The SQL audit and analysis feature is disabled by default. Enabling this feature incurs additional costs. For detailed pricing, see billing method. If you disable the feature in the console, the system stops writing new data, but you can still view historical data. This historical data incurs storage and indexing fees. You can delete the Logstore in the Log Service console to delete the historical data. After deletion, Log Service no longer charges for that data.
  • The maximum displayed length of a single SQL statement is 4,096 characters. Any excess characters are truncated.

Troubleshooting problematic SQL

After you enable the SQL audit and analysis feature, you can quickly search SQL logs to identify and troubleshoot issues. For example, to check if anyone has executed a DROP operation, run the following query:

sql_type: Drop   

As shown in the following figure, the query results include the SQL execution time, the user, and the client IP address.

Analyzing high-cost SQL templates

In most applications, SQL statements are dynamically generated from templates with parameters. The real-time analysis capabilities of Log Service let you retrieve a list of high-cost SQL templates from your database.

For example, run the following query:

SELECT sql_code as "SQL Template ID", round(total_time * 1.0 /sum(total_time) over() * 100, 2) as "Total Time Ratio (%)" ,execute_times as "Execution Count", round(avg_time) as "Avg. Execution Time",round(avg_rows) as "Avg. Affected Rows", CASE WHEN length(sql) > 200 THEN  concat(substr(sql, 1, 200), '......') ELSE trim(lpad(sql, 200, ' ')) end as "Sample SQL" FROM  (SELECT sql_code, count(1) as execute_times, sum(response_time) as total_time, avg(response_time) as avg_time, avg(affect_rows) as avg_rows, arbitrary(sql) as sql FROM log GROUP BY sql_code) ORDER BY "Total Time Ratio (%)" desc limit 10

The query results, shown in the following figure, include the SQL template ID, the template's percentage of total execution time, the execution count, the average execution time, the average number of affected rows, and a sample SQL statement. Based on this analysis, you can identify and optimize the highest-cost SQL templates in your application.

Log reports

To simplify analysis, the SQL audit and analysis feature for PolarDB-X 1.0 integrates with Log Service to provide ready-to-use reports. These reports allow you to analyze your database's execution status, performance, and potential security issues in real time.

  • Operation Center

  • Performance Center

  • Security Center
    The DRDS logs - Security Center dashboard displays database security metrics, including error count, large-scale deletion events, large-scale modification events, risky SQL executions, and the number of risky IP addresses. All metrics support hour-over-hour and day-over-day comparisons. The Error Distribution (Today) area below contains the Error Operation Type Distribution and External Network Distribution of Erroneous Clients subpanels.