How to make the mysql MEMORY ENGINE store more data?

前端 未结 5 680
孤独总比滥情好
孤独总比滥情好 2020-12-04 11:45

I want to alter a table from INNODB to MEMORY ENGINE.

So I typed this command:

alter table sns ENGINE=MEMORY;

Then the MySQL shows

相关标签:
5条回答
  • 2020-12-04 11:46

    If you're still having a problem, remember that the disk space a table occupies is often less than the memory requirement. Using VARCHAR (256) with a string of length 8 will consume 8 bytes on disk but because STORAGE doesn't support dynamic rows, it reserves the full 256 bytes in memory for every instance.

    0 讨论(0)
  • 2020-12-04 12:00

    Increase max_heap_table_size.

    0 讨论(0)
  • 2020-12-04 12:06

    The max size for the memory table is set on creation and altering and based on the max_heap_table_size value.

    So when you want to raise the max size for an existing memory table you can change the max_heap_table_size and then apply it by altering the table to the same storage engine.

    Example:

    # Raise max size to 4GB
    SET max_heap_table_size = 1024 * 1024 * 1024 * 4;
    
    # If already a memory table, the alter will not change anything.
    # Only apply the new max size.
    ALTER TABLE table_name ENGINE=MEMORY;
    

    Misunderstanding regarding tmp_table_size

    The tmp_table_size variable only determines the max size for internal memory tables. Not user-defined.

    As stated in the MySQL docs:

    The maximum size of internal in-memory temporary tables. This variable does not apply to user-created MEMORY tables.

    0 讨论(0)
  • 2020-12-04 12:07

    You should adjust the way you make and load the table

    CREATE TABLE sns_memory SELECT * FROM sns WHERE 1=2;
    ALTER TABLE sns_memory ENGINE=MEMORY;
    INSERT INTO sns_memory SELECT * FROM sns;
    DROP TABLE sns;
    ALTER TABLE sns_memory RENAME sns;
    

    This will get around any imposed limits by tmp_table_size and max_heap_table_size.

    Just the same, you need to do two things:

    Add this to /etc/my.cnf

    [mysqld]
    tmp_table_size=2G
    max_heap_table_size=2G
    

    this will cover mysql restarts. To set these values in mysqld right now without restarting run this:

    SET GLOBAL tmp_table_size = 1024 * 1024 * 1024 * 2;
    SET GLOBAL max_heap_table_size = 1024 * 1024 * 1024 * 2;
    

    If you are checking the above variables with

    SELECT @@max_heap_table_size;
    

    or

    SHOW VARIABLES LIKE 'max_heap_table_size';
    

    you may notice that they don't seem to change following the SET GLOBAL... statements. This is because the settings only apply to new connections to the server. Make a new connection, and you'll see the values update or you could change it within your session by running:

    SET tmp_table_size = 1024 * 1024 * 1024 * 2;
    SET max_heap_table_size = 1024 * 1024 * 1024 * 2;
    
    0 讨论(0)
  • 2020-12-04 12:07

    max_heap_table_size is what you are looking for

    0 讨论(0)
提交回复
热议问题