SQL statement optimization approach

  • * Do not use the number for query operations, the use of specific fields.
  • index
    • Add an index on the field where clause and order by involving reasonable.
  • where clause optimization
    • Analyzing to avoid a null value in the where clause, the default value field is set to respond
      SELECT ID from T WHERE NUM IS  null  
      can set the default value to a null value - . 1 (set according to the actual situation) 
      is determined null can 
      SELECT   ID from T WHERE NUM =  - . 1
    • In the where clause to avoid the use! = Or <> operator.
    • Where clause to avoid or to connection condition
      select id from t where num = 1 or num = 5
      /*可以优化为*/
      select id from t where num = 1
      unicon all
      select id form t where num = 5
    • and should be used with caution in not in
      / * Continuous conditions * / 
      SELECT ID from T WHERE NUM in ( . 1 , 2 , . 3 )
       / * use and BETWEEN * / 
      SELECT ID from T WHERE NUM BETWEEN  . 1  and  . 3 
      / * more may be used in place exists in * / 
      SELECT NUM from A WHERE NUM in ( SELECT NUM from T)
       / * alternative statement * / 
      SELECT NUMfrom a where EXISTS (select num from b where a.num = b.num)
    • Fuzzy query SQL optimization
    • / * Under normal circumstances, the percent sign index may be used later * / 
      SELECT   Nickname from T WHERE   Nickname like  ' the DBA% ' 
      / * percent sign in front of the index can not be used, solutions rewritten sql, adding reverse index * / 
      Create  index idx_t1_name ON T1 ( Reverse (name))
       SELECT name   from T1 wHERE  Reverse (name) like  Reverse ( ' % ADC ' );
       / * both before and after a percent, this is generally not use an index. * / 
      / * 1. the search condition character string is always in a fixed position start string, in the cable to optimize the function can be created, to create subStr index function, and then use like 'abc%' example:* / 
      Create  index idx_substr_t1_name ON T1 (substr (name, . 5 , 10 ));
       SELECT ID, name, name_type from T1 WHERE substr (name, . 5 , 10 ) like  ' ABC% ' ;
       / * 2. search condition character always a fixed location at the end of the string occurs, a combination index can be created to optimize the function, create reverse + substr combination index function, and then using Reverse like 'ABC%' * / 
      Create  index idx_t1_reverse_name ON T1 ( Reverse (substr (name, . 1 , length (name) - . 4 ))); 
      
      SELECTID, name, name_type from T1 WHERE  Reverse (substr (name, . 1 , length (name) - . 4 )) like  Reverse ( ' % ABC ' ) 
      
      / * 3. a fixed position search string do not appear, optimization, first establish a common index column rewritten SQL * / 
      Create  index idx_t1_name ON T1 (name) 
      
      SELECT ID, name, name_type from T1 WHERE name in ( SELECT name from T1 WHERE name like  ' %% ABC ' )
    • Eliminating function, an arithmetic or other operation on the expression '=' in the left side of the field where clause
    • Do not select counut (*) from table, count with no such conditions will cause full table scan, use count (1) in place of

Guess you like

Origin www.cnblogs.com/shar-wang/p/11617855.html