---
title: "数据库中使用索引"
author: "piperzero"
date: "2012-12-19T13:33:00Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2013-01-07T09:48:18Z"
id: 1764178
url: "https://piperzero.iteye.com/blog/1764178"
---

数据库中使用索引，可以减少遍历次数，极大提高效率。

1、创建索引：命令：【create index 索引名 on 表名（列名);】，如：create index idx\_mid on customer (mid);

2、查询显示表中的索引信息：命令：【show index from 表名;】,如：show index from custom \\G （\\G代替分号;结束 ，查询显示效果更明了）

3、创建多列构成的复合索引：命令：【create index 索引名 on 表名 (列名1，列名2....);】，如：create index idx\_mid on customer (mid,nam,sex);

4、删除索引：命令：【drop index 索引名 on 表名;】，如 drop index idx\_mid on customer;

5、创建唯一索引：命令：【create unique index 索引名 on 表名（列名）;】,如：create unique index idx\_nam on customer(mid,nam); 注：可为某一列创建唯一性索引，如名字，但现实中存在很多相同的名字，如果单一为nam创建唯一性索引，则输入相同名字时会报错，所以可创建多列构成的复合唯一性索引，输入的两列数据都重复时才报错。

6、确认索引的使用情况：命令：【explain 调查对象 select语句】,如：explain select * from customer where nam='san';