当前位置:网站首页>数据库索引
数据库索引
2022-04-22 19:01:00 【灵丘丶白给王】
零、使用navicat的“解释”按钮查看执行计划,每次都要重新点击一下,不然解释页就是之前的执行计划。
一、索引的设计原则
1)对于经常作为查询条件、使用频率比较高的字段,可以设置为索引列;
2)建立索引,尽量使用全表唯一的字段,建立唯一索引,这样查询效率最高;
3)建立组合索引时,注意字段顺序;
4)索引并不是越多越好,过多的索引会降低DML(数据库操作语言)的速度,需要频繁增、删、改的表受影响很大,因为每次操作数据库都要维护索引。对于数据量少、查询少的表尽量不要建立索引。
二、组合索引专讲
1)建立组合索引,要注意顺序;因为前置索引,也会命中组合索引,所以不要浪费,把经常作为查询条件的字段放在第一位

2)只要包含前置索引就会名中组合索引,与顺序无关

3)组合索引列全在where中,就可以命中索引。跟顺序无关。

4)where条件只用到组合索引的非前置列,无法命中索引。

三、造成索引失效的几种情况
1、查询结果数据行过多时(超过表总行数的30%),就不走索引了,直接全表扫描;
2、where后的查询,使用
> 或者 >=
< 或者 <=
!=
not in
is null
时不走索引
3、列上有运算
以下两条sql返回的结果一样,但列上有运算的不走索引
select * from test where object_id + 1 > 20000; // 全表
select * from test where object_id > 20000 - 1; // 索引
4、 列上有函数
select * from test where to_char(CREATED, 'yyyy-mm-dd hh24:mi:ss') = '2021-04-01 22:24:03'; // 全表
select * from test where CREATED = to_date('2021-04-01 22:24:03', 'yyyy-mm-dd hh24:mi:ss'); // 索引
5、隐式转换
当 字符类型 和 数值类型作比较时,Oracle会把字符类型转换成数值类型,即 TO_NUMBER(“STR_ID”)=3 ,然后比较。
假设表中有一字段STR_ID为字符串类型,那么会造成下述情况
a. select * from test where str_id = '3'; // 索引
b. select * from test where str_id = 3; // 全表
b没命中索引,是因为数据库对字段做了隐式转换,即 TO_NUMBER(“STR_ID”),这就相当于对字段使用函数了。
那么如果列是数字类型呢?以下两个sql都会走索引,这是因为’3’会变成3,没有发生列的隐式转换
a. select count(*) from test where data_object_id = 3; // 1行
b. select * from test where data_object_id = 3; // 全表
6、计划使用组合索引,但没用前置索引列,不会命中索引
版权声明
本文为[灵丘丶白给王]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Derek7117/article/details/124321810
边栏推荐
- System Analyst - paper writing framework construction
- TypeScript中的命名空间使用
- 一份数据满足所有数据场景?腾讯云数据湖解决方案及DLC内核技术介绍
- Nat. Commun. |用于学习生成模型的神经编码框架
- 2020-09-08 去除js代码的注释
- MacOS M1 使用 Homebrew 安装 Mysql
- Stream demo
- Introduction to Alibaba micro service component Sentinel
- leetcode 前缀和-题集
- MCU infrared module knowledge sharing theory is the basis of practical combat in the future
猜你喜欢

原来,这才是开发者打开世界读书日的正确姿势

RHCE-ansible

Introduction to Alibaba micro service component Sentinel

可以惊艳你的登录网页

Pycharm 配置 Conda,国内使用正确的镜像源地址

Redis缓存之String的滥用

Introduction to feign, a microservice invocation component

系统分析师-论文写作 框架搭建

Talk about some opportunities I've missed over the years

Introduction to ribbon of microservice load balancer
随机推荐
Introduction to high performance key value database redis
js中map()是否会改变原数组
2020-10-26 go language grpc learning
2020-09-08 去除js代码的注释
RHCE-ansible
P1794 solving many fish problem
[untitled] 2022 coal mine outburst prevention test exercise and simulation test
HMS Core Discovery第14期回顾长文|纵享丝滑剪辑,释放视频创作力
It's fun to learn programming. Why don't you know where to start when you do a project alone?
readline分析日志
Nodejs file operation
Nat. Commun. | Neural coding framework for learning generation model
Error c4996 'fopen': this function or variable may be unsafe Consider using fopen_ s instead. To disabl
【自我救赎--牛客网Top101 4天刷题计划】 第一天 热身运动
B03 based on STM32 single chip microcomputer independent key control nixie tube stopwatch Proteus design, Keil program, C language, source code, standard library version
XML file input of Chapter 13 of kettle paoding jieniu
Will map () in JS change the original array
Namespace usage in typescript
Compare the difference between the two lists. Each element of the list is a string
What are the advantages of enterprise communication software compared with other communication software