diff --git a/sql-statements/sql-statement-explain-analyze.md b/sql-statements/sql-statement-explain-analyze.md index 90bf6175318bf..7e611e654d21e 100644 --- a/sql-statements/sql-statement-explain-analyze.md +++ b/sql-statements/sql-statement-explain-analyze.md @@ -293,6 +293,51 @@ RU:273.842670 > > This value shows the actual RUs consumed by this execution. The same SQL statement might consume different amounts of RUs each time it is executed due to the effects of caching (for example, [coprocessor cache](/coprocessor-cache.md)). +You can calculate the RU from the other values in `EXPLAIN ANALYZE`, specifically the `execution info` column. For example: + +```json +'executeInfo': + time:2.55ms, + loops:2, + RU:0.329460, + Get:{ + num_rpc:1, + total_time:2.13ms + }, + total_process_time: 231.5µs, + total_wait_time: 732.9µs, + tikv_wall_time: 995.8µs, + scan_detail: { + total_process_keys: 1, + total_process_keys_size: 150, + total_keys: 1, + get_snapshot_time: 691.7µs, + rocksdb: { + block: { + cache_hit_count: 2, + read_count: 1, + read_byte: 8.19 KB, + read_time: 10.3µs + } + } + }, +``` + +The base costs are defined in the [`tikv/pd` source code](https://github.com/tikv/pd/blob/aeb259335644d65a97285d7e62b38e7e43c6ddca/client/resource_group/controller/config.go#L58C19-L67) and the calculations are performed in the [`model.go`](https://github.com/tikv/pd/blob/54219d649fb4c8834cd94362a63988f3c074d33e/client/resource_group/controller/model.go#L107) file. + +If you are using TiDB v7.1, the calculation is the sum of `BeforeKVRequest()` and `AfterKVRequest()` in `pd/pd-client/model.go`, that is: + +``` +before key/value request is processed: + consumption.RRU += float64(kc.ReadBaseCost) -> kv.ReadBaseCost * rpc_nums + +after key/value request is processed: + consumption.RRU += float64(kc.ReadBytesCost) * readBytes -> kc.ReadBytesCost * total_process_keys_size + consumption.RRU += float64(kc.CPUMsCost) * kvCPUMs -> kc.CPUMsCost * total_process_time +``` + +For writes and batch gets, the calculation is similar with different base costs. + ### Other common execution information The Coprocessor operators usually contain two parts of execution time information: `cop_task` and `tikv_task`. `cop_task` is the time recorded by TiDB, and it is from the moment that the request is sent to the server to the moment that the response is received. `tikv_task` is the time recorded by TiKV Coprocessor itself. If there is much difference between the two, it might indicate that the time spent waiting for the response is too long, or the time spent on gRPC or network is too long.