Home Database Mysql Tutorial 所用MySQL索引类型的应用与创建

所用MySQL索引类型的应用与创建

Jun 07, 2016 pm 04:09 PM
mysql main create application article type index

以下的文章主要描述的是MySQL索引类型,MySQL索引一共可以分为5中不同类型,以下的文章就是对正5种索引的具体应用以及实际创建方式的描述,希望会给你带来一些帮助在MySQL索引类型方面。 (1)MySQL索引类型:普通索引 这是最基本的索引,它没有任何限制。它有

以下的文章主要描述的是MySQL索引类型,MySQL索引一共可以分为5中不同类型,以下的文章就是对正5种索引的具体应用以及实际创建方式的描述,希望会给你带来一些帮助在MySQL索引类型方面。
 

(1)MySQL索引类型:普通索引

这是最基本的索引,它没有任何限制。它有以下几种创建方式:

创建索引

<ol class="dp-xml"><li class="alt"><span><span>CREATE INDEX indexName ON mytable(username(length)); </span></span></li></ol>
Copy after login

如果是CHAR,VARCHAR类型,length可以小于字段实际长度;如果是BLOB和TEXT类型,必须指定 length,下同。

修改表结构

<ol class="dp-xml"><li class="alt"><span><span>ALTER mytable ADD INDEX [indexName] ON (username(length)) </span></span></li></ol>
Copy after login

创建表的时候直接指定

<ol class="dp-xml"><li class="alt"><span><span>CREATE TABLE mytable( ID INT NOT NULL, username <br>VARCHAR(16) NOT NULL, INDEX [indexName] (username(length)) ); </span></span></li></ol>
Copy after login

删除索引的语法:

<ol class="dp-xml"><li class="alt"><span><span>DROP INDEX [indexName] ON mytable; </span></span></li></ol>
Copy after login

(2)MySQL索引类型:唯一索引

它与前面的普通索引类似,不同的就是:索引列的值必须唯一,但允许有空值。如果是组合索引,则列值的组合必须唯一。它有以下几种创建方式:

创建索引

<ol class="dp-xml"><li class="alt"><span><span>CREATE UNIQUE INDEX indexName ON mytable(username(length)) </span></span></li></ol>
Copy after login

修改表结构

<ol class="dp-xml"><li class="alt"><span><span>ALTER mytable ADD UNIQUE [indexName] ON (username(length)) </span></span></li></ol>
Copy after login

创建表的时候直接指定

<ol class="dp-xml"><li class="alt"><span><span>CREATE TABLE mytable( ID INT NOT NULL, <br>username VARCHAR(16) NOT NULL, UNIQUE [indexName] (username(length)) ); </span></span></li></ol>
Copy after login

(3)MySQL索引类型:主键索引

它是一种特殊的唯一索引,不允许有空值。一般是在建表的时候同时创建主键索引:

<ol class="dp-xml"><li class="alt"><span><span>CREATE TABLE mytable( ID INT NOT NULL, username VARCHAR(16) NOT NULL, PRIMARY KEY(ID) ); </span></span></li></ol>
Copy after login

当然也可以用 ALTER 命令。记住:一个表只能有一个主键。

(4)MySQL索引类型:组合索引

为了形象地对比单列索引和组合索引,为表添加多个字段:

<ol class="dp-xml"><li class="alt"><span><span>CREATE TABLE mytable( ID INT NOT NULL, username VARCHAR(16) NOT NULL, <br>city VARCHAR(50) NOT NULL, age INT NOT NULL ); </span></span></li></ol>
Copy after login

为了进一步榨取MySQL的效率,就要考虑建立组合索引。就是将 name, city, age建到一个索引里:

<ol class="dp-xml"><li class="alt"><span><span>ALTER TABLE mytable ADD INDEX name_city_age (name(10),city,age); </span></span></li></ol>
Copy after login

建表时,usernname长度为 16,这里用 10。这是因为一般情况下名字的长度不会超过10,这样会加速索引查询速度,还会减少索引文件的大小,提高INSERT的更新速度。

