search
HomeDatabaseMysql Tutorial辛星让mysql跑得更快第二节之索引上部分_MySQL

如果把我们的数据库信息当做一本书或者一个字典,那么索引可以理解为它的目录,如果我们创建一个优秀的目录,那么我们检索信息就会快得多,如果我们创建一个渣渣索引,也有可能拖垮整个系统。

索引我们分为四类,通常分为四大类型,即主键索引、全文索引、唯一索引、普通索引,这是按照索引的类型来分的。所谓主键索引,那就是当我们创建一张表的时候,如果我们指定了一个主键,那么它就自动成为主键索引,比如SQL语句如下(这里的id就自动成为了主键索引):

create table xin(id int unsigned primary key auto_increment,
		name   varchar(32) not null default '');

一般来说,对于普通索引,我们可以在创建表的时候指定索引,也可以在创建表的时候指定索引,下面我们以县创建表然后再添加索引为例,看代码:

#创建一个数据表
create table xin(id int unsigned,
	name   varchar(32) not null default '');

#在该表上创建一个索引
create index xiaohei  on xin(id);
这里说一下把,这里的添加索引的格式是:create index 索引名 on 表名(列名);

然后说一下全文索引,所谓全文索引,主要是从数据库中搜索字符串信息的,比如我们逛很多论坛,它的搜索功能就特别需要全文索引了,我们的全文索引主要针对文件、文本的索引,而且目前来说,全文索引依然只对引擎MyIASM有效,咱们指定一下表的引擎就可以了,看下面代码:

#创建一个文章表,并且设置索引
create table article(id int primary key,
	title varchar(200) not null ,
	body  text,
	fulltext(title,body)
) engine = myisam ;
那么我们使用该全文索引的时候应该使用使用match和against,看下面操作:
#按照这种方式我们可以快速的使用全文索引来查找内容
select * from article where match(title,body)  against ('xin');
但是mysql自带的这个全文索引并不支持中文,我们可以考虑使用sphinx来支持中文,这里先不介绍。

对于unique索引,也就是唯一索引,我们直接在在列级完整性约束后面加一个unique即可,这里需要注意的是被unique修饰的字段是可以为空的,而且是可以有多个为空的,当然也可以像创建一个普通index那样去创建,但是此时的index必须在前面加上一个unique修饰符。

对于索引的删除,可以使用【alter table 表名 drop index 索引名】,但是我感觉我们使用【drop index 索引名 on 表名】更好一些。

那么我们怎么看一个表的索引呢,我们可以用【desc 表名】的方式来查看索引,我们还可以使用【show index from 表名】或者【show index from 表名】来查看一个表的索引。

Statement
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
How does MySQL handle data replication?How does MySQL handle data replication?Apr 28, 2025 am 12:25 AM

MySQL processes data replication through three modes: asynchronous, semi-synchronous and group replication. 1) Asynchronous replication performance is high but data may be lost. 2) Semi-synchronous replication improves data security but increases latency. 3) Group replication supports multi-master replication and failover, suitable for high availability requirements.

How can you use the EXPLAIN statement to analyze query performance?How can you use the EXPLAIN statement to analyze query performance?Apr 28, 2025 am 12:24 AM

The EXPLAIN statement can be used to analyze and improve SQL query performance. 1. Execute the EXPLAIN statement to view the query plan. 2. Analyze the output results, pay attention to access type, index usage and JOIN order. 3. Create or adjust indexes based on the analysis results, optimize JOIN operations, and avoid full table scanning to improve query efficiency.

How do you back up and restore a MySQL database?How do you back up and restore a MySQL database?Apr 28, 2025 am 12:23 AM

Using mysqldump for logical backup and MySQLEnterpriseBackup for hot backup are effective ways to back up MySQL databases. 1. Use mysqldump to back up the database: mysqldump-uroot-pmydatabase>mydatabase_backup.sql. 2. Use MySQLEnterpriseBackup for hot backup: mysqlbackup--user=root-password=password--backup-dir=/path/to/backupbackup. When recovering, use the corresponding life

What are some common causes of slow queries in MySQL?What are some common causes of slow queries in MySQL?Apr 28, 2025 am 12:18 AM

The main reasons for slow MySQL query include missing or improper use of indexes, query complexity, excessive data volume and insufficient hardware resources. Optimization suggestions include: 1. Create appropriate indexes; 2. Optimize query statements; 3. Use table partitioning technology; 4. Appropriately upgrade hardware.

What are views in MySQL?What are views in MySQL?Apr 28, 2025 am 12:04 AM

MySQL view is a virtual table based on SQL query results and does not store data. 1) Views simplify complex queries, 2) Enhance data security, and 3) Maintain data consistency. Views are stored queries in databases that can be used like tables, but data is generated dynamically.

What are the differences in syntax between MySQL and other SQL dialects?What are the differences in syntax between MySQL and other SQL dialects?Apr 27, 2025 am 12:26 AM

MySQLdiffersfromotherSQLdialectsinsyntaxforLIMIT,auto-increment,stringcomparison,subqueries,andperformanceanalysis.1)MySQLusesLIMIT,whileSQLServerusesTOPandOracleusesROWNUM.2)MySQL'sAUTO_INCREMENTcontrastswithPostgreSQL'sSERIALandOracle'ssequenceandt

What is MySQL partitioning?What is MySQL partitioning?Apr 27, 2025 am 12:23 AM

MySQL partitioning improves performance and simplifies maintenance. 1) Divide large tables into small pieces by specific criteria (such as date ranges), 2) physically divide data into independent files, 3) MySQL can focus on related partitions when querying, 4) Query optimizer can skip unrelated partitions, 5) Choosing the right partition strategy and maintaining it regularly is key.

How do you grant and revoke privileges in MySQL?How do you grant and revoke privileges in MySQL?Apr 27, 2025 am 12:21 AM

How to grant and revoke permissions in MySQL? 1. Use the GRANT statement to grant permissions, such as GRANTALLPRIVILEGESONdatabase_name.TO'username'@'host'; 2. Use the REVOKE statement to revoke permissions, such as REVOKEALLPRIVILEGESONdatabase_name.FROM'username'@'host' to ensure timely communication of permission changes.

See all articles

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor