Classic usage of MySQL----temporary table and memory table

MySQL5.5 performance optimization - memory table

Temporary table and memory table

There are two types of memory tables, but the common point is that after restarting the database, all data in memory will be lost, the functions of memory tables are partially limited, and some attributes cannot be used like normal tables, so please refer to them carefully when using them. Official document. The following is just a suggestion. 
1. Temporary table: table is built in memory, data is in memory 
2. Memory table: table is built in disk, data is in memory, 
which includes 2 important parameters 
[mysqld] 
# memory table Capacity 
max_heap_table_size=1024M 
# Temporary table capacity 
tmp_table_size=1024M 

When creating a memory table, in 5.5, you need to specify the engine type of the table ENGINE=MEMORY 
CREATE TABLE coldtest_vardata ( 
  Id int(11) AUTO_INCREMENT, 
  name varchar(255) 
) ENGINE=MEMORY DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT; 

temporary table 
create temporary table tmp1(id int not null); 

other common parameters 
# skip hostname, just use ip 
skip-name-resolve 

# auto start event 

event_scheduler=1

 

2. Mysql 5.5 cannot log in remotely: Can't get hostname for your address

Error message: Can't get hostname for your address 
Modify the configuration file 
under windows, the file is my.ini, under Linux is my.cnf The 
solution is 
[mysqld] 
skip-name-resolve #Add this attribute 

 

It will prevent MySQL Server from DNS resolution for external connections. Using this option can eliminate the time that MySql does DNS resolution.

However, it should be noted that if this option is enabled, all remote host connection authorizations must use the IP address method, otherwise MySQL will not be able to process connection requests normally.

 

The process of mysql processing client parsing can be understood as follows:

1. When the mysql client initiates a connection request, MySql Server will actively check the client's host name.

2. First, find the /etc/hosts file in the Windows system directory, and search for the correspondence between the domain name and the IP.

3. If there is no hosts file, search for DNS settings. If no DNS server is set, it will return failure immediately; if a DNS server is set, reverse resolution will be performed until timeout.

Note: If the skip-name-resolve option is enabled, to confirm whether MySql has used the authorization of the host name,

Run the following command in mysql:

mysql> select user,host from mysql.user where host <> 'localhost' ;

Typically a record with "%" authorization (that is, any address) is obtained:

+------------------+-------------+

| user             | host        |

+------------------+-------------+

| root             | %           |

| user_sync | 192.168.0.113 |

如果有host名是什么“DB1”“DB2”的,那么删除授权表中有 hostanme 的记录,然后重启mysqld。

3。mysql JDBC 驱动常用的有两个,一个是gjt(Giant Java Tree)组织提供的mysql驱动,其JDBC Driver名称(JAVA类名)为:org.gjt.mm.mysql.Driver

详情请参见网站:http://www.gjt.org/另一个是mysql官方提供的JDBC Driver,其Java类名为:com.mysql.jdbc.Driver
驱动下载网址:http://dev.mysql.com/downloads/,进入其中的MySQL Connector/J区域下载。

mysql JDBC URL格式如下:
jdbc:mysql://[host:port]/[database][?参数名1][=参数值1][&参数名2][=参数值2]...

 

MySQL内存表相信大家都不会陌生,下面就对MySQL内存表heap使用进行了总结归纳,供您参考学习之用。

MySQL内存表使用哈希散列索引把数据保存在内存中,因此具有极快的速度,适合缓存中小型数据库,但是使用上受到一些限制。

1、heap对所有用户的连接是可见的,这使得它非常适合做缓存。

2、仅适合使用的场合。heap不允许使用xxxTEXT和xxxBLOB数据类型;只允许使用=和<=>操作符来搜索记录(不允许& lt;、>、<=或>=);不支持auto_increment;只允许对非空数据列进行索引(not null)。
注:操作符 “<=>” 说明:NULL-safe equal.这个操作符和“=”操作符执行相同的比较操作,不过在两个操作码均为NULL时,其所得值为1而不为NULL,而当一个操作码为NULL时,其所得值为0而不为NULL。

3、一旦服务器重启,所有heap表数据丢失,但是heap表结构仍然存在,因为heap表结构是存放在实际数据库路径下的,不会自动删除。重启之后,heap将被清空,这时候对heap的查询结果都是空的。

4、如果heap是复制的某数据表,则复制之后所有主键、索引、自增等格式将不复存在,需要重新添加主键和索引,如果需要的话。

5、对于重启造成的数据丢失,有以下的解决办法:
a、在任何查询之前,执行一次简单的查询,判断heap表是否存在数据,如果不存在,则把数据重新写入,或者DROP表重新复制某张表。这需要多做一次查询。不过可以写成include文件,在需要用该heap表的页面随时调用,比较方便。
b、对于需要该heap表的页面,在该页面第一次且仅在第一次查询该表时,对数据集结果进行判断,如果结果为空,则需要重新写入数据。这样可以节省一次查询。
c、更好的办法是在mysql每次重新启动时自动写入数据到heap,但是需要配置服务器,过程比较复杂,通用性受到限制。
蓝草目前采用的是第二种办法。

6、一些预期可能用到的sql语句

//如果表存在,则删除
DROP TABLE IF EXISTS `abc`;
//复制整张表xyz为heap表abc(包含所有数据)
CREATE TABLE `abc` type=heap select * from `xyz`;
//添加主键id
ALTER TABLE `abc` ADD PRIMARY KEY (`id`);
//添加索引username
ALTER TABLE `abc` ADD INDEX `abc` (`username`);

Guess you like

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