MySQL indexes and views

First, what is the index?

     Is a special index file (index on InnoDB table is part of a table space), which contains pointers to all records of the reference data table. More popular to say, a database index is like a book in front of the catalog, you can speed up database query speed.

    Clustered index into an index and two kinds of non-clustering index, a clustered index is stored in accordance with the physical location of the data as sequential, rather than clustered index is not the same; clustered index can improve the multi-line retrieval speed, for non-clustered index quickly retrieve a single row

It should be noted that the establishment of too many updates and inserts index will affect the speed, because it requires the same update each index file. For a regular need to update and insert a table, where there is no need for the words a little-used index alone, and for a relatively small table, sort of overhead is not great, there is no need to create additional indexes.

 

   1 , the general index

General index (the index by the keyword KEY or INDEX defined) only task is to speed up access to data. Therefore, the data that most often appear in the query conditions (WHERE column = ...) or Sort (ORDER BY column) column to create the index should only. Whenever possible, you should select a data most neatly, the most compact data columns (such as an integer data type column) to create the index.

1.1 directly create an index (length representation before using the name 1ength characters) 

CREATE INDEX index_name ON table_name(column_name(length)) 

1.2 Adding configuration of a table index modifier 

ALTER TABLE table_name ADD INDEX index_name ON (column_name) 

1.3 Create a table when creating an index at the same time 

CREATE TABLE table_name ( 

id int(11) NOT NULL AUTO_INCREMENT , 

title char(255) NOT NULL , 

PRIMARY KEY (id), 

INDEX index_name (title) 

) ENGINE=InnoDB  DEFAULT CHARSET=utf8mb4; 

1.4 Delete Index 

DROP INDEX index_name ON table_name;   

1.5 build composite index. 

CREATE INDEX mytable_categoryid_userid  ON  mytable (category_id,user_id); 

 

  2 , a unique index

     Similar to the general index, it is different: the value of the index columns must be unique, but allow nulls (note different and primary key). If it is a composite index, a combination of the column value must be unique, and create a method similar to ordinary index.

     If you can determine the data column will only contain a different value each other, when creating an index of the data for this column should be defined with the keyword UNIQUE to it as a unique index. The benefit of this: First, MySQL simplifies the management of this index, the index has thus become more efficient; Second, MySQL will be the value when new records into the data table, automatically check for new records this field It has appeared in this field in a record; if it is, MySQL will refuse to insert a piece of new records. In other words, the only index to ensure the uniqueness of the data can be recorded. In fact, in many cases, people tend to create a unique index purpose is not to improve the access speed, but only in order to avoid duplication of data.

 

   2.1 create a unique index 

CREATE UNIQUE INDEX index_name ON table_name(column_name) 

2.2 modify table structure 

ALTER TABLE table_name ADD UNIQUE index_name ON (column_name) 

2.3 Create a table when specified directly 

CREATE TABLE table_name ( 

id int(11) NOT NULL AUTO_INCREMENT , 

title char(255) NOT NULL , 

PRIMARY KEY (id), 

UNIQUE index_name (title) 

); 

3 , the main index 

     Must be a primary key field to create an index, the index is called the "main index." The only difference between the main index and unique index is: the former keyword is used when defining PRIMARY instead UNIQUE. 

 

Second, what view?

     A view is a virtual table, or a plurality of tables is lead out from the database table. The definition stored in the database view only, and are not stored in the data view, the data stored in the original table. When a query using the view data corresponding to the database system data is extracted from the original table.

    1, the view of the role:

(1) simplification of the operator;

(2) increase data security;

(3) increase the independence of the logic table;

    2, create a view

The CREATE [UNDEFIEND the ALGORITHM = {| the MERGE | TempTable}]
           the VIEW view name [(attribute list)]
          the AS the SELECT statement
         [WITH [CASCADED | LOCAL] CHECK OPTION];

    

      2.1 to create a view on a single table

CREATE VIEW v1 AS SELECT * FROM t_book;

CREATE VIEW v2 AS SELECT bookName,price FROM t_book;

CREATE VIEW v3(b,p) AS SELECT bookName,price FROM t_book;

2.2 to create a view on multiple tables

CREATE VIEW v4 AS SELECT bookName,bookTypeName FROM t_book,t_booktype

WHERE t_book.bookTypeId=t_booktype.id;

CREATE VIEW v5 AS SELECT tb.bookName,tby.bookTypeName FROM t_book

tb,t_booktype tby WHERE tb.bookTypeId=tby.id;

   

  3, see the view
3.1 DESCRIBE statement to see the view basic information

            DESC v5;

3.2 SHOW TABLE STATUS statement to see the view basic information

            SHOW TABLE STATUS LIKE 'v5';

3.3 SHOW CREATE VIEW statement view View Details

            SHOW TABLE STATUS LIKE 't_book';

3.4 View View details in table views

            SHOW CREATE VIEW v5;

   4, modify the view

4.1 CREATE OR REPLACE VIEW statement modifies view

CREATE OR REPLACE [ ALGORITHM ={ UNDEFINED | MERGE | TEMPTABLE }]

           VIEW view name [(property list)]

           AS SELECT statement

           [ WITH [ CASCADED | LOCAL ] CHECK OPTION ];

           E.g:

CREATE OR REPLACE VIEW v1(bookName,price)

AS SELECT bookName,price

FROM t_book;

4.2 ALTER statement modifies view

ALTER [ ALGORITHM ={ UNDEFINED | MERGE | TEMPTABLE }]

             VIEW view name [(property list)]

             AS SELECT statement

            [ WITH [ CASCADED | LOCAL ] CHECK OPTION ];

           E.g:

ALTER VIEW v1 AS SELECT * FROM t_book;

    5, update view

Means to insert update view (the INSERT) through the view, update (the UPDATE) and delete (DELETE) data in the table. Because the view is a virtual table, where there is no data. Through the view update, the basic conversion table is updated. When updating a view, you can only update data within their competence. Out of range, it can not be updated.

5.1 insert (the INSERT)

INSERT INTO v1 VALUES(NULL,'java good',120,'feng',1);

5.2 update (UPDATE)

UPDATE v1 SET bookName='java very good',price=200 WHERE id=5;

5.3 Delete (DELETE)

DELETE FROM v1 WHERE id=5;

6, delete, view

Delete to delete the view refers to a view that already exists in the database. When you delete a view, the view can only delete the definition, does not delete data;

DROP VIEW [IF EXISTS] name in the list view [RESTRICT | CASCADE]

DROP VIEW IF EXISTS v4;

Guess you like

Origin www.cnblogs.com/lone5wolf/p/11520420.html