51
51
}
52
52
53
53
Summary struct {
54
- tables []TableSummary
54
+ tables []* TableSummary
55
55
failures []FailuresSummary
56
56
hotQueries []keys.QueryAnalysisResult
57
57
}
60
60
Table string
61
61
ReadQueryCount int
62
62
WriteQueryCount int
63
- Columns map [ColumnInformation ]ColumnUsage
63
+ ColumnUses map [ColumnInformation ]ColumnUsage
64
64
JoinPredicates []operators.JoinPredicate
65
65
Failed bool
66
66
RowCount int
@@ -78,6 +78,19 @@ type (
78
78
queryGraph map [graphKey ]map [operators.JoinPredicate ]int
79
79
)
80
80
81
+ func (s * Summary ) GetTable (name string ) * TableSummary {
82
+ for _ , table := range s .tables {
83
+ if table .Table == name {
84
+ return table
85
+ }
86
+ }
87
+ return nil
88
+ }
89
+
90
+ func (s * Summary ) AddTable (table * TableSummary ) {
91
+ s .tables = append (s .tables , table )
92
+ }
93
+
81
94
const (
82
95
Join Position = iota
83
96
JoinRange
@@ -112,9 +125,9 @@ func (ts TableSummary) GetColumns() iter.Seq2[ColumnInformation, ColumnUsage] {
112
125
ci ColumnInformation
113
126
cu ColumnUsage
114
127
}
115
- columns := make ([]colDetails , 0 , len (ts .Columns ))
128
+ columns := make ([]colDetails , 0 , len (ts .ColumnUses ))
116
129
maxColUse := make (map [string ]float64 )
117
- for colInfo , usage := range ts .Columns {
130
+ for colInfo , usage := range ts .ColumnUses {
118
131
columns = append (columns , colDetails {ci : colInfo , cu : usage })
119
132
if maxColUse [colInfo .Name ] < usage .Percentage {
120
133
maxColUse [colInfo .Name ] = usage .Percentage
@@ -156,19 +169,16 @@ func (ts TableSummary) UseCount() int {
156
169
// and prints this summary information to the output.
157
170
func printKeysSummary (out io.Writer , fileName string , analysedQueries * keys.Output , now time.Time , hotMetric , schemaInfoPath string ) {
158
171
var err error
172
+ summary := & Summary {}
159
173
160
174
metricReader := getMetricForHotness (hotMetric )
161
175
162
- var schemaInfo * schema.Info
163
- if schemaInfoPath != "" {
164
- schemaInfo , err = schema .Load (schemaInfoPath )
165
- if err != nil {
166
- panic (err )
167
- }
176
+ err = summarizeSchemaInfo (summary , schemaInfoPath )
177
+ if err != nil {
178
+ panic (err )
168
179
}
169
- summary := & Summary {}
170
- summarizeKeysQueries (summary , analysedQueries , metricReader , schemaInfo )
171
180
181
+ summarizeKeysQueries (summary , analysedQueries , metricReader )
172
182
md := & markdown.MarkDown {}
173
183
msg := `# Query Analysis Report
174
184
@@ -178,7 +188,7 @@ func printKeysSummary(out io.Writer, fileName string, analysedQueries *keys.Outp
178
188
`
179
189
md .Printf (msg , now .Format (time .DateTime ), fileName )
180
190
renderHotQueries (md , summary .hotQueries , metricReader )
181
- renderTableUsage (summary .tables , md , schemaInfo != nil )
191
+ renderTableUsage (summary .tables , md , schemaInfoPath != "" )
182
192
renderTablesJoined (md , analysedQueries )
183
193
renderFailures (md , summary .failures )
184
194
@@ -188,6 +198,26 @@ func printKeysSummary(out io.Writer, fileName string, analysedQueries *keys.Outp
188
198
}
189
199
}
190
200
201
+ func summarizeSchemaInfo (summary * Summary , schemaInfoPath string ) error {
202
+ if schemaInfoPath == "" {
203
+ return nil
204
+ }
205
+ schemaInfo , err := schema .Load (schemaInfoPath )
206
+ if err != nil {
207
+ return err
208
+ }
209
+ for _ , ti := range schemaInfo .Tables {
210
+ table := summary .GetTable (ti .Name )
211
+ if table == nil {
212
+ table = & TableSummary {Table : ti .Name }
213
+ summary .AddTable (table )
214
+ }
215
+ table .RowCount = ti .Rows
216
+ }
217
+
218
+ return nil
219
+ }
220
+
191
221
type getMetric = func (q keys.QueryAnalysisResult ) float64
192
222
193
223
func getMetricForHotness (metric string ) getMetric {
@@ -269,12 +299,15 @@ func renderHotQueries(md *markdown.MarkDown, queries []keys.QueryAnalysisResult,
269
299
}
270
300
}
271
301
272
- func renderTableUsage (tableSummaries []TableSummary , md * markdown.MarkDown , includeRowCount bool ) {
302
+ func renderTableUsage (tableSummaries []* TableSummary , md * markdown.MarkDown , includeRowCount bool ) {
273
303
if len (tableSummaries ) == 0 {
274
304
return
275
305
}
276
306
277
307
sort .Slice (tableSummaries , func (i , j int ) bool {
308
+ if tableSummaries [i ].UseCount () == tableSummaries [j ].UseCount () {
309
+ return tableSummaries [i ].Table < tableSummaries [j ].Table
310
+ }
278
311
return tableSummaries [i ].UseCount () > tableSummaries [j ].UseCount ()
279
312
})
280
313
@@ -287,7 +320,7 @@ func renderTableUsage(tableSummaries []TableSummary, md *markdown.MarkDown, incl
287
320
}
288
321
}
289
322
290
- func renderTableOverview (md * markdown.MarkDown , tableSummaries []TableSummary , includeRowCount bool ) {
323
+ func renderTableOverview (md * markdown.MarkDown , tableSummaries []* TableSummary , includeRowCount bool ) {
291
324
headers := []string {"Table Name" , "Reads" , "Writes" }
292
325
if includeRowCount {
293
326
headers = append (headers , "Number of Rows" )
@@ -308,7 +341,7 @@ func renderTableOverview(md *markdown.MarkDown, tableSummaries []TableSummary, i
308
341
md .PrintTable (headers , rows )
309
342
}
310
343
311
- func renderColumnUsageTable (md * markdown.MarkDown , summary TableSummary ) {
344
+ func renderColumnUsageTable (md * markdown.MarkDown , summary * TableSummary ) {
312
345
md .PrintHeader (fmt .Sprintf ("Table: `%s` (%d reads and %d writes)" , summary .Table , summary .ReadQueryCount , summary .WriteQueryCount ), 4 )
313
346
314
347
headers := []string {"Column" , "Position" , "Used %" }
@@ -427,7 +460,7 @@ func makeKey(lhs, rhs operators.Column) graphKey {
427
460
return graphKey {rhs .Table , lhs .Table }
428
461
}
429
462
430
- func summarizeKeysQueries (summary * Summary , queries * keys.Output , metricReader getMetric , schemaInfo * schema. Info ) {
463
+ func summarizeKeysQueries (summary * Summary , queries * keys.Output , metricReader getMetric ) {
431
464
tableSummaries := make (map [string ]* TableSummary )
432
465
tableUsageWriteCounts := make (map [string ]int )
433
466
tableUsageReadCounts := make (map [string ]int )
@@ -438,39 +471,31 @@ func summarizeKeysQueries(summary *Summary, queries *keys.Output, metricReader g
438
471
checkQueryForHotness (& summary .hotQueries , query , metricReader )
439
472
}
440
473
441
- tableRows := make (map [string ]int )
442
- if schemaInfo != nil {
443
- for _ , ti := range schemaInfo .Tables {
444
- tableRows [ti .Name ] = ti .Rows
445
- }
446
- }
447
-
448
474
// Second pass: calculate percentages
449
475
for _ , tblSummary := range tableSummaries {
450
476
tblSummary .ReadQueryCount = tableUsageReadCounts [tblSummary .Table ]
451
477
tblSummary .WriteQueryCount = tableUsageWriteCounts [tblSummary .Table ]
452
478
count := tblSummary .ReadQueryCount + tblSummary .WriteQueryCount
453
- if schemaInfo != nil {
454
- if rowCount , ok := tableRows [tblSummary .Table ]; ok {
455
- tblSummary .RowCount = rowCount
456
- }
457
- }
458
479
countF := float64 (count )
459
- for colName , usage := range tblSummary .Columns {
480
+ for colName , usage := range tblSummary .ColumnUses {
460
481
usage .Percentage = (float64 (usage .Count ) / countF ) * 100
461
- tblSummary .Columns [colName ] = usage
482
+ tblSummary .ColumnUses [colName ] = usage
462
483
}
463
484
}
464
485
465
486
// Convert map to slice
466
- result := make ([]TableSummary , 0 , len (tableSummaries ))
467
- for _ , summary := range tableSummaries {
468
- result = append (result , * summary )
487
+ for _ , tblSummary := range tableSummaries {
488
+ table := summary .GetTable (tblSummary .Table )
489
+ if table == nil {
490
+ summary .AddTable (tblSummary )
491
+ continue
492
+ }
493
+ table .ReadQueryCount = tblSummary .ReadQueryCount
494
+ table .WriteQueryCount = tblSummary .WriteQueryCount
495
+ if table .ColumnUses != nil {
496
+ panic ("ColumnUses already set for table" + tblSummary .Table )
497
+ }
469
498
}
470
- sort .Slice (result , func (i , j int ) bool {
471
- return result [i ].Table < result [j ].Table
472
- })
473
- summary .tables = result
474
499
475
500
// Collect failed queries
476
501
var failures []FailuresSummary
@@ -508,8 +533,8 @@ func gatherTableInfo(query keys.QueryAnalysisResult, tableSummaries map[string]*
508
533
for _ , table := range query .TableNames {
509
534
if _ , exists := tableSummaries [table ]; ! exists {
510
535
tableSummaries [table ] = & TableSummary {
511
- Table : table ,
512
- Columns : make (map [ColumnInformation ]ColumnUsage ),
536
+ Table : table ,
537
+ ColumnUses : make (map [ColumnInformation ]ColumnUsage ),
513
538
}
514
539
}
515
540
@@ -536,9 +561,9 @@ func summarizeColumnUsage(tableSummary *TableSummary, query keys.QueryAnalysisRe
536
561
columns = slices .Compact (columns )
537
562
538
563
for _ , col := range columns {
539
- usage := tableSummary .Columns [col ]
564
+ usage := tableSummary .ColumnUses [col ]
540
565
usage .Count += query .UsageCount
541
- tableSummary .Columns [col ] = usage
566
+ tableSummary .ColumnUses [col ] = usage
542
567
}
543
568
}
544
569
0 commit comments