SQL code example in MySQL temporary table

Source: Internet
Author: User
The following article describes how to use a temporary MySQL table. We all know that when running a large table, in practice, you may need to run a lot of related queries to obtain a small subset of a large amount of data, rather than running these queries on the entire table. Instead, let MySQL find the required few records each time and select the record

The following article describes how to use a temporary MySQL table. We all know that when running a large table, in practice, you may need to run a lot of related queries to obtain a small subset of a large amount of data, rather than running these queries on the entire table. Instead, let MySQL find the required few records each time and select the record

The following article describes how to use a temporary MySQL table. We all know that when running a large table, in practice, you may need to run a lot of related queries to obtain a small subset of a large amount of data, rather than running these queries on the entire table.

Instead, MySQL can find a few required records each time, and it may be faster to select a temporary table, and then run the query on these tables.

It is easy to CREATE a TEMPORARY MySQL TABLE, and the TEMPORARY keyword is added to the normal create table statement:

SQL code

 
 
  1. CREATE TEMPORARY TABLE tmp_table (
  2. name VARCHAR(10) NOT NULL,
  3. value INTEGER NOT NULL
  4. )
  5. CREATE TEMPORARY TABLE tmp_table (
  6. name VARCHAR(10) NOT NULL,
  7. value INTEGER NOT NULL
  8. )

Temporary tables will exist during your connection to MySQL. When you disconnect, MySQL automatically deletes the table and releases the space used. Of course, you can delete the table and release the space when it is still connected.

 
 
  1. DROP TABLE tmp_table

If the table named tmp_table already exists in the Database when you create a temporary table named tmp_table, it is necessary to block (hide) the temporary table tmp_table.

If you declare that the temporary table is a HEAP table, MySQL also allows you to specify to create it in memory:

SQL code

 
 
  1. CREATE TEMPORARY TABLE tmp_table (
  2. name VARCHAR(10) NOT NULL,
  3. value INTEGER NOT NULL
  4. ) TYPE = HEAP
  5. CREATE TEMPORARY TABLE tmp_table (
  6. name VARCHAR(10) NOT NULL,
  7. value INTEGER NOT NULL
  8. ) TYPE = HEAP

Because the HEAP table is stored in the memory, you may query it faster than the temporary table on the disk. However, HEAP tables are somewhat different from general tables and have their own restrictions. For more information, see the MySQL reference manual.

As previously suggested, you should test temporary tables to see if they are faster than running queries on a large number of databases. If the data is well indexed, the temporary table may be a little unpleasant.

1. After a temporary table is disconnected from MySQL, the system will automatically delete the data in the temporary MySQL table. However, this is only the table created using the following statement:

1) define Fields

 
 
  1. CREATE TEMPORARY TABLE tmp_table (
  2. name VARCHAR(10) NOT NULL,
  3. value INTEGER NOT NULL
  4. )

2) directly import the query results to the temporary table

Create temporary table tmp_table SELECT * FROM table_name

2. In addition, MySQL allows you to directly create temporary tables in the memory, because all the speed in the memory will be fast, and the syntax is as follows:

 
 
  1. CREATE TEMPORARY TABLE tmp_table (
  2. name VARCHAR(10) NOT NULL,
  3. value INTEGER NOT NULL
  4. ) TYPE = HEAP

3. From the above analysis, we can see that the data in the temporary MySQL table will be cleared, and the data will be automatically cleared when you disconnect the connection, however, it is impossible for your program to connect to the database every time an SQL statement is released. (if this is the case, there will be issues you are worried about. If not, there will be no problems ), because data is cleared only when the database connection is disconnected, the system will not automatically clear the temporary table data if multiple SQL statements are released in a database connection.

[Edit recommendations]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.