SQL > SELECT count(*) from SQL > SELECT count(*);

Whether it is a new programmer, or a veteran programmer white, are as always count

The way most people write it today

When reviewing code several times, I found the following:

In business code, you need to query the existence of records based on one or more criteria, regardless of how many records there are. Common SQL and code writing is as follows

#### SQL: SELECT count(*) FROM table WHERE a = 1 AND b = 2 #### If (nums > 0) {// If there is, execute this code} else {// If there is no, execute this code}Copy the code

Does it feel OK, no problem

Recommend some Spring Boot books for yourself:

Docs.qq.com/doc/DQ05qV0…

Optimization scheme

It is recommended as follows:

#### SQL: SELECT 1 FROM table WHERE a = 1 AND b = 2 LIMIT 1 #### Integer exist = xxDao.existXxxxByXxx(params); if ( exist ! = NULL) {// Execute this code if it exists} else {// Execute this code if it does not exist}Copy the code

Instead of using ‘count’, SQL uses’ LIMIT 1 ‘instead of’ count ‘to return a query to the database

Directly determine whether the business code is not empty

conclusion

The more entries are found based on the query criteria, the greater the performance improvement and, in some cases, the reduction in the creation of federated indexes.

Have a harvest, don’t skimp on your retweets and look in.