CREATE TABLE 表名 ( 字段1 数据类型,字段2 数据类型[,...],INDEX 索引名 (列名));例:create table test(id int(4) not null,name varchar(10) not null,cardid varchar(18) not null,index id_index (id));show create table test;
CREATE UNIQUE INDEX 索引名 ON 表名(列名);
ALTER TABLE 表名 ADD UNIQUE 索引名 (列名);例:alter table member add unique name_index (cardid);



In-depth understanding of MySQL index principles and implementation, and rapid database retrieval
Free learning recommendation: mysql video tutorial
##1. The concept of index
1. The index is a sorted list, in which the index value and the physical address of the row containing the data containing the value are stored (similar to C language The linked list points to the memory address of the data record through a pointer). 2. After using the index, you do not need to scan the entire table to locate the data of a certain row. Instead, you first find the physical address corresponding to the row of data through the index table and then access the corresponding data, thus speeding up the query speed of the database. . 3.The index is like the table of contents of a book. You can quickly find the required content based on the page numbers in the table of contents.
4. Index is a method of sorting the values of one or several columns in a table. 5. The purpose of establishing an index is to speed up the search or sorting of records in the table.2. The role of index
1. After setting up a suitable index, the database can greatly speed up the query by using various rapid positioning technologies. This is to create an index. The main reason. 2. When the table is very large or the query involves multiple tables, using indexes can increase the query speed by thousands or tens of thousands of times.
3. It can reduce the I/O cost of the database, and the index can also reduce the sorting cost of the database.
4. By creating a unique index, the uniqueness of each row of data in the data table can be guaranteed.
5. Can speed up the connection between tables.
6. When using grouping and sorting, the time of grouping and sorting can be greatly reduced.
3. Side Effects of Index
1. Indexes require additional disk space.- For the MyISAM engine, the index file and the data file are separated, and the index file is used to save the address of the data record.
- The table data file of the InnoDB engine itself is an index file.
4. Principles of creating indexes
Indexes can increase the speed of database queries, but they are not suitable for creating indexes in all circumstances. Because the index itself consumes system resources, if there is an index, the database will first perform an index query and then locate the specific data row. If the index is used improperly, it will increase the burden on the database. 1. The primary key and foreign key of the table must have indexes. Because the primary key is unique and the foreign key is associated with the primary key of the main table, it can be quickly located during querying. 2. Tables with more than 300 rows of records should have indexes. If there is no index, each query needs to traverse the table, which will seriously affect the performance of the database.
3. Tables that are frequently connected to other tables should create indexes on the connection fields.
4. Fields with poor uniqueness are not suitable for indexing.
5. Fields that are updated too frequently are not suitable for creating indexes.
6. Fields that often appear in where clauses, especially fields in large tables, should be indexed.
7. Indexes should be built on highly selective fields.
8. Indexes should be built on small fields. Do not build indexes on large text fields or even very long fields.
5. Classification and creation of indexes
1 2 |
|
(1) Ordinary index
Ordinary index: the most basic Index type, no restrictions such as uniqueness. 1. Create an index directly1
2
3
4
CREATE INDEX 索引名 ON 表名 (列名[(length)]);
#(列名(length)):length是可选项,下同。如果忽略 length 的值,则使用整个列的值作为索引。如果指定使用列前的 length 个字符来创建索引,这样有利于减小索引文件的大小。
#索引名建议以“_index”结尾。
Copy after login
1 2 3 4 |
|
1
ALTER TABLE 表名 ADD INDEX 索引名 (列名);例:alter table member ADD INDEX phone_index (phone);select phone from member;
Copy after login
1 |
|
1
CREATE TABLE 表名 ( 字段1 数据类型,字段2 数据类型[,...],INDEX 索引名 (列名));例:create table test(id int(4) not null,name varchar(10) not null,cardid varchar(18) not null,index id_index (id));show create table test;
Copy after login
1 |
|
1
2
与普通索引类似,但区别是唯一索引列的每个值都唯一。
唯一索引允许有空值(注意和主键不同)。如果是用组合索引创建,则列值的组合必须唯一。添加唯一键将自动创建唯一索引。
Copy after login
1. Create a unique index directly1 2 |
|
1
CREATE UNIQUE INDEX 索引名 ON 表名(列名);
Copy after login
1 |
|
1
ALTER TABLE 表名 ADD UNIQUE 索引名 (列名);例:alter table member add unique name_index (cardid);
Copy after login
In order to facilitate the experiment, I first deleted the previous index1 |
|
3、创建表的时候指定
1 |
|
(三)、主键索引
1 2 |
|
ALTER TABLE 表名 ADD PRIMARY KEY (列名);
(四)、组合索引(单列索引与多列索引)
1 |
|
1 |
|
(五)、全文索引
1 2 |
|
1 |
|
- 修改表的方式创建
1 |
|
- 创建表的时候指定索引
1 |
|
#数据类型可以为 CHAR、VARCHAR 或者 TEXT
- 使用全文索引查询
1 |
|
六、查看索引
1 2 |
|
Table | 表的名称 |
---|---|
Non_unique | 如果索引不能包括重复词,则为 0;如果可以,则为 1。 |
Key_name | 索引的名称。 |
Seq_in_index | 索引中的列序号,从 1 开始。 |
Column_name | 列名称。 |
Collation | 列以什么方式存储在索引中。在 MySQL 中,有值‘A’(升序)或 NULL(无分类) |
Cardinality | 索引中唯一值数目的估计值。 |
Sub_part | 如果列只是被部分地编入索引,则为被编入索引的字符的数目。如果整列被编入索引,则为 NULL。 |
Packed | 指示关键字如何被压缩。如果没有被压缩,则为 NULL |
Null | 如果列含有 NULL,则含有 YES。如果没有,则该列含有 NO |
Index_type | 用过的索引方法(BTREE, FULLTEXT, HASH, RTREE) |
Comment | 备注 |
七、删除索引
1 2 3 4 |
|
八、实例
1 2 3 4 5 6 7 |
|
更多相关免费学习推荐:mysql教程(视频)
The above is the detailed content of In-depth understanding of MySQL index principles and implementation, and rapid database retrieval. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Big data structure processing skills: Chunking: Break down the data set and process it in chunks to reduce memory consumption. Generator: Generate data items one by one without loading the entire data set, suitable for unlimited data sets. Streaming: Read files or query results line by line, suitable for large files or remote data. External storage: For very large data sets, store the data in a database or NoSQL.

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

To use MySQL stored procedures in PHP: Use PDO or the MySQLi extension to connect to a MySQL database. Prepare the statement to call the stored procedure. Execute the stored procedure. Process the result set (if the stored procedure returns results). Close the database connection.

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

Oracle database and MySQL are both databases based on the relational model, but Oracle is superior in terms of compatibility, scalability, data types and security; while MySQL focuses on speed and flexibility and is more suitable for small to medium-sized data sets. . ① Oracle provides a wide range of data types, ② provides advanced security features, ③ is suitable for enterprise-level applications; ① MySQL supports NoSQL data types, ② has fewer security measures, and ③ is suitable for small to medium-sized applications.
