当前位置:网站首页>Review of MySQL (IX): index

Review of MySQL (IX): index

2022-06-12 18:20:00 BKSW.

MySql Review ( Nine ): Indexes

What is index ?

Indexes are used to quickly find rows with a specific value on a column . No index ,MySQL Have to start with the first record , Then read the entire table until it finds the relevant rows . The bigger the watch , The more time it takes .

--  Although index can improve retrieval efficiency , But you can't add indexes at will , Because the index is also an object in the database , It also requires constant database maintenance . There are maintenance costs .
--  such as : The data in the table is often modified , This is not suitable for adding indexes , Because once the data is modified , The index needs to be reordered , For maintenance .

--  Adding an index is to a field , Or add an index to some fields .

select ename,sal from emp where ename = 'SMITH';
--  When ename When the field is not indexed , above sql Statement will perform a full table scan , scanning ename All values in the field .
--  When ename When adding an index to a field , above sql Statement will scan... According to the index , Rapid positioning .

Create index

If the index is not used , Inquire about Wages are greater than 1500 Will perform a full table scan

image-20220227010212546

An index is equivalent to the contents of a Book

The primary key will be automatically added to the index , Therefore, it is more efficient to query according to the primary key .

If often based on sal The query , And encountered a performance bottleneck , First, check whether the program has algorithm problems , Think again about sal Index , Index as follows :

  1. Create index object

    create unique index Index name on Table name ( Name );

create unique index u_ename on emp(ename);
  1. Modify index name

    alter table Table name add unique index Index name ( Name );

Look at the index

show index from emp;

image-20220227010640793

Use index

Be careful : It must not be used select * … You can see type!=all 了 , Indicates that the index is used

explain select sal from emp where sal > 1500;

image-20220227010745626

Delete index

DROP INDEX index_name ON talbe_name
ALTER TABLE table_name DROP INDEX index_name
ALTER TABLE table_name DROP PRIMARY KEY
--  among , The first two statements are equivalent , Delete the table_name Index in index_name.
--  The first 3 Only delete PRIMARY KEY Use... When indexing , Because a watch can only have one PRIMARY KEY Indexes , 
mysql> ALTER TABLE EMP DROP INDEX test_index;

--  The index is no longer used after deletion , The query will perform a full table scan .

image-20220227010935338

Other knowledge of the index

  • The underlying data structure of the index is :B + Tree

  • The realization principle of index ?
    adopt B Tree Narrow the scanning range , The underlying index is sorted , Partition , The index will carry the data in the table " Physical address ", After the data is finally retrieved through the index , Get the associated physical address ,
    After the data is retrieved through the physical index , Get the associated physical address , Locate the data in the table by physical address , Efficiency is the highest .
    select ename from emp where ename = ‘SMITH’;
    Convert to by index :
    select ename from emp where Physical address = 0x123;

  • Classification of indexes ?
    Single index : Add an index to a single field
    Composite index : Add an index to the union of multiple fields
    primary key : An index is automatically added to the primary key
    unique index : Yes unique The constrained fields are automatically indexed

  • When does the index expire ?
    select ename from emp where ename like ’ %A% ';
    Suo Yin

  • When does the index expire ?
    select ename from emp where ename like ’ %A% ';
    When fuzzy query , The first wildcard uses %, At this time, the index is invalid .

原网站

版权声明
本文为[BKSW.]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202281622108593.html