Skip to content

Commit

Permalink
execdetails: Display vector search info in EXPLAIN ANALYZE (#56478)
Browse files Browse the repository at this point in the history
ref #54245
  • Loading branch information
breezewish authored Oct 8, 2024
1 parent 6122947 commit c3a85be
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 3 deletions.
61 changes: 58 additions & 3 deletions pkg/util/execdetails/execdetails.go
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,16 @@ func (crs *CopRuntimeStats) RecordOneCopTask(address string, summary *tipb.Execu
minRemoteStreamMs: summary.GetTiflashScanContext().GetMinRemoteStreamMs(),
maxRemoteStreamMs: summary.GetTiflashScanContext().GetMaxRemoteStreamMs(),
regionsOfInstance: make(map[string]uint64),

totalVectorIdxLoadFromS3: summary.GetTiflashScanContext().GetTotalVectorIdxLoadFromS3(),
totalVectorIdxLoadFromDisk: summary.GetTiflashScanContext().GetTotalVectorIdxLoadFromDisk(),
totalVectorIdxLoadFromCache: summary.GetTiflashScanContext().GetTotalVectorIdxLoadFromCache(),
totalVectorIdxLoadTimeMs: summary.GetTiflashScanContext().GetTotalVectorIdxLoadTimeMs(),
totalVectorIdxSearchTimeMs: summary.GetTiflashScanContext().GetTotalVectorIdxSearchTimeMs(),
totalVectorIdxSearchVisitedNodes: summary.GetTiflashScanContext().GetTotalVectorIdxSearchVisitedNodes(),
totalVectorIdxSearchDiscardedNodes: summary.GetTiflashScanContext().GetTotalVectorIdxSearchDiscardedNodes(),
totalVectorIdxReadVecTimeMs: summary.GetTiflashScanContext().GetTotalVectorIdxReadVecTimeMs(),
totalVectorIdxReadOthersTimeMs: summary.GetTiflashScanContext().GetTotalVectorIdxReadOthersTimeMs(),
}}, threads: int32(summary.GetConcurrency()),
totalTasks: 1,
}
Expand Down Expand Up @@ -960,6 +970,16 @@ type TiFlashScanContext struct {
minRemoteStreamMs uint64
maxRemoteStreamMs uint64
regionsOfInstance map[string]uint64

totalVectorIdxLoadFromS3 uint64
totalVectorIdxLoadFromDisk uint64
totalVectorIdxLoadFromCache uint64
totalVectorIdxLoadTimeMs uint64
totalVectorIdxSearchTimeMs uint64
totalVectorIdxSearchVisitedNodes uint64
totalVectorIdxSearchDiscardedNodes uint64
totalVectorIdxReadVecTimeMs uint64
totalVectorIdxReadOthersTimeMs uint64
}

