Add performance diagnose rule to check useless prepare statement
#18,743 opened on Jul 23, 2020
Repository metrics
- Stars
- (40,090 stars)
- PR merge metrics
- (Avg merge 14d 4h) (346 merged PRs in 30d)
Description
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: