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
Writing SQL:
SELECT count(*) FROM table WHERE a = 1 AND b = 2
Java write:
int nums = xxDao.countXxxxByXxx(params); If (nums > 0) {// If there is, execute this code} else {// If there is no, execute this code}
Does it feel OK, no problem
Optimization scheme
It is recommended as follows:
Writing SQL:
SELECT 1 FROM table WHERE a = 1 AND b = 2 LIMIT 1
Java write:
Integer exist = xxDao.existXxxxByXxx(params); if ( exist ! = NULL) {// Execute this code if it exists} else {// Execute this code if it does not exist}
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
Java has a wide range of knowledge, and interview questions cover a wide range of topics, including: Java foundation, Java concurrency, JVM, MySQL, data structures, algorithms, Spring, microservices, MQ, etc., involved in the knowledge of how huge, so we often don’t know how to start in the review, today small editor to bring you a set of Java interview questions, questions library is very comprehensive. Java base, Java Collections, JVM, Java concurrency, Spring buckets, Redis, MySQL, Dubbo, Netty, MQ, etc., including Java back-end knowledge 2000 +, part of the following:
Information access: public account: “Programmer Bai Nannan” to obtain the above information