// Clone implements the deep copy of * TiFlashshScanContext
Expand Down Expand Up @@ -995,6 +1015,16 @@ func (context *TiFlashScanContext) Clone() TiFlashScanContext {
minRemoteStreamMs: context.minRemoteStreamMs,
maxRemoteStreamMs: context.maxRemoteStreamMs,
regionsOfInstance: make(map[string]uint64),

totalVectorIdxLoadFromS3: context.totalVectorIdxLoadFromS3,
totalVectorIdxLoadFromDisk: context.totalVectorIdxLoadFromDisk,
totalVectorIdxLoadFromCache: context.totalVectorIdxLoadFromCache,
totalVectorIdxLoadTimeMs: context.totalVectorIdxLoadTimeMs,
totalVectorIdxSearchTimeMs: context.totalVectorIdxSearchTimeMs,
totalVectorIdxSearchVisitedNodes: context.totalVectorIdxSearchVisitedNodes,
totalVectorIdxSearchDiscardedNodes: context.totalVectorIdxSearchDiscardedNodes,
totalVectorIdxReadVecTimeMs: context.totalVectorIdxReadVecTimeMs,
totalVectorIdxReadOthersTimeMs: context.totalVectorIdxReadOthersTimeMs,
}
for k, v := range context.regionsOfInstance {
newContext.regionsOfInstance[k] = v
Expand All @@ -1003,6 +1033,15 @@ func (context *TiFlashScanContext) Clone() TiFlashScanContext {
}

func (context *TiFlashScanContext) String() string {
var output []string
if context.totalVectorIdxLoadFromS3+context.totalVectorIdxLoadFromDisk+context.totalVectorIdxLoadFromCache > 0 {
var items []string
items = append(items, fmt.Sprintf("load:{total:%dms,from_s3:%d,from_disk:%d,from_cache:%d}", context.totalVectorIdxLoadTimeMs, context.totalVectorIdxLoadFromS3, context.totalVectorIdxLoadFromDisk, context.totalVectorIdxLoadFromCache))
items = append(items, fmt.Sprintf("search:{total:%dms,visited_nodes:%d,discarded_nodes:%d}", context.totalVectorIdxSearchTimeMs, context.totalVectorIdxSearchVisitedNodes, context.totalVectorIdxSearchDiscardedNodes))
items = append(items, fmt.Sprintf("read:{vec_total:%dms,others_total:%dms}", context.totalVectorIdxReadVecTimeMs, context.totalVectorIdxReadOthersTimeMs))
output = append(output, "vector_idx:{"+strings.Join(items, ",")+"}")
}

regionBalanceInfo := "none"
if len(context.regionsOfInstance) > 0 {
maxNum := uint64(0)
Expand Down Expand Up @@ -1031,8 +1070,9 @@ func (context *TiFlashScanContext) String() string {
if context.minRemoteStreamMs != 0 || context.maxRemoteStreamMs != 0 {
remoteStreamInfo = fmt.Sprintf("min_remote_stream:%dms, max_remote_stream:%dms, ", context.minRemoteStreamMs, context.maxRemoteStreamMs)
}

// note: "tot" is short for "total"
return fmt.Sprintf("tiflash_scan:{"+
output = append(output, fmt.Sprintf("tiflash_scan:{"+
"mvcc_input_rows:%d, "+
"mvcc_input_bytes:%d, "+
"mvcc_output_rows:%d, "+
Expand Down Expand Up @@ -1089,7 +1129,9 @@ func (context *TiFlashScanContext) String() string {
context.totalDmfileRsCheckMs,
context.totalDmfileReadMs,
dmfileDisaggInfo,
)
))

return strings.Join(output, ", ")
}

// Merge make sum to merge the information in TiFlashScanContext
Expand Down Expand Up @@ -1120,6 +1162,16 @@ func (context *TiFlashScanContext) Merge(other TiFlashScanContext) {
context.totalBuildInputStreamMs += other.totalBuildInputStreamMs
context.staleReadRegions += other.staleReadRegions

context.totalVectorIdxLoadFromS3 += other.totalVectorIdxLoadFromS3
context.totalVectorIdxLoadFromDisk += other.totalVectorIdxLoadFromDisk
context.totalVectorIdxLoadFromCache += other.totalVectorIdxLoadFromCache
context.totalVectorIdxLoadTimeMs += other.totalVectorIdxLoadTimeMs
context.totalVectorIdxSearchTimeMs += other.totalVectorIdxSearchTimeMs
context.totalVectorIdxSearchVisitedNodes += other.totalVectorIdxSearchVisitedNodes
context.totalVectorIdxSearchDiscardedNodes += other.totalVectorIdxSearchDiscardedNodes
context.totalVectorIdxReadVecTimeMs += other.totalVectorIdxReadVecTimeMs
context.totalVectorIdxReadOthersTimeMs += other.totalVectorIdxReadOthersTimeMs

if context.minLocalStreamMs == 0 || other.minLocalStreamMs < context.minLocalStreamMs {
context.minLocalStreamMs = other.minLocalStreamMs
}
Expand Down Expand Up @@ -1150,7 +1202,10 @@ func (context *TiFlashScanContext) Empty() bool {
context.dmfileLmFilterScannedRows == 0 &&
context.dmfileLmFilterSkippedRows == 0 &&
context.localRegions == 0 &&
context.remoteRegions == 0
context.remoteRegions == 0 &&
context.totalVectorIdxLoadFromDisk == 0 &&
context.totalVectorIdxLoadFromCache == 0 &&
context.totalVectorIdxLoadFromS3 == 0
return res
}

Expand Down
12 changes: 12 additions & 0 deletions pkg/util/execdetails/execdetails_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,18 @@ func TestCopRuntimeStatsForTiFlash(t *testing.T) {
require.True(t, stats.ExistsRootStats(tableReaderID))
}

func TestVectorSearchStats(t *testing.T) {
stats := NewRuntimeStatsColl(nil)

var v uint64 = 1

execSummary := mockExecutorExecutionSummaryForTiFlash(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "")
execSummary.DetailInfo.(*tipb.ExecutorExecutionSummary_TiflashScanContext).TiflashScanContext.TotalVectorIdxLoadFromS3 = &v
stats.RecordOneCopTask(1, "tiflash", "8.8.8.8", execSummary)
s := stats.GetOrCreateCopStats(1, "tiflash")
require.Equal(t, "tiflash_task:{time:0s, loops:0, threads:0}, vector_idx:{load:{total:0ms,from_s3:1,from_disk:0,from_cache:0},search:{total:0ms,visited_nodes:0,discarded_nodes:0},read:{vec_total:0ms,others_total:0ms}}, tiflash_scan:{mvcc_input_rows:0, mvcc_input_bytes:0, mvcc_output_rows:0, lm_skip_rows:0, local_regions:0, remote_regions:0, tot_learner_read:0ms, region_balance:none, delta_rows:0, delta_bytes:0, segments:0, stale_read_regions:0, tot_build_snapshot:0ms, tot_build_bitmap:0ms, tot_build_inputstream:0ms, min_local_stream:0ms, max_local_stream:0ms, dtfile:{data_scanned_rows:0, data_skipped_rows:0, mvcc_scanned_rows:0, mvcc_skipped_rows:0, lm_filter_scanned_rows:0, lm_filter_skipped_rows:0, tot_rs_index_check:0ms, tot_read:0ms}}", s.String())
}

func TestRuntimeStatsWithCommit(t *testing.T) {
commitDetail := &util.CommitDetails{
GetCommitTsTime: time.Second,
Expand Down

0 comments on commit c3a85be

Please sign in to comment.