当前位置:网站首页>Indexes and views in MySQL
Indexes and views in MySQL
2022-04-23 17:14:00 【MiMenge】
Indexes
What is index
There are two ways to search tables in the database :
-
Full table scan :( When there is too much data in the table, there will be efficiency problems )
-
Index search :( Efficient )
principle : Narrow the scope of scanning files .
Be careful ----- Although index can improve retrieval efficiency , But you can't just add indexes , Because the index is also an object in the database , It also needs constant maintenance , With maintenance costs . Once the data is changed , The index may also be changed
When to add an index
- The amount of data is huge ( According to the needs of customers , According to the online environment )
- This field is rarely dml operation .( Because the field cannot be modified , Indexes also need to be maintained )
- This field often appears in where clause .( Often query according to a certain field )
Try to retrieve according to the primary key
Create an index
create index < The index name > on < Table name >([ Field name ]);
mysql> create index yin on pay(id);
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> explain select stu_class,stu_name from t_student where stu_class = 2;
+----+-------------+-----------+------------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-----------+------------+------+---------------+------+---------+------+------+----------+-------------+
| 1 | SIMPLE | t_student | NULL | ALL | NULL | NULL | NULL | NULL | 8 | 12.50 | Using where |
+----+-------------+-----------+------------+------+---------------+------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)
Delete index
drop index < Index name > on < Table name >;
see sql Statement execution plan
explain < sentence >;
mysql> explain select * from pay;
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
| 1 | SIMPLE | pay | NULL | ALL | NULL | NULL | NULL | NULL | 4 | 100.00 | NULL |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
1 row in set, 1 warning (0.01 sec)
Classification of indexes
-
Single index : Add an index to a single field
-
Composite index : Add a union index to multiple fields
-
primary key : An index is automatically added to the primary key
-
unique index : Yes unique Indexes are automatically added to the constrained fields
Invalidation of index
Fuzzy queries may invalidate index queries --------- The first wildcard uses %
View (views)
What is the view
Look at the data from different perspectives .( Data from the same table , Look at... From different angles )
Be careful : Adding, deleting, modifying and querying through the view will affect the original table data
Create view
create view < View name > as select Query statement ;
View operation
mysql> create view v_student as select stu_id,stu_name,stu_age from t_student;
Query OK, 0 rows affected (0.01 sec)
mysql> select * from v_student;
+--------+----------+---------+
| stu_id | stu_name | stu_age |
+--------+----------+---------+
| 1 | tom | 18 |
| 2 | andy | 17 |
| 3 | zhansan | 16 |
| 4 | jery | 20 |
| 5 | ldy | 19 |
| 6 | lis | NULL |
| 7 | lby | NULL |
| 14 | mah | 19 |
+--------+----------+---------+
8 rows in set (0.00 sec)
The function of view
The view can hide the implementation details of the table . A system with a high level of confidentiality , The database only provides related views , We can only operate on the view
版权声明
本文为[MiMenge]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230553027628.html
边栏推荐
- Shortcut keys (multiline)
- 1-3 components and modules
- Detailed explanation of C webpai route
- Get the column name list of the table quickly in Oracle
- C# Task. Delay and thread The difference between sleep
- Production environment——
- Milvus 2.0 détails du système d'assurance de la qualité
- Paging the list collection
- Solution of Navicat connecting Oracle library is not loaded
- Understanding and small examples of unity3d object pool
猜你喜欢
![[WPF binding 3] listview basic binding and data template binding](/img/2e/fbdb4175297bb4964a8ccfd0b909ae.png)
[WPF binding 3] listview basic binding and data template binding
![Using quartz under. Net core -- operation transfer parameters of [3] operation and trigger](/img/4e/2161fc448f4af71d9b73b7de64a17f.png)
Using quartz under. Net core -- operation transfer parameters of [3] operation and trigger

Change the password after installing MySQL in Linux

Milvus 2.0 détails du système d'assurance de la qualité

. net cross platform principle (Part I)
![Customize my_ Strcpy and library strcpy [analog implementation of string related functions]](/img/71/a6a0c7b0e652d2b807f259f7cbf139.png)
Customize my_ Strcpy and library strcpy [analog implementation of string related functions]

C# Task. Delay and thread The difference between sleep
![Using quartz under. Net core -- job attributes and exceptions of [4] jobs and triggers](/img/ec/43dddd18f0ce215f0f1a781e31f6a8.png)
Using quartz under. Net core -- job attributes and exceptions of [4] jobs and triggers

Lock lock

groutine
随机推荐
RPC核心概念理解
ASP. Net core reads the configuration file in the class library project
Document operation II (5000 word summary)
ClickHouse-数据类型
Deep understanding of control inversion and dependency injection
freeCodeCamp----shape_ Calculator exercise
[registration] tf54: engineer growth map and excellent R & D organization building
Shell脚本——Shell编程规范及变量
VsCode-Go
[related to zhengheyuan cutting tools]
【生活中的逻辑谬误】稻草人谬误和无力反驳不算证明
1-3 nodejs installation list configuration and project environment
Paging SQL
Understanding of RPC core concepts
Simulation of infrared wireless communication based on 51 single chip microcomputer
websocket
How does matlab draw the curve of known formula and how does excel draw the function curve image?
MySQL master-slave configuration under CentOS
Using quartz under. Net core -- preliminary understanding of [2] operations and triggers
Input file upload