# Best Practices for Writing Effective SQL
![`smart`](BestPracticesinSQL_aa_004_20231020.png)
----------------------------------------------------
So far, we have explored various aspects of SQL optimization in the TiDB database. To summarize the general process of SQL optimization in TiDB, we have provided a flowchart. With the theoretical knowledge and techniques acquired, along with the flowchart, we can typically devise adequate solutions for SQL performance issues encountered in practical scenarios.

According to the chart, our approach to addressing performance issues with SQL statements is as follows:

1. Investigate whether the execution plan has changed, which can be done using the `Slow Queries` or `SQL Statement` features in the TiDB Dashboard. If the execution plan remains unchanged, consider optimizing indexes and table join algorithms.

2. If the execution plan has changed, check whether the statistics are outdated, which is usually detectable through a health check. If the statistics are outdated (indicated by a low health score), manually gather the statistics.

3. If the statistics are current, it indicates that the optimizer has done its best, and there should be no expectation of a better physical execution plan. At this stage, consider manually optimizing the SQL. Experiment with various `Optimizer Hints` to identify a better execution plan. Essentially, this step involves acting as your own optimizer.

4. If a better execution plan is found using `Optimizer Hints`, employ SQL Plan Management (SPM) to modify the execution plan of the SQL statement in the production environment, thereby completing the SQL optimization process.

Should the aforementioned methods fail to enhance SQL performance, consider optimizing the database system or the application itself.

Among the performance optimization techniques discussed, `Query Optimization` is the one used most frequently. In the lesson on "`Query Optimization`," we compared the advantages and disadvantages of various optimizer algorithms. Next, we will introduce some best practices for `Improving Query Performance`. The subsequent content summarizes insights gained from our daily work experiences.