sql experience writing

Do management system, inevitably involves the design of the database table structure, such as whether it is written or cs bs structure of the structure of the sql statement. Therefore, the development of the system, table structure design is reasonable, whether standard sql statement, whether written sql performance optimization often become the standard measure of the company's technical level programmers.

Our programmers are not dba, sql do not need to always pay attention to the running time, trying to optimize the table structure, storage space, table read speed optimization, etc., but in the development of the system and keep good style of writing sql statement is necessary this relates to the individual's reputation, hey, you know. . .

New programmers veterans, in a development team, we need to show your own level, lay position in the company, trying to be a need, from the simplest to write sql statement easily demonstrated, once there once, an old programmer, team leader above the positioning is done, the first to be made about the experience of a single module, there is a list of sql in the fields and then write a select statement directly taken from the other table, and is not obtained by the correlation between tables, look programmer veterans destroy their glorious image.

Do the technical thing is to focus on their content and promote internal strength, ha ha.

Without further ado, a little summary write sql programmer veterans facile effort it:

1. Whether a sql involved multiple tables, each (result sets) operation, the results obtained with the two new tables, and then the next table (result set) operation.

2. Avoid select f1, (select f2 from tableB) .... from tableA thus obtained field column. A.f1 directly associated with tableA tableB and, B.f2 it.

3. Avoid implicit type conversion
 as
 select id from employee where emp_id = ' 8' ( wrong)
 SELECT Employee ID from the emp_id = 8 WHERE (on)
 the emp_id is an integer, with '8' will default startup type conversion, increased query s expenses.
 
4. Minimize the use of regular expressions, try not to use wildcards.

5. Instead of using the function key
   , such as:
   SELECT Employee ID from the UPPER WHERE (Dept) like 'TECH_DB' (wrong)
   SELECT Employee ID WHERE from the SUBSTR (Dept, l, 4) = 'TECH' (wrong)
   SELECT Employee ID from WHERE dept like 'TECH%' (on)
 
6. Do not use conversion function, as far as possible on the constant used in the fields
  such as:
  SELECT Employee ID from WHERE TO_CHAR (CREATE_DATE, 'YYYY-mm-dd') = '2012-10-31 '(wrong)
  SELECT Employee WHERE from CREATE_DATE ID = TO_DATE (' 2012-10-31 ',' YYYY-mm-dd ') (pair)
 
7. do not use a join query
 as: select id from employee where first_name || last_name like 'Jo%' (wrong)
 
8. avoided both before and after use wildcards
  such as:
  SELECT Employee ID from WHERE Dept like '%% TECH' (wrong)
  SELECT Employee ID from Dept like WHERE 'TECH% '(on)

9. The sequence determination condition
  such as:
  SELECT Employee ID from WHERE creat_date-30> TO_DATE ( '2012-10-31', 'YYYY-mm-dd') (wrong)
    SELECT Employee ID from WHERE creat_date> TO_DATE ( '2012-10 -31 ',' yyyy-mm- dd ') + 30 ( pair)
   
10 instead make use exists in
 course, this should be determined according to the recording exists, or with the use in, is often the case that exists
 SELECT ID from employee where salary in (select salary from emp_level where ....) ( wrong)   
 the SELECT the above mentioned id from the Employee salary the WHERE EXISTS (the SELECT 'the X-' from emp_level the WHERE ....) (of)
 
11. not exists instead of not in use
    and similar to the above
   
12 to reduce the number of records of the lookup table range

13. Use the proper index
  index can improve the speed, in general, the higher the selectivity, the higher the efficiency of the index.


14. The index type
  unique index for the fields used in the query, as far as possible using a unique index.
  There are other types, such as bitmap indexes in the gender field, with only the men and women on the field.

15. index in a column often connected, but not specified for a foreign key

16. index in the column will sort frequently grouped as often as column group by or order by operation.

Establish retrieved more different values ​​in column 17. The conditional expression often used on, is not indexed on fewer different values ​​in the column. Such as the sex column is only male and female two different values, there is no need for indexing (or create bitmap indexes). If the index will not only improve the query efficiency, it will seriously reduce the update speed.

18. When the value of the field to make a relatively small order by, flip disorders occur record problem, to bring order by id field do together.

19. Do not use an empty string query
    such as:
    the SELECT the above mentioned id from the Employee the WHERE emp_name like '%%' (wrong)
   
20. try to do is often used as an index for the group by key fields.

Table 21. correctly associated
    using outer joins replacement efficiency is very low not in operation, greatly improving the operating speed.
    Such as:
    SELECT a.id from Employee A WHERE a.emp_no Not in (SELECT, emp_no, from employee1 WHERE Job = 'the SALE') (wrong)
   
22. Using a temporary table   
   in, where necessary, to reduce the number of read and to be used after temporary table index of speed.
   Such as:
   SELECT Employee e.id from E, D WHERE Dept = d.id e.dept_id and e.empno> 1000 Order by e.id (wrong)
  
   SELECT ID, EMPNO from Employee INTO temp_empl WHERE EMPNO> 1000 by Order ID
   SELECT m.id from temp_emp1 m, dept d where m.empno = d.id ( on)
  
   
 
   
 for a large amount of data sql statement performance optimization more work on to dba to practice, we programmers do these basics just fine.

Published 14 original articles · won praise 0 · Views 215

Guess you like

Origin blog.csdn.net/qq_36283674/article/details/102941857