(1)准备环境

mysql> create table t1(id int,name varchar(50));
mysql> \d $$
mysql> create procedure autoinsert_t1()
begin
declare i int default 1;
while(i<200000)do
insert into school.t1 values(i,'ccc');
set i=i+1
;
end while;
END $$
mysql> \d ;
mysql> show create procedure autoinsert_t1\G
mysql> call autoinsert_t1();

(2)创建索引

1)创建表时创建索引

创建普通索引:mysql> create table departname(id int,name varchar(50),comment varchar(50),index(name));

创建唯一索引:mysql> create table department2(id int,name varchar(50),comment varchar(50),unique index(name));

创建全文索引:mysql> create table department3(id int,name varchar(50),log text,fulltext index(log)); \针对文章给文章内容起索引

创建多列索引:mysql> create table department5(id int,name varchar(50),comment varchar(50),index index_name_comment(name,comment));

\创建多列索引建议给索引起个名字

2)在已存在表上创建索引:create

语法:create [unique|fulltext|spatial] index 索引名 on 表名(字段[(长度)] [ASC|DESC])

创建普通索引:mysql> create index index_name on department(name); \索引关键字MUL

创建唯一索引:mysql> create unique index index_id on department(id);

创建全文索引:mysql> create fulltext index index_name on department(name);

创建多列索引:mysql> create index index_name_comment on department(name,comment);

3)在已存在表上创建索引:alter table

语法:alter table 表名 add [unique|fulltext|spatial] index 索引名(字段[(长度)] [ASC|DESC])

创建普通索引:alter table department add index index_name(name);

创建唯一索引:alter table department add unique index index_name(name);

创建全文索引:alter table department add fulltext index index_name(name);

创建多列索引:alter table department add index index_name_comment(name,comment);

(3)管理索引

1)查看索引

语法:show create table 表名\G

2)测试索引

语法:explain select * from 表名 where 条件

3)删除索引

语法:drop index 索引名 on 表名;

mysql> drop index index_name on department6;

注意:先使用show create table 表名\G 查看索引名字,例如 KEY index_name (name) index_name 就是索引名字

(4)测试索引:

未使用索引之前:注意key和rows

mysql> explain  select * from t1 where id=190000;
+----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+-------------+
| 1 | SIMPLE | t1 | NULL | ALL | NULL | NULL | NULL | NULL | 200186 | 10.00 | Using where |
+----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+-------------+

创建索引之后:

mysql> explain  select * from t1 where id=190000;
+----+-------------+-------+------------+------+---------------+----------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+----------+---------+-------+------+----------+-------+
| 1 | SIMPLE | t1 | NULL | ref | index_id | index_id | 5 | const | 1 | 100.00 | NULL |
+----+-------------+-------+------------+------+---------------+----------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)

使用索引:查询语句使用where 条件,条件使用的列必须是索引

(八)MySQL索引操作的更多相关文章

  1. MySQL索引操作

    创建普通索引 CREATE INDEX index_name ON table_name(column1,column2); 另一种建立方式: ALTER TABLE table_name ADD I ...

  2. mysql 索引及其原理

    mysql 索引 KEY与INDEX的区别: KEY is something on the logical level, describes your table and database desi ...

  3. MySQL索引的缺点以及MySQL索引在实际操作中有哪些事项

    以下的文章主要介绍的是MySQL索引的缺点以及MySQL索引在实际操作中有哪些事项是值得我们大家注意的,我们大家可能不知道过多的对索引进行使用将会造成滥用.因此MySQL索引也会有它的缺点: 虽然索引 ...

  4. Mysql之表的操作与索引操作

    表的操作: 1.表的创建: create table if not exists table_name(字段定义); 例子: create table if not exists user(id in ...

  5. 着重基础之—MySql 不能遗忘的索引操作

    着重基础之—MySql 不能遗忘的索引操作 关于MySql索引的基础知识我就不在这里写了,我不太想当信息的搬运工. 技巧分享:Workbench 作为一款专为MySQL设计的ER/数据库建模工具.除了 ...

  6. Mysql高级操作学习笔记:索引结构、树的区别、索引优缺点、创建索引原则(我们对哪种数据创建索引)、索引分类、Sql性能分析、索引使用、索引失效、索引设计原则

    Mysql高级操作 索引概述: 索引是高效获取数据的数据结构 索引结构: B+Tree() Hash(不支持范围查询,精准匹配效率极高) 树的区别: 二叉树:可能产生不平衡,顺序数据可能会出现链表结构 ...

  7. 第二百八十六节,MySQL数据库-MySQL事务操作(回滚)

    MySQL数据库-MySQL事务操作(回滚) 事务用于将某些操作的多个SQL作为原子性操作,一旦有某一个出现错误,即可回滚到原来的状态,从而保证数据库数据完整性. 举例:有这样一张表 从表里可以看出张 ...

  8. [MySQL]索引类型总结和使用技巧以及注意事项

    一.普通索引 这是最基本的索引,它没有任何限制.它有以下几种创建方式: 1.创建索引 CREATE INDEX [indexName] ON [mytable] ([column][(length)] ...

  9. 深入浅出分析MySQL索引设计背后的数据结构

    在我们公司的DB规范中,明确规定: 1.建表语句必须明确指定主键 2.无特殊情况,主键必须单调递增 对于这项规定,很多研发小伙伴不理解.本文就来深入简出地分析MySQL索引设计背后的数据结构和算法,从 ...

随机推荐

  1. 【转】Java线程系列:Callable和Future

    一.前言 在研究JDK1.8的CompletableFuture时,顺道将Futrue一起扫了盲~这篇博文纯转载 二.正文 本篇说明的是Callable和Future,它俩很有意思的,一个产生结果,一 ...

  2. 微服务日志监控与查询logstash + kafka + elasticsearch

    使用 logstash + kafka + elasticsearch 实现日志监控 https://blog.csdn.net/github_39939645/article/details/788 ...

  3. POJ2774 Long Long Message 【后缀数组lcp】

    长长的消息 时间限制: 4000MS   内存限制: 131072K 提交总数: 32393   接受: 13079 案件时间限制: 1000MS 描述 小猫在拜特兰的首府物理专业.最近有一个不幸的消 ...

  4. Super Moban

    HAO BAN ZI 包括求解,判断无解,求自由变元个数以及标记不确定的变元.来源:http://blog.csdn.net/keshuqi/article/details/51921615 #inc ...

  5. 用实例工厂的方法实例化bean

    在实例化bean时,除了setter,constructor方法外,还有实例工厂方法,和静态工厂方法. 看代码: People类的代码如下: package com.timo.domain; publ ...

  6. 对Office文档进行授权

    Microsoft.Office.Interop.Word.ApplicationClass app = new Microsoft.Office.Interop.Word.ApplicationCl ...

  7. ext4文件系统由文件的inode号定位其inode Table

    在ubuntu中(以16.06为例),stat filename 可以查看文件的inode数值,但是如何确定该inode项具体在哪个块组下的inode Table中不是那么容易,接下来通过一步步计算来 ...

  8. react 记录:React Warning: Hash history cannot PUSH the same path; a new entry will not be added to the history stack

    前言: react-router-dom 4.4.2 在页面中直接使用 import { Link } from 'react-router-dom' //使用 <Link to={{ path ...

  9. 【Foreign】红与蓝 [暴力]

    红与蓝 Time Limit: 10 Sec  Memory Limit: 256 MB Description Input Output Sample Input 2 2 0 1 -1 -1 2 0 ...

  10. 【洛谷 P1667】 数列 (贪心)

    题目链接 对于一个区间\([x,y]\),设这个区间的总和为\(S\) 那么我们在前缀和(设为\(sum[i]\))的意义上考虑到原操作其实就是\(sum[x−1]+=S\) , \(sum[x]+S ...