MySQLIn certain scenarios when processing requests, the server creates internal temporary tables. That is, the table is processed in memory using the MEMORY engine, or stored on disk using the MyISAM engine. If the table is too large, the server may transfer the temporary table in memory to the disk.
About derived tables
When the main query contains a derived table, or when the select statement contains a union clause, or when the select statement contains an order by clause of a field (group by clause for another field), in order to complete the query, MySQL needs to automatically create a temporary table to store the temporary result set. This temporary table is created and maintained by MySQL itself and becomes an automatically created temporary table. For automatically created temporary tables, because the performance of the memory temporary table is superior, MySQL always uses the memory temporary table first. When the memory temporary table becomes too large and reaches a certain threshold, the memory temporary table will be dumped. It is an external temporary table. In other words, the external storage temporary table is an extension of the memory temporary table in the storage space. The threshold for transferring a memory temporary table to an external memory temporary table is determined by the smaller value of the system variables max_heap_table_size and tmp_table_size.
Derived tables are generally used in the from clause. Such as:
select * from (select * from table) as t;
About temporary tables
When working on very large tables, you may occasionally need to run many queries to obtain a small subset of a large amount of data Rather than running these queries against the entire table, it may be faster to have MySQL find the few records needed at a time, select the records into a temporary table, and then run the query on those tables.
Creating a temporary table is easy, add the TEMPORARY keyword to the normal CREATE TABLE statement:
CREATE TEMPORARY TABLE tmp_table ( name VARCHAR(10) NOT NULL, value INTEGER NOT NULL )
The temporary table will exist while you are connected to MySQL. When you disconnect, MySQL will automatically drop the table and free up the used space. Of course you can drop the table and free up space while still connected.
DROP TABLE tmp_table
If a table named tmp_table already exists in the database when you create a temporary table named tmp_table, the temporary table will need to be masked (hidden) ) non-temporary table tmp_table.
If you declare the temporary table to be a HEAP table, MySQL also allows you to specify that it be created in memory:
CREATE TEMPORARY TABLE tmp_table ( name VARCHAR(10) NOT NULL, value INTEGER NOT NULL ) TYPE = HEAP
Because the HEAP table is stored in memory, the queries you run on it may be larger than Temporary tables on disk are faster. However, HEAP tables are somewhat different from ordinary tables and have their own limitations. See the MySQL Reference Manual for details.
As suggested earlier, you should test temporary tables to see if they are actually faster than running queries against a large database. If the data is well indexed, the temporary table may not be fast at all.
1. After the temporary table is disconnected from mysql, the system will automatically delete the data in the temporary table, but this is only limited to tables created with the following statement:
Define fields:
CREATE TEMPORARY TABLE tmp_table ( name VARCHAR(10) NOT NULL, value INTEGER NOT NULL )
2) Directly import the query results into the temporary table
CREATE TEMPORARY TABLE tmp_table SELECT * FROM table_name
2. In addition, mysql also allows you to create temporary tables directly in memory, because everything in memory will be very fast. The syntax is as follows:
CREATE TEMPORARY TABLE tmp_table ( name VARCHAR(10) NOT NULL, value INTEGER NOT NULL ) TYPE = HEAP
3. From the above analysis, we can see that the data in the temporary table will be cleared. It will be cleared automatically when you disconnect the connection, but it is impossible to issue SQL every time in your program. Just connect to the database once (if this is the case, then there will be the problem you are worried about, if not, there will be no problem), because the data will be cleared only when the database connection is disconnected. If you issue multiple sql in a database connection, the system Temporary table data will not be cleared automatically.
The above is the detailed content of Detailed introduction to MySQL temporary tables and derived tables. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于架构原理的相关内容,MySQL Server架构自顶向下大致可以分网络连接层、服务层、存储引擎层和系统文件层,下面一起来看一下,希望对大家有帮助。

方法:1、利用right函数,语法为“update 表名 set 指定字段 = right(指定字段, length(指定字段)-1)...”;2、利用substring函数,语法为“select substring(指定字段,2)..”。

mysql的msi与zip版本的区别:1、zip包含的安装程序是一种主动安装,而msi包含的是被installer所用的安装文件以提交请求的方式安装;2、zip是一种数据压缩和文档存储的文件格式,msi是微软格式的安装包。

在mysql中,可以利用char()和REPLACE()函数来替换换行符;REPLACE()函数可以用新字符串替换列中的换行符,而换行符可使用“char(13)”来表示,语法为“replace(字段名,char(13),'新字符串') ”。

转换方法:1、利用cast函数,语法“select * from 表名 order by cast(字段名 as SIGNED)”;2、利用“select * from 表名 order by CONVERT(字段名,SIGNED)”语句。

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于MySQL复制技术的相关问题,包括了异步复制、半同步复制等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了mysql高级篇的一些问题,包括了索引是什么、索引底层实现等等问题,下面一起来看一下,希望对大家有帮助。

在mysql中,可以利用REGEXP运算符判断数据是否是数字类型,语法为“String REGEXP '[^0-9.]'”;该运算符是正则表达式的缩写,若数据字符中含有数字时,返回的结果是true,反之返回的结果是false。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version
Chinese version, very easy to use

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools
