MySQL memory table and temporary table

MySQL memory table and temporary table

 

memory table

 

session 1
$ mysql -uroot
root@(none) 10:05:06>use test
Database changed
root@test 10:06:06>CREATE TABLE tmp_memory (i INT) ENGINE = MEMORY;
Query OK, 0 rows affected (0.00 sec)
root@test 10:08:46>insert into tmp_memory values (1);
Query OK, 1 row affected (0.00 sec)
root@test 10:08:46>

session2
$ mysql -uroot test
root@test 10:05:12>CREATE TABLE tmp_memory (i INT) ENGINE = MEMORY;
ERROR 1050 (42S01): Table 'tmp_memory' already exists
root@test 10:16:27>select * from tmp_memory;
+------+
| i |
+------+
| 1 |
+------+
row in set (0.00 sec)

 

 

1. For multiple sessions, the names of the created tables cannot be the same

2. After a session creates a session, it is also visible to other sessions

3. There is only tmp_memory.frm in the data directory, the table structure is placed on the disk, and the data is placed in the memory

4. After mysql is restarted or closed, the data in the memory table will be lost, but the table structure still exists

5. Can create indexes, delete indexes, support unique indexes

6. Does not affect the master and backup, the data inserted in the master database can also be found in the slave database

7. show tables

 

 

Temporary tables

 

session1
$ mysql -uroot test
root@test 10:30:18>CREATE TEMPORARY TABLE tmp_table (name VARCHAR(10) NOT NULL,value INTEGER NOT NULL);
Query OK, 0 rows affected (0.05 sec)
root@test 10:31:54>select * from tmp_table;
+--------+-------+
| name | value |
+--------+-------+
| aaaaaa | 10 |
+--------+-------+
row in set (0.00 sec)

session2
root@test 10:20:13> CREATE TEMPORARY TABLE tmp_table (name VARCHAR(10) NOT NULL,value INTEGER NOT NULL);
Query OK, 0 rows affected (0.02 sec)

root@test 10:30:39>insert into tmp_table values ('bbbbbbb',10);
Query OK, 1 row affected (0.01 sec)

root@test 10:31:33>select * from tmp_table;
+---------+-------+
| name | value |
+---------+-------+
| bbbbbbb | 10 |
+---------+-------+
row in set (0.00 sec)

root@test 10:31:43>exit
Bye
[1 Single:MS-Master db152011.sqa.cm6: mysql ~ ]
$ mysql -uroot test
root@test 10:32:17>select * from tmp_table;
ERROR 1146 (42S02): Table 'test.tmp_table' doesn't exist
root@test 10:32:22>
root@test 10:32:23>

 

 

1. The name of the created table can be the same 

2. Table structure and data are placed in memory

3. The session disappears, the table structure and data disappear

4. You can create and delete indexes

5. The table created by the main database cannot be found by the standby database.

6. show tables can not see the table

 

 

Matters needing attention when using memory tables

1. The memory table needs its own delete data or drop table; it needs drop permission, which is more dangerous

2. The table structure of the memory table is stored on the disk. If multiple sessions use the same table name, there will be conflicts; if you do not need to use the table name, if you need to create the table structure once, there will be many small The existence of the file is not conducive to the maintenance of the db, and the dba cleaning table is also at risk;

 

 

Based on the above, it is not suitable to use a memory table

1. Temporary tables are session-level, even if the table names created by multiple sessions are the same, they will not affect each other

2. The session disappears and everything disappears, which is not conducive to application troubleshooting

 

In addition, these two need to consume additional memory space, although the db side can tolerate it, but it is not very controllable; the db side also has this parameter:

max_tmp_tables The maximum number of temporary tables that a client can keep open at the same time, this value defaults to 32, this value can be adjusted as needed

 

 

refer to:

http://www.cnblogs.com/sunss/archive/2013/07/15/3191137.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326509763&siteId=291194637