Add performance diagnose rule to check useless prepare statement
#18,743 创建于 2020年7月23日
仓库指标
- Star
- (40,090 star)
- PR 合并指标
- (平均合并 14天 4小时) (30 天内合并 346 个 PR)
描述
Feature Request


As you can see, the client try to use the prepared statement, but the prepare statement should only compile once.
If the client has to prepare again before each execution, actually the performance is worse than not use prepare.
This Issue wants to add a diagnose rule to auto find this problem by diagnose.
How to diagnose?
You can simply check the statement count of prepare and execute. Normally, the statement count of execute should much more than the statement count of prepare.
And you can use the metrics tables to get this information.
here is a SQL you can use to diagnose:
select t1.execute/t2.prepare > 10 from (select avg(value) as execute from tidb_ops_statement where type='Execute' and time>= '2020-07-23 17:30:00' and time < '2020-07-23 17:35:00') as t1,(select avg(value) as prepare from tidb_ops_statement where type='Prepare' and time>= '2020-07-23 17:30:00' and time < '2020-07-23 17:35:00') as t2;
+----------------------------+
| t1.execute/t2.prepare > 10 |
+----------------------------+
| 0 |
+----------------------------+
If the upper SQL returns 0, you can output a warning like:
The `Execute` statements count should much more than the `Prepare` statements count. otherwise, the performance may be worse than not use prepared statements.
Describe alternatives you've considered: