Query governance
Database Autonomy Service (DAS) uses offline data analysis to analyze and automatically tag slow SQL queries from all your instances on the previous day, running daily at 01:00. DAS automatically classifies slow SQL queries, prioritizes them for optimization, and provides governance suggestions and data export.
Prerequisites
-
The database instance must be an ApsaraDB RDS for PostgreSQL instance.
-
This feature supports database instances in the Chinese mainland, China (Hong Kong), and Singapore regions.
NoteStarting April 1, 2023, query governance supports database instances in the China (Hong Kong) and Singapore regions.
Limitations
-
Query governance uses T+1 offline analysis. If you tag a SQL query as Optimization Not Required, the change in the number of Optimizable SQL queries appears on the following day.
-
Predefined SQL tags from DAS cannot be disabled.
-
For each database instance, DAS analyzes only the top 200 slow SQL queries by execution count. Therefore, the maximum number of Slow SQL Templates for each instance is 200.
Key concepts
|
Term |
Description |
|
Optimizable SQL |
After excluding the SQL statements that do not require optimization from all slow SQL statements, the remaining statements are the Optimizable SQL. |
|
SQL Not Requiring Optimization |
Includes SQL queries automatically tagged by DAS as Ignored and queries manually tagged by users as Optimization Not Required. |
|
Ignored |
DAS automatically tags SQL statements as Optimization Not Required, such as those starting with |
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 left-side navigation pane, choose .
-
On the Query Governance page, review the query governance results.
-
Query Governance Overview: Displays data categorized and tagged by the system.
NoteThe Failed SQL Executions metric is available only for instances for which DAS Enterprise Edition is enabled.
-
Query Governance Trend: Displays how query governance results change over the selected time range.
-
Top Rankings: View the Best-performing Instances and Worst-performing Instances lists.
-
Worst-performing Instances: Ranks database instances by the number of slow query executions.
-
Best-performing Instances: Ranks database instances by the change in slow query execution counts. A negative number indicates a decrease, reflecting effective optimization. A positive number indicates an increase.
DAS recommends that you focus on the rankings for Optimizable SQL.
-
-
SQL to Be Optimized: Set filter conditions to find specific SQL statements to manage.
NoteYou can filter by DB Name, SQL Keyword, Rule Tag, and Username. These filters use AND logic.
-
Separate multiple database names with a comma (,) to apply OR logic.
-
Separate multiple SQL keywords with spaces to apply AND logic.
-
Separate multiple database usernames with a comma (,) to apply OR logic.
-
Selecting multiple rule tags applies OR logic.
-
In the Actions column of a target SQL sample, click Suggestions to view detailed governance suggestions.
-
In the Actions column of a target SQL sample, click Add Tag to manually add a tag to the SQL statement. For more information about the meanings of tags, see User tags.
You can also select multiple SQL statements and tag them at once.
-
In the Actions column of a target SQL sample, click Samples to view the details of slow query log samples for the SQL statement.
-
In the Actions column of a target SQL sample, click Trend to view the analysis details of the slow query logs for the SQL statement. For more information about slow query log analysis, see Slow query logs.
You can export and share the data of SQL statements that need optimization. For more information, see Best practices.
-
-
Failed SQL: Set filter conditions to find specific SQL statements to view.
Note-
The Failed SQL feature is available only on instances with DAS Enterprise Edition enabled.
-
You can filter by DB Name and SQL Keyword. These filters use AND logic.
-
Separate multiple database names with a comma (,) to apply OR logic.
-
Separate multiple SQL keywords with spaces to apply AND logic.
-
In the Actions column of a target SQL sample, click Samples to view sample details for the SQL statement.
-
-
Best practices
-
Use tags to classify and prioritize SQL optimization.
The core governance logic divides All Slow Queries into two categories: Optimization Not Required and Optimizable SQL. Use the following SQL tags to filter and manage slow queries by priority.
ID
Tag name
Level
Recommendation
Description
NEW_SQL
New slow SQL
Critical
✔️
A slow SQL query that has not appeared in the past seven days.
DAS_IGNORE
DAS ignored
Notice
➖
DAS automatically tags the query as not requiring optimization. Examples include SQL statements that start with
show,create,xa,commit,rollback,select sleep, orexplain.FUZZY_LIKE
Fuzzy LIKE query
Notice
➖
A
likeclause is used for fuzzy matching, which prevents the use of an index.HAS_EXPR
Contains expression
Notice
➖
The query contains an expression. The column involved in the calculation cannot use an index.
LARGE_IN_LIST
Large IN list
Notice
➖
The
INclause contains more than 200 elements, which is likely generated by a program. Indexes cannot be used on the involved columns.SELECT_STAR
SELECT full column
Notice
➖
When you use
SELECT, specify only the required fields to avoid performance issues and resource waste caused by returning unnecessary columns.INDEX_ADVISOR
Index suggestion
Notice
✔️
A suggestion to create an index to accelerate SQL execution.
COMPLEX_JOIN
Complex JOIN
Notice
➖
Do not
joinmore than three tables. The fields that youjoinmust have identical data types. For multi-table joins, ensure that the join fields are indexed.CROSS_DB
Cross-database query
Notice
➖
When you migrate databases and tables across instances, cross-database queries may become unavailable.
SUBQUERY
Contains subquery
Notice
➖
The query contains a subquery. Consider rewriting it with a
JOINfor better performance.DEEP_PAGING
Deep paging
Critical
➖
The query uses a
LIMITclause for deep paging. We recommend rewriting the query with a join.WITHOUT_PREDICATE
No predicate
Critical
✔️
The statement contains no predicates. Check whether it involves a full table scan.
NULL_COMPARE
NULL match error
Critical
➖
Use
IS NULLorIS NOT NULLto check for aNULLvalue, because a direct comparison between a value and NULL always results in NULL.COUNT_NOT_STAR
Improper COUNT syntax
Critical
➖
Use
COUNT(*)instead ofcount(column_name) orcount(constant).count(*)is the standard syntax for counting rows as defined in SQL92. It is database-independent and is not affected byNULLor non-NULLvalues.count(column_name) does not count rows where the column has aNULLvalue.LARGE_ROWS_EXAMINED
Excessive rows scanned
Notice
➖
The query scans more than 50,000 rows on average. Excessive scanning consumes more resources, which slows down the query and can impact overall database performance.
NoteConsider using more selective filter conditions.
LARGE_ROWS_SENT
Excessive rows returned
Notice
➖
The query returns more than 5,000 rows on average. Consider reducing the number of returned rows.
NO_ADVICE
No suggestion
Notice
➖
No analysis suggestion is available.
PERIOD_SQL
Periodic SQL
Notice
➖
Executed at a fixed time every day.
DAS recommends that you focus on Optimizable SQL. The following table lists manual tags you can apply to organize and prioritize your work. For example, adding the Optimization Not Required tag excludes a statement from the Optimizable SQL list.
ID
Tag name
Level
Description
USER_IGNORE
No need to optimize
Notice
After you add this tag, DAS excludes the query from the next day's Optimizable SQL statistics.
DAS_IMPORTANT
Important SQL
Notice
Marks the SQL query as important.
DAS_NOT_IMPORTANT
Not important SQL
Notice
Marks the SQL query as not important.
DAS_IN_PLAN
Optimization planned
Notice
Marks the SQL query as scheduled for optimization.
-
Export data:
-
You can click Create Export Task to download all data currently displayed in the SQL Details tab.
NoteExported data is available for download for three days.
-
Create download tasks with different filter conditions, such as instances or rules, and assign them to different owners for optimization.
-
Alternatively, select multiple SQL records to export only those items.
-
-
Share data:
Share filtered slow SQL queries with team members in two ways:
-
You can select multiple SQL records and click Batch Sharing. The system generates a short link. When users who have permissions to access the DAS console click the link, they will see only the SQL records that you selected.
-
Alternatively, click Share (next to Export) to generate a URL that captures your current view, including all filters. Users with DAS console permissions can open the URL to see the same data without reconfiguring the filters.
-