index index
In order to improve query efficiency,
the index is based on the field and is added
type of index
-
Unique index: primary key constraints, unique constraints are unique indexes
-
Ordinary index: can be added to any required field
-
Composite index: combine multiple fields to build an index
Will SQL use a composite index, using a
最左原则
What kind of fields are suitable for adding indexes
-
- The primary key comes with a unique index
-
- Unique keys are suitable for adding indexes
-
- Foreign keys are suitable for adding indexes (adding foreign key constraints will bring their own indexes)
-
- Fields that often appear in where conditions
-
- The value repetition rate in the field is not high, suitable for adding indexes
-
- Fields that are often used for grouping are suitable for adding indexes
add index
create [unique] index <indexName> on <tableName>(columnName, ...)
alter table <tableName> add index <indexName>(columnName, ...) ;
delete index
drop index <indexName> on <tableName> ;
alter table <tableName> drop index <indexName> ;
ViewView
View View is a mapping of a real table, a virtual table, and an SQL command.
The view does not store data, but stores SQL query commands.
The role of the view
- Hide the real table to ensure data security
- simplified query
create view
create view <viewName> as select ... ;
delete view
drop view <viewName> ;