---
title: "explain优化sql语句"
author: "myfreespace"
date: "2011-06-22T18:51:17Z"
tags: ["SQL"]
categories: ["数据库"]
slug: "explain-sql"
draft: false
lastmod: "2011-06-22T18:51:17Z"
id: 1101433
url: "https://myfreespace.iteye.com/blog/1101433"
---

1.explain是用来对select语句查询效率查看的语句，通过explain我们可以优化自己的sql语句。执行完explain后有几个关键的字段，

type: ref

key: y  
Extra: Using where

type:表示搜索的类型，好坏依次为system &gt; const &gt; eq\_ref &gt; ref &gt; fulltext &gt; ref\_or\_null &gt; index\_merge &gt; unique\_subquery &gt; index\_subquery &gt; range &gt; index &gt; ALL

key：为搜索用到的索引，最好在多条件的时候用到索引

extra：为排序的类型

 

2.索引的类型

普通索引：可以建多个，在一个表的多列上面添加，允许值为空和重复，

唯一索引：不允许值重复，但可以是空

主键索引：一张表中就一个主键索引，不能为空，不能重复

组合索引：多个字段一起做索引

 

对性能影响较大的排序问题，