-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsortprofile.go
More file actions
189 lines (175 loc) · 4.68 KB
/
Copy pathsortprofile.go
File metadata and controls
189 lines (175 loc) · 4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package qshape
import (
"sort"
"strings"
pg_query "github.com/pganalyze/pg_query_go/v6"
)
const (
sortHostPrefix = "SELECT 1 ORDER BY "
unrenderableSort = "<unrenderable>"
)
type (
SortVariant struct {
Fingerprint string `json:"fingerprint"`
SortClauses []string `json:"sort_clauses,omitempty"`
Calls int64 `json:"calls,omitempty"`
TotalExecTimeMs float64 `json:"total_exec_time_ms,omitempty"`
MeanExecTimeMs float64 `json:"mean_exec_time_ms,omitempty"`
Rows int64 `json:"rows,omitempty"`
}
SortProfile struct {
CostKey string `json:"cost_key"`
DistinctSorts int `json:"distinct_sorts"`
Variants []SortVariant `json:"variants"`
TotalCalls int64 `json:"total_calls"`
TotalExecTimeMs float64 `json:"total_exec_time_ms,omitempty"`
MeanExecTimeMs float64 `json:"mean_exec_time_ms,omitempty"`
Rows int64 `json:"rows,omitempty"`
}
)
// SortProfiles groups clusters by CostKey and reports the distinct orderings in each
func SortProfiles(clusters []Cluster) []SortProfile {
groups := make(map[string]*SortProfile)
var order []string
var unkeyed []SortProfile
for _, c := range clusters {
v := SortVariant{
Fingerprint: c.Fingerprint,
SortClauses: sortClausesOf(c.Canonical),
Calls: c.TotalCalls,
TotalExecTimeMs: c.TotalExecTimeMs,
MeanExecTimeMs: c.MeanExecTimeMs,
Rows: c.Rows,
}
key, err := CostKey(c.Canonical)
if err != nil {
unkeyed = append(unkeyed, SortProfile{
DistinctSorts: 1,
Variants: []SortVariant{v},
TotalCalls: c.TotalCalls,
TotalExecTimeMs: c.TotalExecTimeMs,
MeanExecTimeMs: c.MeanExecTimeMs,
Rows: c.Rows,
})
continue
}
p, ok := groups[key]
if !ok {
p = &SortProfile{CostKey: key}
groups[key] = p
order = append(order, key)
}
p.Variants = append(p.Variants, v)
p.TotalCalls += c.TotalCalls
p.TotalExecTimeMs += c.TotalExecTimeMs
p.Rows += c.Rows
}
out := make([]SortProfile, 0, len(groups)+len(unkeyed))
for _, key := range order {
p := groups[key]
if p.TotalCalls > 0 {
p.MeanExecTimeMs = p.TotalExecTimeMs / float64(p.TotalCalls)
}
sortVariants(p.Variants)
p.DistinctSorts = distinctSorts(p.Variants)
out = append(out, *p)
}
out = append(out, unkeyed...)
hasTiming := false
for _, p := range out {
if p.TotalExecTimeMs > 0 {
hasTiming = true
break
}
}
sort.SliceStable(out, func(i, j int) bool {
if hasTiming && out[i].TotalExecTimeMs != out[j].TotalExecTimeMs {
return out[i].TotalExecTimeMs > out[j].TotalExecTimeMs
}
if out[i].TotalCalls != out[j].TotalCalls {
return out[i].TotalCalls > out[j].TotalCalls
}
if out[i].CostKey != out[j].CostKey {
return out[i].CostKey < out[j].CostKey
}
return firstFingerprint(out[i]) < firstFingerprint(out[j])
})
return out
}
func firstFingerprint(p SortProfile) string {
if len(p.Variants) == 0 {
return ""
}
return p.Variants[0].Fingerprint
}
func sortVariants(vs []SortVariant) {
sort.Slice(vs, func(i, j int) bool {
if vs[i].TotalExecTimeMs != vs[j].TotalExecTimeMs {
return vs[i].TotalExecTimeMs > vs[j].TotalExecTimeMs
}
if vs[i].Calls != vs[j].Calls {
return vs[i].Calls > vs[j].Calls
}
return vs[i].Fingerprint < vs[j].Fingerprint
})
}
func distinctSorts(vs []SortVariant) int {
seen := make(map[string]bool, len(vs))
for _, v := range vs {
if len(v.SortClauses) == 0 {
continue
}
seen[strings.Join(v.SortClauses, "\x00")] = true
}
return len(seen)
}
// sortClausesOf collects the ORDER BY of every SelectStmt in sql, outermost first
func sortClausesOf(sql string) []string {
tree, err := pg_query.Parse(sql)
if err != nil {
return nil
}
var out []string
var walk func(n *pg_query.Node)
walk = func(n *pg_query.Node) {
if n == nil {
return
}
if _, ok := n.Node.(*pg_query.Node_SortBy); ok {
return
}
if s, ok := n.Node.(*pg_query.Node_SelectStmt); ok && s.SelectStmt != nil && len(s.SelectStmt.SortClause) > 0 {
text := deparseSortClause(s.SelectStmt.SortClause)
if text == "" {
text = unrenderableSort
}
out = append(out, text)
}
forEachChild(n, walk)
}
for _, raw := range tree.Stmts {
if raw != nil {
walk(raw.Stmt)
}
}
return out
}
func deparseSortClause(clause []*pg_query.Node) string {
host, err := pg_query.Parse("SELECT 1")
if err != nil || len(host.Stmts) == 0 {
return ""
}
sel := host.Stmts[0].Stmt.GetSelectStmt()
if sel == nil {
return ""
}
sel.SortClause = clause
out, err := pg_query.Deparse(host)
if err != nil {
return ""
}
if !strings.HasPrefix(out, sortHostPrefix) {
return ""
}
return strings.TrimPrefix(out, sortHostPrefix)
}