Twenty-four road Python []: Database Index

Database Index

I. Introduction to Indexes

Index in mysql also called "key", is a data structure storage engine used to quickly find the record. Index for good performance is critical, especially when the amount of data in the table more and more, more important index for the performance;

 

Index optimization should be the most effective means to optimize the performance of the query;

Index can easily improve query performance by several orders of magnitude;

Dictionary index is equivalent to the sequencer table to check if a new word, if you do not use the sequencer table, you need to find one from the hundreds of pages, the efficiency is too low.

 

Index features: create and maintain an index will consume a lot of time and disk space, but the query speed greatly improved;

Second, the index syntax

- When you create a table
 - Syntax: 
    the CREATE TABLE table name ( 
                field name 1 data type [integrity constraints ...], 
                field name 2 Data Type [integrity constraints ...], 
                [UNIQUE | FULLTEXT | the SPATIAL] INDEX | KEY 
                [index name] (field [(length)] [the ASC | DESC]) 
                );

 ------------------------------ - 

- creating Common index example: 

    the CREATE TABLE emp1 ( 
        ID the INT, 
        name VARCHAR ( 30 ), 
        Resume VARCHAR ( 50 ), 
        the iNDEX index_emp_name (name)
     - KEY index_dept_name (dept_name,) 
        );



 -Create a unique index Example: 

    the CREATE TABLE EMP2 ( 
        the above mentioned id INT, 
        name VARCHAR ( 30 ), 
        bank_num CHAR ( 18 ) UNIQUE, 
        Resume VARCHAR ( 50 ), 
        UNIQUE INDEX index_emp_name (name) 
        );

 - Create a full-text index Example: 

    the CREATE TABLE EMP3 ( 
        the INT ID, 
        name VARCHAR ( 30 ), 
        Resume VARCHAR ( 50 ), 
        FULLTEXT the iNDEX index_resume (Resume) 
        );

 - creating a multi-column index exemplary: 

    the CREATE TABLE emp4 ( 
        ID the INT, 
        name VARCHAR ( 30), 
        Resume VARCHAR ( 50 ), 
        the INDEX index_name_resume (name, Resume) 
        );



 --------------------------------- 

--- add an index

     --- the cREATE to create an index on a table that already exists 
      the cREATE [UNIQUE | FULLTEXT | the SPATIAL] iNDEX index name 
              oN table name (field name [(length)] [ASC | DESC]);
    
     --- the ALTER tABLE create an index on an existing table 
    
      ALTER tABLE table name the ADD [UNIQUE | FULLTEXT | the SPATIAL] iNDEX 
                    index name (field name [(length)] [ASC | DESC]); 
    
    
    
     the cREATE iNDEX index_emp_name oN emp1 (name); 
     ALTER tABLE emp2 ADD UNIQUE INDEX index_bank_num (band_num) ;


- remove the index 
    
    Syntax: DROP INDEX index name on table 
    
    DROP INDEX index_emp_name on emp1; 
    DROP INDEX bank_num on EMP2;

 

Third, the index test

- Create Table 
Create Table Indexdb.t1 (int ID, name VARCHAR ( 20 is ));


 - a storage procedure 

DELIMITER $$ 
Create Procedure The autoinsert () 
the BEGIN 
DECLARE default I int . 1 ;
 the while (I <500000 ) do 
INSERT INTO INDEXDB. values T1 (I, ' Yuan ' ); 
SET I = I +. 1 ; 
End the while ; 
the END $$ 

DELIMITER;

 - call the function 
call The autoinsert ();

 - it takes time comparison:
 - before creating the index 
   SELECT * from INDEXDB. t1 where id = 300000; -0.32s
 - add an index 
   the Create index index_id ON Indexdb.t1 (the above mentioned id);
 - index after creating 
   the SELECT * from Indexdb.t1 the WHERE the above mentioned id = 300000; - 0.00s

 

Guess you like

Origin www.cnblogs.com/hackerer/p/11596574.html
Recommended