MySQL临时表中的Sql代码示例
下面的文章主要讲述的是MySQL临时表具体使用的详细讲解,我们大家都知道当工作在十分大表上运行时,在实际操作中你可能会需要运行很多的相关查询,来获的一个大量数据的小的子集,不是对整个表运行这些查询。 而是让MySQL每次找出所需的少数记录,将记录选择
下面的文章主要讲述的是MySQL临时表具体使用的详细讲解,我们大家都知道当工作在十分大表上运行时,在实际操作中你可能会需要运行很多的相关查询,来获的一个大量数据的小的子集,不是对整个表运行这些查询。
而是让MySQL每次找出所需的少数记录,将记录选择到一个临时表可能更快些,然后多这些表运行查询。
创建MySQL临时表很容易,给正常的CREATE TABLE语句加上TEMPORARY关键字:
Sql代码
<ol class="dp-xml"> <li class="alt"><span><span>CREATE TEMPORARY TABLE tmp_table ( </span></span></li> <li><span>name VARCHAR(10) NOT NULL, </span></li> <li class="alt"><span>value INTEGER NOT NULL </span></li> <li><span>) </span></li> <li class="alt"><span>CREATE TEMPORARY TABLE tmp_table ( </span></li> <li><span>name VARCHAR(10) NOT NULL, </span></li> <li class="alt"><span>value INTEGER NOT NULL </span></li> <li><span>) </span></li> </ol>
临时表将在你连接MySQL期间存在。当你断开时,MySQL将自动删除表并释放所用的空间。当然你可以在仍然连接的时候删除表并释放空间。
<ol class="dp-xml"><li class="alt"><span><span>DROP TABLE tmp_table </span></span></li></ol>
如果在你创建名为tmp_table临时表时名为tmp_table的表在数据库中已经存在,MySQL临时表将有必要屏蔽(隐藏)非临时表tmp_table。
如果你声明临时表是一个HEAP表,MySQL也允许你指定在内存中创建它:
Sql代码
<ol class="dp-xml"> <li class="alt"><span><span>CREATE TEMPORARY TABLE tmp_table ( </span></span></li> <li><span>name VARCHAR(10) NOT NULL, </span></li> <li class="alt"><span>value INTEGER NOT NULL </span></li> <li> <span>) </span><span class="attribute">TYPE</span><span> = </span><span class="attribute-value">HEAP</span><span> </span> </li> <li class="alt"><span>CREATE TEMPORARY TABLE tmp_table ( </span></li> <li><span>name VARCHAR(10) NOT NULL, </span></li> <li class="alt"><span>value INTEGER NOT NULL </span></li> <li> <span>) </span><span class="attribute">TYPE</span><span> = </span><span class="attribute-value">HEAP</span><span> </span> </li> </ol>
因为HEAP表存储在内存中,你对它运行的查询可能比磁盘上的临时表快些。然而,HEAP表与一般的表有些不同,且有自身的限制。详见MySQL参考手册。
正如前面的建议,你应该测试临时表看看它们是否真的比对大量数据库运行查询快。如果数据很好地索引,临时表可能一点不快。
1、临时表再断开于MySQL的连接后系统会自动删除MySQL临时表中的数据,但是这只限于用下面语句建立的表:
1)定义字段
<ol class="dp-xml"> <li class="alt"><span><span>CREATE TEMPORARY TABLE tmp_table ( </span></span></li> <li><span>name VARCHAR(10) NOT NULL, </span></li> <li class="alt"><span>value INTEGER NOT NULL </span></li> <li><span>) </span></li> </ol>
2)直接将查询结果导入临时表
CREATE TEMPORARY TABLE tmp_table SELECT * FROM table_name
2、另外MySQL也允许你在内存中直接创建临时表,因为是在内存中所有速度会很快,语法如下:
<ol class="dp-xml"> <li class="alt"><span><span>CREATE TEMPORARY TABLE tmp_table ( </span></span></li> <li><span>name VARCHAR(10) NOT NULL, </span></li> <li class="alt"><span>value INTEGER NOT NULL </span></li> <li> <span>) </span><span class="attribute">TYPE</span><span> = </span><span class="attribute-value">HEAP</span><span> </span> </li> </ol>
3、从上面的分析可以看出MySQL临时表的数据是会被清空的,你断开了连接就会被自动清空,但是你程序中不可能每发行一次sql就连接一次数据库吧(如果是这样的话,那就会出现你担心的问题,如果不是就没有问题),因为只有断开数据库连接才会被清空数据,在一个数据库连接里面发行多次sql的话系统是不会自动清空临时表数据的。
【编辑推荐】

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Big data structure processing skills: Chunking: Break down the data set and process it in chunks to reduce memory consumption. Generator: Generate data items one by one without loading the entire data set, suitable for unlimited data sets. Streaming: Read files or query results line by line, suitable for large files or remote data. External storage: For very large data sets, store the data in a database or NoSQL.

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

To use MySQL stored procedures in PHP: Use PDO or the MySQLi extension to connect to a MySQL database. Prepare the statement to call the stored procedure. Execute the stored procedure. Process the result set (if the stored procedure returns results). Close the database connection.

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

Since the launch of ChatGLM-6B on March 14, 2023, the GLM series models have received widespread attention and recognition. Especially after ChatGLM3-6B was open sourced, developers are full of expectations for the fourth-generation model launched by Zhipu AI. This expectation has finally been fully satisfied with the release of GLM-4-9B. The birth of GLM-4-9B In order to give small models (10B and below) more powerful capabilities, the GLM technical team launched this new fourth-generation GLM series open source model: GLM-4-9B after nearly half a year of exploration. This model greatly compresses the model size while ensuring accuracy, and has faster inference speed and higher efficiency. The GLM technical team’s exploration has not
