我正在阅读使用 MEMORY ENGINE
(在内存中存储的表)的有关表格的内容。
是否可以使用
CREATE TABLE AS SELECT
语法,但创建的表使用MEMORY ENGINE
?MEMORY表的大小是否有限制?能否创建一个1.5GB物理表的MEMORY副本表?
我正在阅读使用 MEMORY ENGINE
(在内存中存储的表)的有关表格的内容。
是否可以使用CREATE TABLE AS SELECT
语法,但创建的表使用MEMORY ENGINE
?
MEMORY表的大小是否有限制?能否创建一个1.5GB物理表的MEMORY副本表?
Yes, it can be done, but not with simple syntax CREATE TABLE t AS SELECT ...
- you will need to specify table columns in full statement, Sample:
CREATE TABLE t (col1 INT(11), col2 INT(11))
ENGINE=MEMORY
AS
SELECT * FROM another_t
Maximum size of table by default if 16Mb
, but it can be adjusted with max_heap_table_size
server system variable. But please, note, that this limit is per engine - not per table. I.e. all your memory
tables will share it. To restrict size of individual table, you'll need to operate on session value for max_heap_table_size
, like
mysql> SET max_heap_table_size = 24*1024*1024;
你也可以用这种方法创建一个临时表:
CREATE TEMPORARY TABLE IF NOT EXISTS tmp ENGINE=MEMORY AS (
SELECT 'abc' As tomcat, 'def' As apache);
或者这样:
CREATE TEMPORARY TABLE IF NOT EXISTS tmp ENGINE=MEMORY AS (
SELECT tomcat, apache From servers);
请注意,第一个选项将创建两个长度为 varchar(3) 的列,而第二个选项将导入原始表中所有列的信息,包括它们的注释。
max_heap_table
的限制,表格会被悄无声息地写入磁盘。 - Mihai Stancutmp_table_size
和max_heap_table_size
中较小的值。如果内存中的临时表超过了限制,MySQL 会自动将其转换为磁盘上的 MyISAM 表。如果您执行许多高级GROUP BY
查询并且有大量内存,请增加tmp_table_size
(如果需要,也可以增加max_heap_table_size
)的值。 - Bad_Neighbor