diff --git a/distsql/select_result.go b/distsql/select_result.go index 5c4b83560787b..300996fddf517 100644 --- a/distsql/select_result.go +++ b/distsql/select_result.go @@ -326,6 +326,7 @@ type selectResultRuntimeStats struct { totalProcessTime time.Duration totalWaitTime time.Duration rpcStat tikv.RegionRequestRuntimeStats + CoprCacheHitNum int64 } func (s *selectResultRuntimeStats) mergeCopRuntimeStats(copStats *tikv.CopRuntimeStats, respTime time.Duration) { @@ -338,6 +339,9 @@ func (s *selectResultRuntimeStats) mergeCopRuntimeStats(copStats *tikv.CopRuntim s.totalProcessTime += copStats.ProcessTime s.totalWaitTime += copStats.WaitTime s.rpcStat.Merge(copStats.RegionRequestRuntimeStats) + if copStats.CoprCacheHit { + s.CoprCacheHitNum++ + } } func (s *selectResultRuntimeStats) String() string { @@ -391,6 +395,8 @@ func (s *selectResultRuntimeStats) String() string { buf.WriteString(strconv.FormatInt(copRPC.Count, 10)) buf.WriteString(", rpc_time: ") buf.WriteString(time.Duration(copRPC.Consume).String()) + buf.WriteString(fmt.Sprintf(", copr_cache_hit_ratio: %v", + strconv.FormatFloat(float64(s.CoprCacheHitNum)/float64(len(s.copRespTime)), 'f', 2, 64))) } buf.WriteString("}") diff --git a/planner/core/cbo_test.go b/planner/core/cbo_test.go index 42e85aa568880..2bd17ee0dbfcc 100644 --- a/planner/core/cbo_test.go +++ b/planner/core/cbo_test.go @@ -98,6 +98,9 @@ func (s *testAnalyzeSuite) TestExplainAnalyze(c *C) { execInfo := row[5].(string) c.Assert(strings.Contains(execInfo, "time"), Equals, true) c.Assert(strings.Contains(execInfo, "loops"), Equals, true) + if strings.Contains(row[0].(string), "Reader") || strings.Contains(row[0].(string), "IndexLookUp") { + c.Assert(strings.Contains(execInfo, "copr_cache_hit_ratio"), Equals, true) + } } } diff --git a/store/tikv/coprocessor.go b/store/tikv/coprocessor.go index 69e3d18a3cb11..c00b6416b7f6f 100644 --- a/store/tikv/coprocessor.go +++ b/store/tikv/coprocessor.go @@ -1056,6 +1056,7 @@ func (worker *copIteratorWorker) handleCopResponse(bo *Backoffer, rpcCtx *RPCCon data := make([]byte, len(cacheValue.Data)) copy(data, cacheValue.Data) resp.pbResp.Data = data + resp.detail.CoprCacheHit = true } else { // Cache not hit or cache hit but not valid: update the cache if the response can be cached. if cacheKey != nil && resp.pbResp.CanBeCached && resp.pbResp.CacheLastVersion > 0 { @@ -1081,6 +1082,8 @@ func (worker *copIteratorWorker) handleCopResponse(bo *Backoffer, rpcCtx *RPCCon type CopRuntimeStats struct { execdetails.ExecDetails RegionRequestRuntimeStats + + CoprCacheHit bool } func (worker *copIteratorWorker) handleTiDBSendReqErr(err error, task *copTask, ch chan<- *copResponse) error {