Linux数据库MySQL内部临时表策略
来源:才华咖 本文已影响2.52W人
来源:才华咖 本文已影响2.52W人
MySQL内部临时表的使用有一定的策略,从源码中关于SQL查询是否需要内部临时表。可以总结如下:
1、DISTINCT查询,但是简单的INCT查询,比如对primary key、unique key等DISTINCT查询时,查询优化器会将DISTINCT条件优化,去除DISTINCT条件,也不会创建临时表;
2、不是第一个表的字段使用ORDER BY 或者GROUP BY;
3、ORDER BY和GROUP BY使用不同的顺序;
4、用户需要缓存结果;
5、ROLLUP查询。
源码如下所示
代码地址:sql_, 函数:JOIN::optimize(),位置:sql_
/*
Check if we need to create a temporary table.
This has to be done if all tables are not already read (const tables)
and one of the following conditions holds:
- We are using DISTINCT (simple distincts are already optimized away)
- We are using an ORDER BY or GROUP BY on fields not in the first table
- We are using different ORDER BY and GROUP BY orders
- The user wants us to buffer the result.
When the WITH ROLLUP modifier is present, we cannot skip temporary table
creation for the DISTINCT clause just because there are only const tables.
*/
need_tmp= (( const_tables != tables &&
(( select_distinct || !simple_order || !simple_group) ||
( group_list && order ) ||
test(select_options & OPTION_BUFFER_RESULT))) ||
( e != ROLLUP:: STATE_NONE && select_distinct ));
内部临时表使用原则
但是使用了内部临时表,那么他是怎么存储的呢?原则是这样的:
1、当查询结果较小的情况下,使用heap存储引擎进行存储。也就是说在内存中存储查询结果。
2、当查询结果较大的情况下,使用myisam存储引擎进行存储。
3、当查询结果最初较小,但是不断增大的情况下,将会有从heap存储引擎转化为myisam存储引擎存储查询结果。
什么情况算是查询结果较小呢?从源码中if的几个参数可以看出:
1、有blob字段的情况;
2、使用唯一限制的情况;
3、当前表定义为大表的情况;
4、查询结果的选项为小结果集的情况;
5、查询结果的选项为强制使用myisam的情况。
Oracle数据库知识点:SQLPLUS介绍
Oracle数据库SQLPLUS介绍
Linux+Apache+Mysql+PHP优化技巧
六年级英语下册Unit 7 Summer holiday plans的说课稿
Yii2如何实现跨mysql数据库关联查询排序
基于ubuntu下nginx+php+mysql安装配置
计算机《MySQL》知识点:将数据装入数据库表
LINUX系统下mysql的大小写区分问题
linux正确重启MySQL的教程
计算机二级《MySQL》知识点:将数据装入数据库表
2017计算机二级MySQL考试在MySQL数据库比较日期的方法
Linux认证系统管理:linuxmail命令发送邮件失败
关于Linux禁止MySQL开机自启动的诀窍
sqlplus如何访问远程oracle数据库
分享Mysql运行环境优化(Linux系统)
数据库工程师
Linux数据库MySQL内部临时表策略
My Cousin Stella优秀英语作文
Unit 13 Lesson50 Standing Room Only说课稿
Liu Xiang in a TV Commercial Film英语美文
我的暑假计划My summer holiday plan英语作文
Linux关闭selinux安全子系统的技巧
mysql,mysqli,PDO的优劣比较
优化MySQL 数据库性能的八大技巧
英语《Module 6 Unit 1 You can play football well》的教案设计
计算机二级MySQL数据库模拟题
php基础之连接mysql数据库和查询数据
计算机二级mysql数据库模拟试题
Linux系统xlsatom命令如何使用
CentOS 7.0编译安装Nginx+MySQL+PHP的方法
数据库求职信
Zend studio for eclipse中使php可以调用mysql相关函数的设置方
My Cousin Stella英语作文
Unit 5 We love animals第一课时的内容
My Cousin Stella小学英语作文
大数据时代个人隐私保护策略
计算机二级MySQL数据库真题
Linux系统中smbclient命令的使用方法
PHP获取MySQL数据库里所有表的实现代码
MySQL数据库设计的八种优化方法