Slow query logs can significantly impact database stability. When a database experiences high workloads or performance fluctuations, database administrators (DBAs) or developers first check for running slow queries. Database Autonomy Service (DAS) provides a slow log analysis feature that collects statistics and analyzes requests with running times that exceed a specified threshold. This helps you quickly identify and resolve database performance issues to improve system stability and reliability.
Video introduction
Prerequisites
The target database instance is an ApsaraDB for MongoDB replica set instance or sharded cluster instance.
Background information
Slow query logs are output by the database kernel. The configuration parameters and thresholds vary for different database engines. For specific parameters and thresholds, see the official documentation for the corresponding database engine.
Procedure
Log on to the DAS console.
In the navigation pane on the left, click .
Find the target instance and click the instance ID to open the instance details page.
In the navigation pane on the left, click .
Select a time range to view the Slow Log Trend, Event Distribution, Slow Log Statistics, and Slow Log Details.
NoteThe end time must be later than the start time. The maximum time range is seven days. You can query slow query logs that were generated in the last month.
In the Slow Query Log Trends chart, you can select a point in time to view the corresponding Slow Query Log Statistics, and Slow Query Log Details.
NoteIf a slow SQL statement is too long to be fully displayed, you can hover the pointer over the statement. The complete statement appears in a dialog box.
For sharded cluster instances, you can view the number of slow requests for each node in the Node ID drop-down list area.
Click
to save the slow log information locally.Click
to go to OpenAPI Explorer and debug the API with the parameters that you selected.In the Event Distribution section:
You can query slow log events within the specified time range. Click an event to view its details.
In the Slow Log Statistics area, find the target log template. In the Actions column, click the Details button to view the Slow Log Sample .
In the Slow Log Statistics area, find the target log template. In the Actions column, click the Optimize button. In the Optimize dialog box that appears, view the diagnosis results.
NoteThis optimization feature, which mainly provides index optimization suggestions, is available only for slow query log scenarios where the Operation Type is
query. The supported scope is as follows:Supported scenarios:
Field-specific search: If a query such as
db.col.find({ a: 1 })is recorded in the slow query log, the system recommends that you create a corresponding index:db.col.createIndex({ a: 1 }).Compound query: If a query such as
db.col.find({ a: 1, b: 1 })is recorded in the slow query log, we recommend that you create a compound index, such asdb.col.createIndex({ a: 1, b: 1 }).
Unsupported scenarios:
Existing indexes are not recommended again.
Special index types such as
text,unique, orhashare not supported.Queries do not support complex logical operators, such as
$oror$and.Index suggestions are generated only for
findoperations, not for other operations such as sorting$sortor collation$collation.
If you accept the diagnostic suggestion, click Copy in the upper-right corner of the page, and paste the optimized statement into a database client or DMS to execute it. If you do not accept the diagnostic suggestion, you can click Cancel to end the diagnosis.
ImportantAdding an index might degrade the execution plans of other queries and consume resources such as instance performance and storage space. Before you accept a suggestion, assess the potential risks and impacts on other queries and overall resource consumption.
FAQ
Q: Why can I access the legacy slow query log page but encounter a permission error, such as NoPermission or Forbidden.RAM, when I try to access the new page?
A: The redesigned slow query log page provides more powerful data filtering features. Therefore, it requires different access policies (Actions) than the previous version. You cannot access this page without the required permissions. To grant the following access policies to a RAM user, see the RAM user authorization document:
Required permissions: viewing slow log trend chart data (DescribeSlowLogHistogramAsync), slow log template statistics (DescribeSlowLogStatistic), slow log details (DescribeSlowLogRecords), Event Center data (GetInstanceEventWithGroupType), and Performance Trend data (GetPerformanceMetrics).
Recommended permissions: tagging SQL statements (CreateSqlTag) and retrieving SQL tags (DescribeDasQueryTagNames).