How to Create a memory table in mysql

Source: Internet
Author: User

How to Create a memory table?
Creating a MEMORY table is very simple. You only need to specify ENGINE = MEMORY:
Copy codeThe Code is as follows:
Create table 'tablename' ('columnname' varchar (256) not nul) ENGINE = memory default charset = latin1 MAX_ROWS = 100000000;

Note:

When the data in the memory table is larger than the size set by max_heap_table_size, mysql converts the data that exceeds the size and stores the data on the disk. Therefore, the performance is compromised, so we also need to adjust max_heap_table_size according to our actual situation, for example, in. in the cnf file, add the following content to [mysqld:
Max_heap_table_size = 2048 M
In addition, you can use MAX_ROWS to control the number of records in a table.

Memory tables use Hash hash indexes to store data in the memory. Therefore, it is extremely fast and suitable for caching Small and Medium databases. However, the usage is limited. The following are some of the feelings about the usage of the Blue Grass.

1. heap is visible to all users, which makes it very suitable for caching.

2. It is only suitable for use. Heap does not allow xxxTEXT and xxxBLOB data types. Only the = and <=> operators are allowed to search records (not allowed <,>, <= or> =). auto_increment is not supported; only non-empty data columns can be indexed (not null ).
Note: The operator "<=>" description: NULL-safe equal. this operator and the "=" operator perform the same comparison operation. However, when both operation codes are NULL, the obtained value is 1 instead of NULL. When an operation code is NULL, the value is 0 instead of NULL.

3. Once the server is restarted, all heap table data is lost, but the heap table structure still exists because the heap table structure is stored in the actual database path and will not be automatically deleted. After the restart, the heap will be cleared. At this time, the heap query results will be empty.

4. If heap is a replicated data table, all primary key, index, auto-increment and other formats will no longer exist after the replication. You need to re-Add the primary key and index if necessary.

5. There are the following solutions for data loss caused by restart:

A. Before any query, run a simple query to check whether the heap table has data. If no data exists, write the data again, or DROP the table to copy a table again. This requires more than one query. However, you can write an include file, which can be called at any time on the page that requires the heap table.
B. For the page on which the heap table is to be queried, the result of the dataset is judged for the first time and only for the first time. If the result is empty, the data needs to be written again. This saves one query.
C. A better way is to automatically write data to the heap every time mysql restarts. However, you need to configure the server. The process is complicated and the versatility is limited.

6. Some SQL statements that may be used as expected
Copy codeThe Code is as follows:
// Delete a table if it exists
Drop table if exists 'abc ';
// Copy the entire table xyz to heap table abc (including all data)
Create table 'abc' type = heap select * from 'xyz ';
// Add the primary key id
Alter table 'abc' add primary key ('id ');
// Add the index username
Alter table 'abc' add index 'abc' ('username ');

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.