如果分别在 usernname,city,age上建立单列索引,让该表有3个单列索引,查询时和上述的组合索引效率也会大不一样,远远低于我们的组合索引。虽然此时有了三个索引,但MySQL只能用到其中的那个它认为似乎是最有效率的单列索引。

建立这样的组合索引,其实是相当于分别建立了下面三组组合索引:

<ol class="dp-xml"><li class="alt"><span><span>usernname,city,age usernname,city usernname </span></span></li></ol>
Copy after login

为什么没有 city,age这样的组合索引呢?

这是因为MySQL组合索引“最左前缀”的结果。简单的理解就是只从最左面的开始组合。并不是只要包含这三列的查询都会用到该组合索引,下面的几个SQL就会用到这个组合索引:

<ol class="dp-xml"><li class="alt"><span><span>SELECT * FROM mytable WHREE </span><span class="attribute">username</span><span>=</span><span class="attribute-value">"admin"</span><span> AND </span><span class="attribute">city</span><span>=</span><span class="attribute-value">"郑州"</span><span> <br>SELECT * FROM mytable WHREE </span><span class="attribute">username</span><span>=</span><span class="attribute-value">"admin"</span><span> </span></span></li></ol>
Copy after login

而下面几个则不会用到:

<ol class="dp-xml"><li class="alt"><span><span>SELECT * FROM mytable WHREE </span><span class="attribute">age</span><span>=</span><span class="attribute-value">20</span><span> AND </span><span class="attribute">city</span><span>=</span><span class="attribute-value">"郑州"</span><span> <br>SELECT * FROM mytable WHREE </span><span class="attribute">city</span><span>=</span><span class="attribute-value">"郑州"</span><span> </span></span></li></ol>
Copy after login

(5)MySQL索引类型:建立索引的时机

到这里我们已经学会了建立索引,那么我们需要在什么情况下建立索引呢?一般来说,在WHERE和JOIN中出现的列需要建立索引,但也不完全如此,因为MySQL只对,>=,BETWEEN,IN,以及某些时候的LIKE才会使用索引。例如:

<ol class="dp-xml"><li class="alt"><span><span>SELECT t.Name FROM mytable t LEFT JOIN mytable m ON </span><span class="attribute">t.<br>Name</span><span>=m.username WHERE </span><span class="attribute">m.age</span><span>=</span><span class="attribute-value">20</span><span> AND </span><span class="attribute">m.city</span><span>=</span><span class="attribute-value">'郑州'</span><span> </span></span></li></ol>
Copy after login

此时就需要对city和age建立索引,由于mytable表的userame也出现在了JOIN子句中,也有对它建立索引的必要。

刚才提到只有某些时候的LIKE才需建立索引。因为在以通配符%和_开头作查询时,MySQL不会使用索引。例如下句会使用索引:

<ol class="dp-xml"><li class="alt"><span><span>SELECT * FROM mytable WHERE username like'admin%' </span></span></li></ol>
Copy after login

而下句就不会使用:

<ol class="dp-xml"><li class="alt"><span><span>SELECT * FROM mytable WHEREt Name like'%admin' </span></span></li></ol>
Copy after login

以上的相关内容就是对MySQL索引类型的介绍,望你能有所收获。
 


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

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.

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

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 a MySQL table using PHP? How to insert data into a MySQL table using PHP? Jun 02, 2024 pm 02:26 PM

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.

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

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

How to use MySQL stored procedures in PHP? How to use MySQL stored procedures in PHP? Jun 02, 2024 pm 02:13 PM

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.

How to create a MySQL table using PHP? How to create a MySQL table using PHP? Jun 04, 2024 pm 01:57 PM

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.

The difference between oracle database and mysql The difference between oracle database and mysql May 10, 2024 am 01:54 AM

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.

How to delete data from MySQL table using PHP? How to delete data from MySQL table using PHP? Jun 05, 2024 pm 12:40 PM

PHP provides the following methods to delete data in MySQL tables: DELETE statement: used to delete rows matching conditions from the table. TRUNCATETABLE statement: used to clear all data in the table, including auto-incremented IDs. Practical case: You can delete users from the database using HTML forms and PHP code. The form submits the user ID, and the PHP code uses the DELETE statement to delete the record matching the ID from the users table.

See all articles