Practical Java knowledge points and practical Java knowledge points
1. For some tasks that require asynchronous processing, do not create a new thread directly. The thread pool should be used. When using the thread pool, you should set a reasonable number of threads. Generally, the maximum number is not more than 50. Of course, you also need to consider your IO and CPU.
2. Branch judgment if... When else is used, the most common condition processing is put in front.
3. When the object is compared, the constant is placed in front to reduce the appearance of null pointers.
4. Reduce the amount of synchronized code to be processed, and try to put it out.
Database:
5. Generally, the query is slow. It is likely that no index is created or the index is useless. Check more.
6. join queries for two large tables can be replaced by secondary database access. First, the data of Table A is found, and then the associated fields are used to query the data of Table B. Do not think about an SQL statement as best.
7. resolutely avoid querying the full table data or a large amount of data, and loading the returned list to the memory. I accidentally checked million data and found the data frequently. The memory burst. If this risk occurs, change it to paging query.
8. Avoid long connections or long transactions in the transaction. In this case, occupying data connections will affect the performance. Some unnecessary logic can be put out of the transaction for execution.
9. Put the field addition, subtraction, multiplication, division, and division into SQL, strictly avoiding get processing first, and then computation in the set to the database. The concurrency is very easy to cause distortion.
10. The code in the method should not be too long. Pay attention to encapsulation, name semantics, and neat code.
SQL specifications and performance optimization Mysql11. SQL Execution sequence: a. Check whether the syntax is correct. B. Check whether the table exists and whether the permissions are met. C. generate a better execution plan based on statistical information (such as data length, rows, index length, and index uniqueness. D. perform data retrieval, filtering, merging, and sorting based on the execution plan. During data access, if table data exists in the memory, perform operations directly. Otherwise, read table data from the tape, put the data into the memory, and then perform operations. If the memory is insufficient, then, the cold data in the memory is pushed out of the memory, and then the data is read from the memory. 12. What is an execution plan? a. determines how to access table data, whether to use indexes, and whether to sort table data. B. Which table is accessed first. C. Which connection method is used for multi-table join, but currently MySQL only has nested connections. (Nested loop, as the name implies, is to take a table as the starting point, and traverse all records of another table one by one) 13. index: if an index is used during query, the efficiency can be improved, because after an index is created, it can be understood as the structure storage of the data dictionary (such as the directory in the book ), therefore, conditional queries are more efficient. Next, let's take a look at the frequently-used index types in MySQL. A. Common Index: when creating a common index, no restrictions are attached. This type of index can be created in any data type. Whether its value is unique or not is determined by the integrity constraints of the field. After an index is created, you can use the index to query it. For example, create a normal index on the stu_id field of the student table. You can query a record based on the index. B. UNIQUE index: You can use the UNIQUE parameter to set an index as a UNIQUE index. When creating a unique index, the value of the index must be unique. For example, if you create a unique index in the stu_name field of the student table, the value of the stu_name field must be unique. A unique index can be used to quickly determine a record. A primary key is a special unique index. C. Single-Column index: Create an index on a single field in the table. A single column index is indexed only based on this field. A single-column index can be a common index, a unique index, or a full-text index. Make sure that the index corresponds to only one field. D. Multi-column Index: A Multi-column index creates an index on multiple fields in the table. This index points to multiple fields corresponding to the creation. You can query these fields. However, the index is used only when the first field is used in the query condition. For example, if you create an index with multiple columns on the id, name, and sex fields of a table, the index is used only when the id field is used in the query condition. There are also spatial indexes and full-text indexes, which are rarely used at ordinary times. 14. Generally, there should be no more than five indexes in a table, and duplicate indexes should be avoided, and no index is created. The index will take effect if the index is queried Based on the index field conditions. 15. Common scenarios that will cause index failure: a. Use the like keyword to match the first character string with "%. B. The condition contains the or, in, not in, and <> keywords, which are not indexed by default. C. When the number of data rows in the Access Table exceeds 30% of the total number of records in the table, the data rows are scanned in the whole table. D. query conditions use functions on the index column or perform operations on the index column. E. In multi-column indexes, the first index column uses range query and can only use partial or unavailable indexes. F. In multi-column indexes, the first query condition is not the leftmost index column. 16. Two indexes cannot be used simultaneously. One is used to filter data, and the other is used to sort data (except for primary keys ). 17. If the DML statement uses an index, it will lock the entire table. If a non-unique index is used, it may only lock a certain range. Therefore, we recommend that you use indexes whenever possible to update/delete data. If you can use primary keys or unique indexes, you must submit transactions in a timely manner. 18. view the execution plan and analyze the SQL Performance. Reference: http://www.cnblogs.com/xiaoboluo768/p/5400990.html19. log records should be placed in independent transactions as much as possible to avoid log loss caused by subsequent exceptions. 20.
TransientModifying this element does not perform jvm default serialization. You can complete the serialization of this element by yourself. 21. It is best to generalize the same variables used for multiple times into constants. 22. Try to avoid using fewer method calls in the loop, which can save the creation of the method stack. For (int I = 0; I <list. size (); I ++) {System. out. println (I);} can be changed to: for (int I = 0, size = list. size (); I <size; I ++) {System. out. println (I);} 23. use the basic data type as the local variable type, because the variables of the basic type are stored in the stack, the packaging class variables are in the heap, And the stack operation speed is much faster than the heap. 24. Assign the unused variable reference to null as soon as possible, which can help jvm to recycle memory more quickly,
Release memory.25. Use of referenced values and strings in mybate <# if sortBy?> $ {"Order by $ {sortBy}"} </# if>