Automated testing of SQL to find out if "exists", stop counting, it is time-consuming

According to a certain condition, query "
Yes " and "No" from the database table, and there are only two states, so why do you need to select count(*) when writing SQL? Whether it is a new star programmer or a superb The programmer Lao Bai who has been on the battlefield for many years has the same count as always

At present, most people's writing method
has repeatedly reviewed the code, and found the phenomenon as it is: In the business code, it is necessary to query whether there are records based on one or more conditions, and do not care how many records there are. Common SQL and code writing are as follows

  ##### SQL写法: 
  SELECT count(*) FROM table WHERE a = 1 AND b = 2 
   
  ##### Java写法: 
  int nums = xxDao.countXxxxByXxx(params); 
  if ( nums > 0 ) {
    
     
    //当存在时,执行这里的代码 
  } else {
    
     
    //当不存在时,执行这里的代码 
  } 

Does it feel OK? There is no problem to
 
optimize the solution. The
recommended writing is as follows:

 ##### SQL写法: 
  SELECT 1 FROM table WHERE a = 1 AND b = 2 LIMIT 1 
   
  ##### Java写法: 
  Integer exist = xxDao.existXxxxByXxx(params); 
  if ( exist != NULL ) {
    
     
    //当存在时,执行这里的代码 
  } else {
    
     
    //当不存在时,执行这里的代码 
  } 

SQL no longer uses count, but instead uses LIMIT 1, so that when the database query encounters one, it will return. Don't continue to find how many more ones are left. Just judge whether it is not empty in the business code.

Summary
The more the number of items found according to the query conditions, the more obvious the performance improvement, and in some cases, the creation of joint indexes can be reduced.

Recommend good articles:

What kind of person is suitable for software testing?

Knowledge to understand python automated testing(3)

Which is more suitable for automated testing, Python or Java?

The daily work of software testers

Play with Python+Selenium automated testing in 10 minutes, and teach you a quick start!

10 years of software testing engineers' perceptions-to friends who are still in confusion

Finally: Welcome to follow the editor to receive a summary of the core knowledge of Python automated test engineers with a 300-page pdf document! Software testing technology exchange group: (313782132) The content of these materials are all the knowledge points that the interviewer must ask during the interview. The chapter includes many knowledge points, including basic knowledge, Linux essentials, Shell, Internet program principles, Mysql Database, package capture tool topics, interface testing tools, advanced testing-Python programming, Web automation testing, APP automation testing, interface automation testing, advanced continuous integration testing, testing architecture development testing framework, performance testing, security testing, etc.

Guess you like

Origin blog.csdn.net/weixin_50271247/article/details/108491066