MYSQL索引

小白作者,只会基础,如果有大佬,可以在评论区讨论

创建索引

1,建表时创建索引

create table index_demo(
id BIGINT(20) UNSIGNED not NULL auto_increment ,
name varchar(10) DEFAULT "",
phone BIGINT(11) DEFAULT 0,
age TINYINT(4) DEFAULT 0,
create_time datetime DEFAULT CURRENT_TIMESTAMP,
update_time TIMESTAMP null DEFAULT CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
primary key (id)
index id_name(name);		-- 创建索引
)ENGINE=INNODB default CHARSET=utf8;

2,建表后使用alter table创建索引

create table index_demo(
id BIGINT(20) UNSIGNED not NULL auto_increment ,
name varchar(10) DEFAULT "",
phone BIGINT(11) DEFAULT 0,
age TINYINT(4) DEFAULT 0,
create_time datetime DEFAULT CURRENT_TIMESTAMP,
update_time TIMESTAMP null DEFAULT CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
primary key (id)
)ENGINE=INNODB default CHARSET=utf8;

alter table index_demo add index id_index_demo_name(name);

3,建表后使用create index创建索引

create table index_demo(
id BIGINT(20) UNSIGNED not NULL auto_increment ,
name varchar(10) DEFAULT "",
phone BIGINT(11) DEFAULT 0,
age TINYINT(4) DEFAULT 0,
create_time datetime DEFAULT CURRENT_TIMESTAMP,
update_time TIMESTAMP null DEFAULT CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
primary key (id)
)ENGINE=INNODB default CHARSET=utf8;

create index id_index_demo_name on index_demo(name);

四种索引类型

fulltext:以词为基础的,MySQL默认的分词是所有非字母和数字的特殊符号都是分词符

create fulltext index id_index_demo_name on index_demo(name);

index:最基本的索引,它没有任何限制

create index id_index_demo_name on index_demo(name);

 unique index:索引列的值必须唯一,但允许有空值。如果是组合索引,则列值的组合必须唯一

create unique index id_index_demo_name on index_demo(name);

 index index_name on table_name(column,column..):多列索引

create index id_index_demo_name on index_name(name,phone);

 使用以下命令可以查看表的索引

show index from table_name;    -- table_name改成你的表名

使用以下命令可以删除索引

drop index index_name on table_name   

alter table table_name drop index index_name

#index_name替换成索引的名字,table_name替换成表名

注:id_index_demo_name是索引名,前面的id表示索引,index_demo是表名,name是列名

使用函数插入数据

创建函数

DELIMITER $$		-- 创建函数
create function mock_data()		-- 函数名
RETURNS INT		--返回数字
BEGIN		-- 函数开始
	DECLARE num INT DEFAULT 1000000;
	DECLARE i INT DEFAULT 0;
	WHILE i<num Do
		INSERT INTO index_demo(name,phone,age)VALUES(concat("user",i),concat('1',floor(rand()*((9999999999-1000000000)+1000000000))),floor(rand()*100));
		SET i = i+1;
	End	WHILE;
	RETURN i;
END;

# 如果报错:This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creators variable) 就先运行下面的这条语句

set GLOBAL	log_bin_trust_function_creators=1;	

# set GLOBAL	log_bin_trust_function_creators=0;	-- 使用完成后关闭

运行函数

select mock_data();

查看当前已有函数

show function status

删除函数

drop function mock_data;        -- 注意后面没有括号

删除表操作

truncate table table_name    -- 清空表中所有数据
delete from table_name where      -- 删除符合where条件的数据,可回滚,不删除表的结构
drop table table_name        -- 删除表

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MySQL索引是一种数据结构,可以帮助MySQL快速定位和访问表中的数据。使用索引可以提高查询效率,降低数据库的负载。下面是MySQL索引的一些基本概念和使用方法: 1. 索引类型 MySQL支持多种类型的索引,包括B树索引、哈希索引、全文索引等。其中,B树索引是最常用的一种,也是默认的索引类型。B树索引可以用于精确匹配和范围查询,而哈希索引主要用于等值查询,全文索引则用于文本检索。 2. 索引创建 可以在创建表时指定索引,例如: ``` CREATE TABLE users ( id INT PRIMARY KEY, name VARCHAR(50), email VARCHAR(50), INDEX idx_email (email) ); ``` 也可以在已有的表上添加索引,例如: ``` ALTER TABLE users ADD INDEX idx_name (name); ``` 3. 索引使用 查询语句中可以使用WHERE子句和ORDER BY子句来利用索引,例如: ``` SELECT * FROM users WHERE email = 'example@example.com'; SELECT * FROM users WHERE name LIKE 'John%' ORDER BY id DESC; ``` 需要注意的是,索引并不是越多越好,过多的索引会占用过多的磁盘空间并降低写操作的性能。因此,需要根据实际情况选择合适的索引。同时,还需要定期对索引进行维护,包括优化查询语句、删除不必要的索引等。 4. 索引优化 MySQL提供了一些工具来优化索引,例如EXPLAIN命令可以帮助分析查询语句的执行计划,找出慢查询和不必要的全表扫描。可以使用OPTIMIZE TABLE命令来优化表的索引和碎片,从而提高查询性能。还可以使用缓存来避免频繁的查询操作,例如使用Memcached或Redis等缓存工具。 以上就是MySQL索引的一些基本概念和使用方法,需要根据实际情况进行选择和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值