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
60 changes: 19 additions & 41 deletions internal/sql-analyzer/const/sql/sql_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,56 +83,34 @@ const (
CheckPlanExistence = `SELECT COUNT(*) FROM sql_plan WHERE TENANT_ID = ? AND SVR_IP = ? AND SVR_PORT = ? AND PLAN_ID = ?`
GetPlanStats = `
WITH PlanAgg AS (
SELECT
TENANT_ID,
SVR_IP,
SVR_PORT,
PLAN_ID,
PLAN_HASH,
MAX(GMT_CREATE) as GMT_CREATE,
SUM(IO_COST) as IO_COST,
SUM(CPU_COST) as CPU_COST,
SUM(COST) as COST,
SUM(REAL_COST) as REAL_COST
FROM sql_plan
WHERE SQL_ID = ?
GROUP BY TENANT_ID, SVR_IP, SVR_PORT, PLAN_ID, PLAN_HASH
),
PlanStats AS (
SELECT
PLAN_HASH,
CAST(AVG(IO_COST) AS BIGINT) as IO_COST,
CAST(AVG(CPU_COST) AS BIGINT) as CPU_COST,
CAST(AVG(COST) AS BIGINT) as COST,
CAST(AVG(REAL_COST) AS BIGINT) as REAL_COST
FROM PlanAgg
GROUP BY PLAN_HASH
),
LatestPlan AS (
SELECT
TENANT_ID,
SVR_IP,
SVR_PORT,
PLAN_ID,
PLAN_HASH,
GMT_CREATE,
ROW_NUMBER() OVER (PARTITION BY PLAN_HASH ORDER BY GMT_CREATE DESC, PLAN_ID DESC) as rn
FROM PlanAgg
IO_COST,
CPU_COST,
COST,
REAL_COST,
ROW_NUMBER() OVER (PARTITION BY TENANT_ID, SVR_IP, SVR_PORT, PLAN_HASH ORDER BY GMT_CREATE DESC, PLAN_ID DESC) as rn
FROM sql_plan
WHERE SQL_ID = ? AND ID = 0
)
SELECT
lp.TENANT_ID,
lp.SVR_IP,
lp.SVR_PORT,
lp.PLAN_ID,
lp.PLAN_HASH,
lp.GMT_CREATE,
ps.IO_COST,
ps.CPU_COST,
ps.COST,
ps.REAL_COST
FROM LatestPlan lp
JOIN PlanStats ps ON lp.PLAN_HASH = ps.PLAN_HASH
WHERE lp.rn = 1
TENANT_ID,
SVR_IP,
SVR_PORT,
PLAN_ID,
PLAN_HASH,
GMT_CREATE,
IO_COST,
CPU_COST,
COST,
REAL_COST
FROM PlanAgg
WHERE rn = 1
`
GetTableInfo = `
SELECT
Expand Down
3 changes: 2 additions & 1 deletion internal/sql-analyzer/generated/swagger/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,8 @@ const docTemplate = `{
"type": "integer"
},
"planHash": {
"type": "integer"
"type": "string",
"example": "0"
},
"planId": {
"type": "integer"
Expand Down
3 changes: 2 additions & 1 deletion internal/sql-analyzer/generated/swagger/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,8 @@
"type": "integer"
},
"planHash": {
"type": "integer"
"type": "string",
"example": "0"
},
"planId": {
"type": "integer"
Expand Down
3 changes: 2 additions & 1 deletion internal/sql-analyzer/generated/swagger/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ definitions:
ioCost:
type: integer
planHash:
type: integer
example: "0"
type: string
planId:
type: integer
realCost:
Expand Down