MySQL 简单索引
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | create database demo_01 default charset=utf8mb4; use demo_01; CREATE TABLE `city` ( `city_id` int (11) NOT NULL AUTO_INCREMENT, `city_name` varchar (50) NOT NULL , `country_id` int (11) NOT NULL , PRIMARY KEY (`city_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE `country` ( `country_id` int (11) NOT NULL AUTO_INCREMENT, `country_name` varchar (100) NOT NULL , PRIMARY KEY (`country_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; insert into `city` (`city_id`, `city_name`, `country_id`) values (1, '西安' ,1); insert into `city` (`city_id`, `city_name`, `country_id`) values (2, 'NewYork' ,2); insert into `city` (`city_id`, `city_name`, `country_id`) values (3, '北京' ,1); insert into `city` (`city_id`, `city_name`, `country_id`) values (4, '上海' ,1); insert into `country` (`country_id`, `country_name`) values (1, 'China' ); insert into `country` (`country_id`, `country_name`) values (2, 'America' ); insert into `country` (`country_id`, `country_name`) values (3, 'Japan' ); insert into `country` (`country_id`, `country_name`) values (4, 'UK' ); drop index idx_city_name on city; show index from city; alter table city add index idx_city_name(city_name); alter table city add unique idx_city_name(city_name); create 命令创建索引: create index index_name on tb_name(字段名字); create index idx_city_name on city(city_nsme); 创建复合索引: CREATE INDEX idx_name_email_status ON tb_seller( NAME ,email,STATUS); 就相当于 对 name 创建索引 ; 对 name , email 创建了索引 ; 对 name , email, status 创建了索引 ; alter 方法加索引: 1). alter table tb_name add primary key (column_list); 该语句添加一个主键,这意味着索引值必须是唯一的,且不能为 NULL 2). alter table tb_name add unique index_name(column_list); 这条语句创建索引的值必须是唯一的(除了 NULL 外, NULL 可能会出现多次) 3). alter table tb_name add index index_name(column_list); 添加普通索引, 索引值可以出现多次。 4). alter table tb_name add fulltext index_name(column_list); 该语句指定了索引为FULLTEXT, 用于全文索引 加索引的原则: 1)对查询频次较高,且数据量比较大的表建立索引。 2)索引字段的选择,最佳候选列应当从 where 子句的条件中提取,如果 where 子句中的组合比较多,那么应当挑 选最常用、过滤效果最好的列的组合。 3)使用唯一索引,区分度越高,使用索引的效率越高。 4)索引可以有效的提升查询数据的效率,但索引数量不是多多益善,索引越多,维护索引的代价自然也就水涨 船高。对于插入、更新、删除等DML操作比较频繁的表来说,索引过多,会引入相当高的维护代价,降低 DML操作的效率,增加相应操作的时间消耗。另外索引过多的话,MySQL也会犯选择困难病,虽然最终仍然 会找到一个可用的索引,但无疑提高了选择的代价。 5)使用短索引,索引创建之后也是使用硬盘来存储的,因此提升索引访问的I/O效率,也可以提升总体的访问效 率。假如构成索引的字段总长度比较短,那么在给定大小的存储块内可以存储更多的索引值,相应的可以有 效的提升MySQL访问索引的I/O效率。 7)利用最左前缀,N个列组合而成的组合索引,那么相当于是创建了N个索引,如果查询时 where 子句中使用了 组成该索引的前几个字段,那么这条查询SQL可以利用组合索引来提升查询效率。 |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· DeepSeek “源神”启动!「GitHub 热点速览」
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器