Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions sqle/model/instance_audit_plan_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,16 @@ var FilterMap = map[FilterName]FilterType{
}

var OrderByMap = map[string] /* field name */ string /* field name with table*/ {
"counter": "audit_plan_sqls.info->'$.counter'",
"last_receive_timestamp": "audit_plan_sqls.info->'$.last_receive_timestamp'",
"query_time_avg": "audit_plan_sqls.info->'$.query_time_avg'",
"query_time_max": "audit_plan_sqls.info->'$.query_time_max'",
"row_examined_avg": "audit_plan_sqls.info->'$.row_examined_avg'",
"counter": "audit_plan_sqls.info->'$.counter'",
"query_time_total": "audit_plan_sqls.info->'$.query_time_total'",
"cpu_time_total": "audit_plan_sqls.info->'$.cpu_time_total'",
"disk_read_total": "audit_plan_sqls.info->'$.disk_read_total'",
"buffer_read_total": "audit_plan_sqls.info->'$.buffer_read_total'",
"user_io_wait_time_total": "audit_plan_sqls.info->'$.user_io_wait_time_total'",
"last_receive_timestamp": "audit_plan_sqls.info->'$.last_receive_timestamp'",
"query_time_avg": "audit_plan_sqls.info->'$.query_time_avg'",
"query_time_max": "audit_plan_sqls.info->'$.query_time_max'",
"row_examined_avg": "audit_plan_sqls.info->'$.row_examined_avg'",
}

var instanceAuditPlanSQLQueryTpl = `
Expand Down
55 changes: 38 additions & 17 deletions sqle/pkg/oracle/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (o *DB) Close() error {
return o.db.Close()
}

func (o *DB) QueryTopSQLs(ctx context.Context, topN int, notInUsers []string, orderBy string) ([]*DynPerformanceSQLArea, error) {
func (o *DB) QueryTopSQLs(ctx context.Context, collectIntervalMinute string, topN int, notInUsers []string, orderBy string) ([]*DynPerformanceSQLArea, error) {
// if notInUsers is empty, notInUsersStr will be empty
// if notInUsers is not empty, notInUsersStr will be formatted as "AND u.username NOT IN ('user1', 'user2')"
var notInUsersStr string
Expand All @@ -66,26 +66,47 @@ func (o *DB) QueryTopSQLs(ctx context.Context, topN int, notInUsers []string, or
notInUsersStr = strings.Join(notInUsersFormatted, ",")
notInUsersStr = fmt.Sprintf(notInUserSqlTpl, notInUsersStr)
}
query := fmt.Sprintf(DynPerformanceViewSQLAreaTpl, notInUsersStr, orderBy, topN)
rows, err := o.db.QueryContext(ctx, query)
if err != nil {
return nil, errors.Wrapf(err, "failed to query %s", query)
metrics := []string{DynPerformanceViewSQLAreaColumnElapsedTime, DynPerformanceViewSQLAreaColumnCPUTime, DynPerformanceViewSQLAreaColumnDiskReads, DynPerformanceViewSQLAreaColumnBufferGets}
if orderBy != "" {
metrics = []string{orderBy}
}
if topN == 0 {
topN = 10
}
defer rows.Close()

var ret []*DynPerformanceSQLArea
for rows.Next() {
res := DynPerformanceSQLArea{}
err = rows.Scan(&res.SQLFullText, &res.Executions, &res.ElapsedTime, &res.UserIOWaitTime, &res.CPUTime, &res.DiskReads, &res.BufferGets, &res.UserName)
for _, metric := range metrics {
query := fmt.Sprintf(DynPerformanceViewSQLAreaTpl, collectIntervalMinute, notInUsersStr, metric, topN)
err := func() error {
rows, err := o.db.QueryContext(ctx, query)
if err != nil {
return errors.Wrapf(err, "failed to query %s", query)
}
defer rows.Close()

for rows.Next() {
res := DynPerformanceSQLArea{}
if err := rows.Scan(
&res.SQLFullText,
&res.Executions,
&res.ElapsedTime,
&res.UserIOWaitTime,
&res.CPUTime,
&res.DiskReads,
&res.BufferGets,
&res.UserName,
); err != nil {
return errors.Wrapf(err, "failed to scan %s", query)
}
ret = append(ret, &res)
}
if err := rows.Err(); err != nil {
return errors.Wrapf(err, "failed to iterate %s", query)
}
return nil
}()
if err != nil {
return nil, errors.Wrapf(err, "failed to scan %s", query)
return nil, err
}
ret = append(ret, &res)
}

if err := rows.Err(); err != nil {
return nil, errors.Wrapf(err, "failed to iterate %s", query)
}

return ret, nil
}
1 change: 1 addition & 0 deletions sqle/pkg/oracle/perf.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ SELECT * FROM (
JOIN
DBA_USERS u ON s.parsing_user_id = u.user_id
WHERE
last_active_time >= SYSDATE - INTERVAL '%v' MINUTE AND
s.EXECUTIONS > 0
%v
ORDER BY %v DESC
Expand Down
45 changes: 19 additions & 26 deletions sqle/server/auditplan/task_type_oracle_topsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,7 @@ func (at *OracleTopSQLTaskV2) Params(instanceId ...string) params.Params {
Type: params.ParamTypeInt,
I18nDesc: locale.Bundle.LocalizeAll(locale.ParamCollectIntervalMinuteOracle),
},
{
Key: "top_n",
Desc: "Top N",
Value: "3",
Type: params.ParamTypeInt,
},
{
Key: "order_by_column",
Value: oracle.DynPerformanceViewSQLAreaColumnElapsedTime,
Type: params.ParamTypeString,
I18nDesc: locale.Bundle.LocalizeAll(locale.ParamOrderByColumn),
},
}

}

func (at *OracleTopSQLTaskV2) Metrics() []string {
Expand Down Expand Up @@ -138,7 +125,7 @@ func (at *OracleTopSQLTaskV2) ExtractSQL(logger *logrus.Entry, ap *AuditPlan, pe
notInUser = append(notInUser, blacklist.FilterContent)
}
// filter db user by
sqls, err := db.QueryTopSQLs(ctx, ap.Params.GetParam("top_n").Int(), notInUser, ap.Params.GetParam("order_by_column").String())
sqls, err := db.QueryTopSQLs(ctx, ap.Params.GetParam("collect_interval_minute").String(), ap.Params.GetParam("top_n").Int(), notInUser, ap.Params.GetParam("order_by_column").String())
if err != nil {
return nil, fmt.Errorf("query top sql fail, error: %v", err)
}
Expand Down Expand Up @@ -207,28 +194,34 @@ func (at *OracleTopSQLTaskV2) Head(ap *AuditPlan) []Head {
Desc: model.AuditResultDesc,
},
{
Name: MetricNameCounter,
Desc: locale.ApMetricNameCounter,
Name: MetricNameCounter,
Desc: locale.ApMetricNameCounter,
Sortable: true,
},
{
Name: MetricNameQueryTimeTotal,
Desc: locale.ApMetricNameQueryTimeTotal,
Name: MetricNameQueryTimeTotal,
Desc: locale.ApMetricNameQueryTimeTotal,
Sortable: true,
},
{
Name: MetricNameCPUTimeTotal,
Desc: locale.ApMetricNameCPUTimeTotal,
Name: MetricNameCPUTimeTotal,
Desc: locale.ApMetricNameCPUTimeTotal,
Sortable: true,
},
{
Name: MetricNameDiskReadTotal,
Desc: locale.ApMetricNameDiskReadTotal,
Name: MetricNameDiskReadTotal,
Desc: locale.ApMetricNameDiskReadTotal,
Sortable: true,
},
{
Name: MetricNameBufferGetCounter,
Desc: locale.ApMetricNameBufferGetCounter,
Name: MetricNameBufferGetCounter,
Desc: locale.ApMetricNameBufferGetCounter,
Sortable: true,
},
{
Name: MetricNameUserIOWaitTimeTotal,
Desc: locale.ApMetricNameUserIOWaitTimeTotal,
Name: MetricNameUserIOWaitTimeTotal,
Desc: locale.ApMetricNameUserIOWaitTimeTotal,
Sortable: true,
},
{
Name: MetricNameDBUser,
Expand Down
Loading