简体   繁体   English

MySQL:创建临时表时是否自动创建主键?

[英]MySQL: Is a primary key created automatically when creating a temporary table?

I have a query that is taking quite a long time (11 million or so observations), and three joins (I can't stop it to check). 我有一个查询需要相当长的时间(1100万左右的观察)和三个连接(我不能阻止它检查)。 One of the joins is with a temporary table. 其中一个连接是临时表。

When I create a temporary table using data from a table that has a primary key within it, will the new table carry over the indexing, or do I have to explicitly create an index in the new temporary table (with the primary key from the parent table)? 当我使用其中包含主键的表中的数据创建临时表时,新表是否会继承索引,或者我是否必须在新临时表中显式创建索引(使用父键中的主键)表)?

No - For explicitly defined temporary tables no indexes are defined automatically. 否 - 对于显式定义的临时表,不会自动定义索引。 You will need to either define the index while the table creation or afterwards with ALTER TABLE .. . 您需要在创建表时或之后使用ALTER TABLE ..定义索引。

You can check it with SHOW CREATE TABLE my_temptable . 您可以使用SHOW CREATE TABLE my_temptable

Try the following script: 请尝试以下脚本:

drop table if exists my_persisted_table;
create table my_persisted_table (
    id int auto_increment primary key,
    col varchar(50)
);
insert into my_persisted_table(col) values ('a'), ('b');

drop temporary table if exists my_temptable;
create temporary table my_temptable as 
    select * from my_persisted_table;

show create table my_temptable;

alter table my_temptable add index (id);

show create table my_temptable;

The first SHOW CREATE statement will show no indexes: 第一个SHOW CREATE语句将不显示索引:

CREATE TEMPORARY TABLE `my_temptable` (
  `id` int(11) NOT NULL DEFAULT '0',
  `col` varchar(50) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8

After creating an index with ALTER TABLE we can see it with the second SHOW CREATE statement: 使用ALTER TABLE创建索引后,我们可以使用第二个SHOW CREATE语句查看它:

CREATE TEMPORARY TABLE `my_temptable` (
  `id` int(11) NOT NULL DEFAULT '0',
  `col` varchar(50) DEFAULT NULL,
  KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

Demo: http://rextester.com/JZQCP29681 演示: http//rextester.com/JZQCP29681

TEMPORARY tables have a very loose relationship with databases (schemas). TEMPORARY表与数据库(模式)的关系非常松散。 Dropping a database does not automatically drop any TEMPORARY tables created within that database. 删除数据库不会自动删除在该数据库中创建的任何TEMPORARY表。 Also, you can create a TEMPORARY table in a nonexistent database if you qualify the table name with the database name in the CREATE TABLE statement. 此外,如果使用CREATE TABLE语句中的数据库名限定表名,则可以在不存在的数据库中创建TEMPORARY表。 In this case, all subsequent references to the table must be qualified with the database name. 在这种情况下,必须使用数据库名称限定对表的所有后续引用。

during generation of TEMPORARY table you have to mention all record of the table

https://dev.mysql.com/doc/refman/5.7/en/create-temporary-table.html https://dev.mysql.com/doc/refman/5.7/en/create-temporary-table.html

This syntax also works: 此语法也有效:

create temporary table my_temptable
    ( PRIMARY KEY(id) )
    select * from my_persisted_table;

That is, you can have extra CREATE TABLE clauses in place from the start. 也就是说,您可以从一开始就有额外的CREATE TABLE子句。 This can be especially effective if the SELECT is delivering the rows to an InnoDB table in PK order: 如果SELECT以PK顺序将行传递到InnoDB表,这可能特别有效:

create temporary table my_temptable
    ( PRIMARY KEY(id) )
        ENGINE=InnoDB
    select * from my_persisted_table
        ORDER BY id;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM