CREATE TABLE as SELECT - using MEMORY ENGINE (in RAM memory)

后端 未结 2 1992
广开言路
广开言路 2020-12-25 13:35

I was reading about tables using MEMORY ENGINE (tables stored in ram).

  • Is it possible to use CREATE TABLE AS SELECT syntax, but have the create

相关标签:
2条回答
  • 2020-12-25 14:12

    you can also create a temporary table this way:

    CREATE TEMPORARY TABLE IF NOT EXISTS tmp ENGINE=MEMORY AS (
    SELECT 'abc' As tomcat, 'def' As apache);
    

    Or this way:

    CREATE TEMPORARY TABLE IF NOT EXISTS tmp ENGINE=MEMORY AS (
    SELECT tomcat, apache From servers);
    

    Be advised, the first option will create the two columns as varchar(3), meanwhile the second option will import any column information from the original table, even their comments.

    0 讨论(0)
  • 2020-12-25 14:25
    • 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;
      
    0 讨论(0)
提交回复
热议问题