Base of additions and deletions, database optimization, index

Source: Internet
Author: User

Features of MySQL
    1. relational database, free to use,
    2. Plug-in storage engine,
    3. High Performance,
Basic additions and deletions to the search
  1. DDL statements, data definition statements

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    CreateDatabase test1;
    DropDatabase test1;
    Use test1;
    CreateTable EMP (enamevarchar), HireDateDate,salDecimal10,2), Deptnoint2));
    DropTable emp;
    AlterTable empModify Enamevarchar20);
    AlterTable empAddColumn Ageint3);
    AlterTable empDropcolumn age;
    alter table emp change age age1 int (4);
    alter table emp add birth date after ename;
    alter table emp modify age int (3) first;
    alter table emp rename emp1;
    /span>
  2. DML statements, data manipulation statements

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    Insertinto EMP (ENAME,HIREDATE,SAL,DEPTNO)Values' Zzx1 ',' 2000-10-11 ',2000,1);
    InsertInto EMPValues' Lisa ',' 2004-05-09 ',3000,2);
    InsertInto DeptValues5,' Dept5 '), (6,' Dept6 ');
    Update empSet sal=4000where ename=' Lisa ';
    Update EMP A,dept bSet A.sal=a.sal*b.deptno,b.deptname=a.enamewhere A.deptno=b.deptno;
    DeleteFrom EMPwhere ename=' Dony ';
    Delete A, bFrom EMP a,dept bwhere A.deptno=b.deptnoand a.deptno=3;
    SELECT *From EMPwhere ename=' Lisa ';
    select distinct deptno span class= "keyword" >from emp;
    select * from emp order by sal (desc);
    select * from emp order by sal limit 5;
    select * from emp order by sal limit 1, 5;ss
  3. DCL statement, Data Control statement

SQL optimization
  1. Use Preparestatement (Java) as much as possible to take advantage of preprocessing capabilities.
  2. When making multiple records additions, modifications, and deletions, it is recommended to use the batch processing function, the number of batches to the whole
    The SQL statement does not exceed the SQL statement size limit for the corresponding database.
  3. It is recommended that the number of elements in the in of each SQL statement be below 200, and if the number is more than one, split into multiple
    SQL statement. It is forbidden to use XX in ("," ...) or xx in (', ', '). ★
  4. Prohibit the use of or more than 200, such as XX = ' 123 ' or xx= ' 456 '. ★
  5. Try not to use outer joins.
  6. The not exist is not recommended for use in statements. ★
  7. Prohibit the use of Union and, if there is a business need, split into two queries. ★
  8. Prohibit the use of more than 3 layers of nested queries in an SQL statement, and if so, consider using temporary tables or
    Intermediate result set.
  9. Try to avoid taking the same number of >= from 4 tables in one SQL statement, for only the filter condition,
    But does not involve the number of tables, not participating in the calculation of the number of tables
  10. Any action on a column in the query condition will result in a table scan, so you should try to make the database function, computation expression
    Write to the right of the logical operator.
  11. When comparing char types, it is recommended that you do not use the RTrim () function, which should be insufficient in the program
    Qi.
  12. Replace the EXISTS clause with a multi-table join.
  13. If there is a multi-table connection, there should be a master-slave point, and try to take the number from a table, such as select A.col1, A.col2
    From a join B on a.col3=b.col4 where b.col5 = ' a '.
  14. When using like, it is recommended that one side of like is a string, and the table column appears on the side.
  15. The condition of the WHERE clause is not allowed to be placed in the having.
  16. Drop the update operation to the last execution of the transaction. Such as
  17. When a transaction needs to update multiple objects, it is necessary to ensure that the order of the updates is consistent to prevent deadlocks from occurring. If always first
    Update the child table and update the main table, according to the Inventory file batch update the existing quantity, the incoming inventory file PK into
    Sorting, and then update processing.
  18. It is forbidden to use temporal tables arbitrarily, and temporary tables are forbidden when the temporary data does not exceed 200 rows.
  19. It is forbidden to use distinct arbitrarily and avoid unnecessary sorting.
Index optimization
    1. Create an index, delete an index

      1
      2
      On the City (city (10));
      On the city;
    2. The index column of the search is best in the where sentence or the join clause

    3. Using a unique index
    4. Use a short index, for longer fields, to index with its prefix
    5. Do not over-use indexes, which cause additional performance overhead and maintenance

Base of additions and deletions, database optimization, index

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.