索引

//创建普通索引
mysql> CREATE TABLE x1
-> (
-> id INT,
-> name VARCHAR(20),
-> score FLOAT,
-> INDEX(id)//普通
-> );
Query OK, 0 rows affected

mysql> SHOW CREATE TABLE x1;
±------±--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
±------±--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| x1 | CREATE TABLE x1 (
id int(11) DEFAULT NULL,
name varchar(20) DEFAULT NULL,
score float DEFAULT NULL,
KEY id (id)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 |
±------±--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set
//创建单列索引
mysql> CREATE TABLE h
-> (
-> name VARCHAR(20) NOT NULL,
-> score FLOAT,
-> INDEX single_name(name(20))//单列
-> );
Query OK, 0 rows affected
//查看表的结构
mysql> SHOW CREATE TABLE h;
±------±----------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
±------±----------------------------------------------------------------------------------------------------------------------------------------------+
| h | CREATE TABLE h (
name varchar(20) NOT NULL,
score float DEFAULT NULL,
KEY single_name (name)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 |
±------±----------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set
//创建唯一索引
mysql> CREATE TABLE x2
-> (
-> id INT NOT NULL,
-> name VARCHAR(20) NOT NULL,
-> score FLOAT,
-> UNIQUE INDEX unique_id(id ASC) //唯一
-> );
Query OK, 0 rows affected

mysql> SHOW CREATE TABLE x2;
±------±--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
±------±--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| x2 | CREATE TABLE x2 (
id int(11) NOT NULL,
name varchar(20) NOT NULL,
score float DEFAULT NULL,
UNIQUE KEY unique_id (id)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 |
±------±--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set
//创建全文索引
mysql> CREATE TABLE x3
-> (
-> id INT NOT NULL,
-> name VARCHAR(20) NOT NULL,
-> score FLOAT,
-> FULLTEXT INDEX fulltext_name(name)
-> )ENGINE=MyISAM;
Query OK, 0 rows affected

mysql> SHOW CREATE TABLE x3;
±------±----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
±------±----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| x3 | CREATE TABLE x3 (
id int(11) NOT NULL,
name varchar(20) NOT NULL,
score float DEFAULT NULL,
FULLTEXT KEY fulltext_name (name)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 |
±------±----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set

//创建多列索引
mysql> CREATE TABLE h2
-> (
-> id INT NOT NULL,
-> name VARCHAR(20) NOT NULL,
-> score FLOAT,
-> INDEX multi(id,name(20))
-> );
Query OK, 0 rows affected

mysql> SHOW CREATE TABLE h2;
±------±----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
±------±----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| h2 | CREATE TABLE h2 (
id int(11) NOT NULL,
name varchar(20) NOT NULL,
score float DEFAULT NULL,
KEY multi (id,name)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 |
±------±----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set
//查看索引是否被使用
mysql> EXPLAIN SELECT * FROM x1 WHERE id=1;
±—±------------±------±-----±--------------±-----±--------±-----±-----±----------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
±—±------------±------±-----±--------------±-----±--------±-----±-----±----------------------------------------------------+
| 1 | SIMPLE | NULL | NULL | NULL | NULL | NULL | NULL | NULL | Impossible WHERE noticed after reading const tables |
±—±------------±------±-----±--------------±-----±--------±-----±-----±----------------------------------------------------+
1 row in set

//空间索引
mysql> CREATE TABLE h3
-> (
-> id INT,space GEOMETRY NOT NULL,
-> SPATIAL INDEX sp(space)
-> )ENGINE=MYISAM;//表的存储引擎为MYISAM
Query OK, 0 rows affected

mysql> SHOW CREATE TABLE h3;
±------±--------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
±------±--------------------------------------------------------------------------------------------------------------------------------------------+
| h3 | CREATE TABLE h3 (
id int(11) DEFAULT NULL,
space geometry NOT NULL,
SPATIAL KEY sp (space)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 |
±------±--------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值