Why can temporary tables have the same name?

In the previous article, we used temporary tables when optimizing join queries. At that time, we used it like this:

create temporary table temp_t like t1;
alter table temp_t add index(b);
insert into temp_t select * from t2 where b>=1 and b<=2000;
select * from t1 join temp_t on (t1.b=temp_t.b);

You may have questions, why use a temporary table? Is it possible to use ordinary tables directly?

Today we start with this question: what are the characteristics of temporary tables, and why is it suitable for this scenario?

Here, I need to help you clarify an easily misunderstood problem: some people may think that a temporary table is a memory table. However, the two concepts are completely different.

1. The memory table refers to the table using the Memory engine. The table creation syntax is create table ... engine=memory. The data in this kind of table is stored in memory and will be cleared when the system is restarted, but the table structure is still there. Except for these two features that look "strange", from other features, it is a normal table.
2. For temporary tables, various engine types can be used. If it is a temporary table using the InnoDB engine or MyISAM engine, when writing data, it is written to the disk. Of course, temporary tables can also use the Memory engine.

After clarifying the difference between memory tables and temporary tables, let's take a look at the characteristics of temporary tables.

Characteristics of Temporary Tables

For ease of understanding, let's look at the following sequence of operations:

Guess you like

Origin blog.csdn.net/yzh_2017/article/details/128725452