Database views and indexes

View of the database:

View creation syntax: CREATE VIEW view_name AS SELECT column_name(s) FROM table_name WHERE condition

Views always show the latest data! Whenever a user queries a view, the database engine rebuilds the data by using the view's SQL statement.

Syntax for query view: SELECT * FROM view_name

Syntax for update view: CREATE OR REPLACE VIEW view_name AS SELECT column_name(s) FROM table_name

WHERE condition

Syntax to drop a view: DROP VIEW view_name

Index of the database:

Pros and cons of creating an index:

advantage:

① The indexed column can ensure the uniqueness of the row and generate a unique rowId

② Establishing an index can effectively shorten the retrieval time of data

③ Establishing an index can speed up the connection between tables

④ Adding indexes to the fields used for sorting or grouping can speed up the grouping and sorting order

shortcoming:

① It takes time to create and maintain indexes, and this cost increases as the amount of data increases

② Creating indexes and maintaining indexes requires space cost. Each index occupies the physical storage space of the database. The larger the amount of data, the larger the space occupied (the data table occupies the data space of the database)

③ It will reduce the efficiency of table additions, deletions and modifications, because each addition, deletion and modification of the index needs to be dynamically maintained, resulting in a longer time.

Basic syntax for creating an index: CREATE INDEX index_name ON table_name

Single-column index: CREATE INDEX index_name ON table_name (column_name)

A single-column index is an index created on only one column of a table

唯一索引:CREATE UNIQUE INDEX index_name ON table_name(column_name)

Using a unique index is not only for performance, but also for data integrity, a unique index does not allow any duplicate values ​​to be inserted into the table.

Composite index: CREATE INDEX index_name ON table_name (column1, column2)

A composite index is an index created on two or more columns of a table.

Basic syntax for dropping an index: DROP INDEX index_name







Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324871847&siteId=291194637