github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/internal/table/scanner/stats.go (about) 1 package scanner 2 3 import ( 4 "time" 5 6 "github.com/ydb-platform/ydb-go-genproto/protos/Ydb_TableStats" 7 8 "github.com/ydb-platform/ydb-go-sdk/v3/table/stats" 9 ) 10 11 // queryStats holds query execution statistics. 12 type queryStats struct { 13 stats *Ydb_TableStats.QueryStats 14 processCPUTime time.Duration 15 pos int 16 } 17 18 func (s *queryStats) ProcessCPUTime() time.Duration { 19 return s.processCPUTime 20 } 21 22 func (s *queryStats) Compilation() (c *stats.CompilationStats) { 23 if s.stats == nil || s.stats.GetCompilation() == nil { 24 return nil 25 } 26 27 return &stats.CompilationStats{ 28 FromCache: s.stats.GetCompilation().GetFromCache(), 29 Duration: time.Microsecond * time.Duration(s.stats.GetCompilation().GetDurationUs()), 30 CPUTime: time.Microsecond * time.Duration(s.stats.GetCompilation().GetCpuTimeUs()), 31 } 32 } 33 34 func (s *queryStats) QueryPlan() string { 35 return s.stats.GetQueryPlan() 36 } 37 38 func (s *queryStats) QueryAST() string { 39 return s.stats.GetQueryAst() 40 } 41 42 func (s *queryStats) TotalCPUTime() time.Duration { 43 return time.Microsecond * time.Duration(s.stats.GetTotalCpuTimeUs()) 44 } 45 46 func (s *queryStats) TotalDuration() time.Duration { 47 return time.Microsecond * time.Duration(s.stats.GetTotalDurationUs()) 48 } 49 50 // NextPhase returns next execution phase within query. 51 // If ok flag is false, then there are no more phases and p is invalid. 52 func (s *queryStats) NextPhase() (p stats.QueryPhase, ok bool) { 53 if s.pos >= len(s.stats.GetQueryPhases()) { 54 return 55 } 56 x := s.stats.GetQueryPhases()[s.pos] 57 if x == nil { 58 return 59 } 60 s.pos++ 61 62 return &queryPhase{ 63 tables: x.GetTableAccess(), 64 pos: 0, 65 duration: time.Microsecond * time.Duration(x.GetDurationUs()), 66 cpuTime: time.Microsecond * time.Duration(x.GetCpuTimeUs()), 67 affectedShards: x.GetAffectedShards(), 68 literalPhase: x.GetLiteralPhase(), 69 }, true 70 } 71 72 // queryPhase holds query execution phase statistics. 73 type queryPhase struct { 74 duration time.Duration 75 cpuTime time.Duration 76 affectedShards uint64 77 tables []*Ydb_TableStats.TableAccessStats 78 pos int 79 literalPhase bool 80 } 81 82 // NextTableAccess returns next accessed table within query execution phase. 83 // 84 // If ok flag is false, then there are no more accessed tables and t is 85 // invalid. 86 func (q *queryPhase) NextTableAccess() (t *stats.TableAccess, ok bool) { 87 if q.pos >= len(q.tables) { 88 return 89 } 90 x := q.tables[q.pos] 91 q.pos++ 92 93 return &stats.TableAccess{ 94 Name: x.GetName(), 95 Reads: initOperationStats(x.GetReads()), 96 Updates: initOperationStats(x.GetUpdates()), 97 Deletes: initOperationStats(x.GetDeletes()), 98 }, true 99 } 100 101 func (q *queryPhase) Duration() time.Duration { 102 return q.duration 103 } 104 105 func (q *queryPhase) CPUTime() time.Duration { 106 return q.cpuTime 107 } 108 109 func (q *queryPhase) AffectedShards() uint64 { 110 return q.affectedShards 111 } 112 113 func (q *queryPhase) IsLiteralPhase() bool { 114 return q.literalPhase 115 } 116 117 func initOperationStats(x *Ydb_TableStats.OperationStats) stats.OperationStats { 118 if x == nil { 119 return stats.OperationStats{} 120 } 121 122 return stats.OperationStats{ 123 Rows: x.GetRows(), 124 Bytes: x.GetBytes(), 125 } 126 }