Summary of development specifications and performance optimization

1. Code specification

1. It is forbidden to query the database (mysql, mongo)
within the loop, which will greatly affect the response speed of the program, even if it is mongo.
2. It is strongly recommended that the number of method lines should not exceed 100 lines.
If the method is too large, it can be greatly reduced. Readability is not conducive to code maintenance. It is not conducive to memory release.
It is recommended to encapsulate the code reasonably.
3. It is forbidden that the interface returns too large data.
The data returned by the interface should be as simple as possible, useless data should not be returned, and data involving safety should not be returned.
It also takes time to receive data on the page. The larger the data received, the slower the page response.
4. It is forbidden to use other people's encapsulation methods by opportunism
. Don't be lazy in the development. You can't say that the ready-made methods have the data you need and use them directly. You need to consider whether this method best meets your needs and does not affect performance. Sometimes opportunism and use doctrine will greatly affect the performance of the system and the development of others.
5. It is forbidden to copy and modify
the interfaces or methods with similar functions. Do not copy and modify directly. You need to study the copied interface or method, whichever is Avoid its shortcomings. Direct copying will have many unexpected problems, such as performance problems caused by incomplete understanding or laziness, redundant code, etc.
6. Redundant code
is prohibited, especially SQL queries that consume more performance. If you don’t need it, delete it in time. Drop.
7. Reasonable naming and writing comments.
8. Need to pay attention to transaction failure scenarios in spring development.
9. It is strongly recommended that only the required data is returned when the query database contains a memory database. To prevent the return of big data, memory overflow may occur in the case of high concurrency.
10. It is recommended to install the Alibaba Java Coding Guideline or SonarLint plug-in in the development tool.
11. It is strongly recommended to be used to non-null verification
12. It is recommended to refer to Ali development specifications for development

Two, mysql specification

1. It is forbidden to operate the database in a loop.
2. It is recommended to add reasonable indexes to the fields that need to be used when designing the table.
Do not wait for the table to be large and add it again. It is a bit late to add when the table is large. If there are a large number of SQLs that do not use index fields , It is troublesome to maintain. Use space for specifications.
Add indexes in advance and standardize development.
3. When creating indexes, consider the overall situation and give priority to adding indexes on commonly used fields. If multiple fields are commonly used, build a combined index first.
Remember that you can't just build an index for the current sql just because a certain sql is slow.
4. A single table does not have too many indexes. Indexes have a greater impact on deletion and update, and consume more space.
5. Use indexes reasonably during database operations.
If it is a composite index, you need to follow the left-most prefix principle.
If multiple index fields are used in a SQL, you need to consider whether there will be a negative optimization problem of index merging.
You also need to bring indexes when deleting and updating to avoid
high concurrency of full table scans . It is forbidden to delete and update using ordinary indexes directly. You need to find out the primary key index and delete according to the primary key index to prevent deadlock.
6. Don't have too many related tables. Up to three.
7. When related tables, use small tables to drive large tables. Mainly. By default, inner join is optimized into small tables to drive large tables.
8. It is strongly recommended that you use explain to view the SQL execution plan. Avoid writing slow sql
9. [Only return the required data when querying, it would be better if index coverage is available] (https://blog.csdn.net/xshsjl/article/details/110560600)
Returning data will also cause performance loss, The more data returned, the more time it will take
10. It is forbidden to query in large quantities. If the query data is too large, query in batches
11. It is recommended to refer to Ali development specifications for development and sql optimization and index optimization of mysql official documents

Three, JPA specification

1. It is forbidden to use findAll (Iterable var1) when querying in batches.
The bottom layer of the query is to traverse findOne to query.

Insert picture description here

2. It is forbidden to use jpa's delete series.
Deleting via Id is to query first and then delete. If it does not exist, an exception will be thrown.
It is recommended to write sql in dao

Insert picture description here

The bulk delete method is circular delete(T entity)

Insert picture description here

It is recommended to perform handwritten sql batch deletion in dao.
3. When saving or updating a large amount of data in batches, the save method
is prohibited. The bottom layer is to use cyclic save to save.
It is recommended to use JDBC for batch storage, especially for large quantities that need to be saved in batches.

Insert picture description here