I heard that wechat search “Java fish” will change strong oh!

This article is posted on Github and Gitee, and contains my entire Java series of articles for study and interview purposes

(1) Preface

Recently, a number of applications have been launched in the whole company, and some problems have been gradually exposed after the launch. In addition to bugs, a point worth paying attention to is the optimization of the system. After all, optimizing the system can not only make the program more stable, but also save some waste of resources. As a very good technical atmosphere of the company, many people will send out their optimization plan and everyone to discuss, but did not expect this discussion to the COMPANY’s CEO and several P8, P9 bigwigs have been blown out.

This article will introduce the common ways of system optimization, as well as some misunderstandings of optimization.

(2) Program optimization

2.1 slow SQL

After the application goes online, we can monitor the slow Sql through various cloud products. Taking Ali Cloud’s products as an example, we can count the slow Sql within a period of time by applying the real-time monitoring service ARMS. Slow Sql is most likely to occur when the Sql does a full table scan without indexing. Check the Sql statement to see the execution process by using the Explain command, build an index if there is no index, and optimize the index if there is one. In addition, we should pay attention to avoid index failure, such as six points will lead to index failure:

  • 1. Do not use composite indexes across columns or out of order (best left prefix) : The indexes are in the same order as the SQL statement queries
  • 2. Composite indexes use full index matching as much as possible
  • 3, do not perform any operations on the index (calculation, function, type conversion)
  • 4, try to start with “constant”, do not start with %, otherwise index invalid
  • 5, try not to use type conversion (explicit, implicit), otherwise index invalid, such as:
The property of name is varchar, which becomes varcharint
select * from teacher where name=123
Copy the code
  • 6, try not to use OR, otherwise index invalid

2.2 Invoke statements to query external services in a loop

In system tuning, we also see calls to query external services or databases in the for loop, such as the following:

for (String user:userList) {
    User resultUser = UserMapper.selectByName(user);
    result.add(resultUser);
}
Copy the code

In a loop, the database query is called multiple times, knowing that each call to the SQL command has network overhead. In this case, you are advised to change a single query to one or several batch queries to save network overhead.

2.3 The Interface Returns only necessary information

This requirement mainly applies to interfaces that use a lot of calls. Some interfaces may return a large amount of data, and some of them are unnecessary, resulting in high network transmission overhead.

The optimization solution is also simple: adjust the returned objects to return only the information you need.

2.4 Asynchronous threads must use thread pools

If asynchronous thread logic is involved in your code, be sure to use thread pools. There is a business on their own open thread, resulting in the process of running the program to open N multithreading, directly the application crashed.

(iii) Product optimization

3.1 Query Added time constraint

Take the big data application we are doing now as an example, the data is always hundreds of millions, at this time to do a good query time range can bring great performance improvement to the system. For example, the ElasticSearch we’re using now, in the production test, lets users only see information from the last three months each time they enter.

Let users choose their own time range if they want to see more. According to the normal usage, the user-defined query time range may be performed only once for every 100 queries, greatly improving efficiency.

3.2 Prevent users from deep paging

There are two main types of front-end pages in common use. The first is to show the user the first and last pages, such as the following:

This kind of paging is only suitable for applications with a small amount of data. Another kind of paging method is to show only the front and back pages of the current page without giving users the opportunity to jump directly to the last page, such as Baidu’s paging:

It’s important to remember that no matter what kind of database pages are, the deeper the pages, the less efficient they are. Try not to let deep paging occur during production testing.

3.3 Constraints on Batch Operation Services

Many products like to design some cool features, such as select all download without setting any restrictions. This is technically very unreasonable design, on this point, it is best to communicate with the product, batch operation set check upper limit.

(4) Misunderstanding of system optimization

4.1 Use multithreading to improve efficiency

In most people’s minds, switching from single-threaded to multi-threaded is always more efficient, but it was this discussion that brought out the CEO and several P8 and P9 bosses. The consensus of these executives, who have worked in Ali for more than ten years, is that when the system is optimized, do not easily use multi-threading. You think you have improved efficiency, but in fact, you may have buried a lot of holes.

Their conclusions come from years of experience, whether it’s HTTP calls, Tomcat embedded in SpringBoot; Or RPC dubbo calls, which themselves maintain thread pools. So it’s really not recommended to have multithreading in service methods unless the RT times of business calls are intolerable. In addition to the uncertainty of code maintenance, there is not much improvement in QPS supported by the overall CPU of the machine.

One P8 said he used to work in a department that liked to add multiple threads to the code. As a result, after the code was written, a group of people were hired to monitor how the code worked, and then another group of people were hired to optimize the code. It takes time to gain no pleasure.

(5) Summary

System optimization is not a lofty technology, in fact, is a lot of basic knowledge. But it is also important to ensure the stability of the system and the saving of resources. In addition, the purpose of system optimization is to make the application run more stable. If the application becomes uncertain due to system optimization, the loss is outweighed by the gain.

Finally, I wish all colleagues 1024 programmers happy festival, I am fish, we will see you next time.