当前位置:网站首页>第十七期八股文巴拉巴拉说(数据库篇)
第十七期八股文巴拉巴拉说(数据库篇)
2022-08-09 21:54:00 【IT学习小镇】

点击上方蓝字关注我们
1 limit20000 如何优化


1、子查询优化法:
原始 slq 语句:select * from Member limit 10000,100
先找出第一条数据,然后大于等于这条数据的 id 就是要获取的数据
缺点:数据必须是连续的,可以说不能有 where 条件,where 条件会筛选数据,导致数据失去连续性。
select * from Member where MemberID >= (select MemberID from Member limit 100000,1) limit 100
2、使用 id 限定优化:
原始 sql 语句:select* from orders_history where type=2 limit 100000,100 这种方式假设数据表的 id 是连续递增的,则我们根据查询的页数和查询的记录数可以算出查询的 id 的范围,可以使用 idbetween and 来查询:
select * from orders_history where type=2 and id between 1000000 and 1000100 limit 100;
3、反向查找优化:
当偏移超过一半记录数的时候,先用排序,这样偏移就反转了 。
缺点:order by 优化比较麻烦,要增加索引,索引影响数据的修改效率,并且要知道总记录数,偏移大于数据的一半。
正向查找:select * from ab where id=123 limit 428775,40
反向向查找:select * from ab where id=123 order by ab_date desc limit 428775,40



2 char varchar text 区别


char:char 最大长度是 255 字符,注意是字符数和字符集没关系。可以有默认值,尾部有空格会被截断。
varchar :varchar 的最大长度 65535 是指能存储的字节数,其实最多只能存储 65532 个字节,还有 3 个字节用于存储长度。注意是字节数这个和字符集有关系。一个汉字字符用 utf8 占用 3字节,用 gbk 占用 2 字节。可以有默认值,尾部有空格不会截断。
text :text 和 varchar 基本相同。text 会忽略指定的大小这和 varchar 有所不同,text 不能有默认值。尾部有空格不会被截断。text 使用额外的 2 个字节来存储数据的大小,varchar 根据存储数据的大小选择用几个字节来存储。text 的 65535 字节全部用来存储数据,varchar则会占用 1-3 个字节去存储数据大小。
注意:以上只针对mysql而言。



3 drop delete truncate 区别


1、delete 和 truncate 只删除表的数据不删除表的结构。
2、速度,一般来说: drop> truncate >delete 。
3、delete 语句是 dml,这个操作会放到 rollback segement 中,事务提交之后才生效;如果有相应的 trigger,执行的时候将被触发. truncate,drop 是 ddl, 操作立即生效,原数据不放到 rollback segme nt 中,不能回滚. 操作不触发 trigger.。
4、drop 直接删掉表。
5、truncate 删除表中数据,再插入时自增长 id 又从 1 开始。
6、delete 删除表中数据,可以加 where 字句。



4 超键、候选键、主键、外键、视图


1、超键:在关系中能唯一标识元组的属性集称为关系模式的超键。一个属性可以为作为一个超键,多个属性组合在一起也可以作为一个超键。超键包含候选键和主键。
2、候选键:是最小超键,即没有冗余元素的超键。
3、主键:数据库表中对储存数据对象予以唯一和完整标识的数据列或属性的组合。一个数据列只能有一个主键,且主键的取值不能缺失,即不能为空值(Null)。
4、外键:在一个表中存在的另一个表的主键称此表的外键。
5、视图是一种虚拟的表,具有和物理表相同的功能。可以对视图进行增,改,查,操作,视图通常是有一个表或者多个表的行或列的子集。对视图的修改不影响基本表。它使得我们获取数据更容易,相比多表查询。



5 MySQL中都有哪些触发器?


1、Before Insert
2、After Insert
3、Before Update
4、After Update
5、Before Delete
6、After Delete




点个在看你最好看

边栏推荐
- 埃氏筛选法:统计素数个数
- Reinforcement Learning Weekly Issue 57: DL-DRL, FedDRL & Deep VULMAN
- Blender程序化建模简明教程【PCG】
- Converting angles to radians
- Pagoda measurement - building LightPicture open source map bed system
- NIO Cup 2022 Nioke Summer Multi-School Training Camp 7 CFGJ
- MLOps的演进历程
- AI+Medical: Using Neural Networks for Medical Image Recognition and Analysis
- Interpretation of the paper (DropEdge) "DropEdge: Towards Deep Graph Convolutional Networks on Node Classification"
- 一本通2074:【21CSPJ普及组】分糖果(candy)
猜你喜欢

Blender程序化建模简明教程【PCG】

STC8H开发(十五): GPIO驱动Ci24R1无线模块

Xiaohei's leetcode journey: 94. Inorder traversal of binary trees (supplementary Morris inorder traversal)
![[Implementation of the interface for adding, deleting, checking, and modifying a double-linked list]](/img/49/ebedcd4d27aa608360ac17e504f36d.png)
[Implementation of the interface for adding, deleting, checking, and modifying a double-linked list]

js十五道面试题(含答案)

Under the NVM node installation;The node environment variable configuration

Install win virtual machine on VMware
![[Microservice~Nacos] Configuration Center of Nacos](/img/c3/9d8fb0fd49a0ebab43ed604f9bd1cc.png)
[Microservice~Nacos] Configuration Center of Nacos

ACM MM 2022 | Cloud2Sketch: 长空云作画,AI笔生花

SQLi-LABS Page-2 (Adv Injections)
随机推荐
AI Knows Everything: Building and Deploying a Sign Language Recognition System from Zero
2021(ICPC)亚洲区域赛昆明站(CGHIJLM)
laravel table migration error [easy to understand]
Chatting embarrassing scenes, have you encountered it?Teach you to get the Doutu emoticon package with one click, and become a chat expert
STC8H Development (15): GPIO Drives Ci24R1 Wireless Module
单元测试
埃氏筛选法:统计素数个数
np中的round函数,ceil函数与floor函数
nvm下node安装;node环境变量配置
Deceptive Dice
Reinforcement Learning Weekly Issue 57: DL-DRL, FedDRL & Deep VULMAN
NIO Cup 2022 Nioke Summer Multi-School Training Camp 7 CFGJ
[Microservice~Nacos] Configuration Center of Nacos
Shanghai Konan SmartRocket series product introduction (3): SmartRocket iVerifier computer interlocking system verification tool
【微服务~Nacos】Nacos服务提供者和服务消费者
TRUNCATE表之后空间未释放
How to Make Your Company Content GDPR Compliant
宝塔实测-搭建LightPicture开源图床系统
MLOps的演进历程
Quotefancy ,提供鼓舞人心语录的壁纸网站 - 倾城之链