首页 > 科技 >

为什么数据库可以连接但是载入表速度很慢

2019-07-21 03:19:07 暂无 阅读:991 评论:0

Q:

为什么数据库能够保持然则载入表速度很慢

A:

前段时间对数据库做了一点小篡改,首要是aotocommit这里

涉及事务的主动提交,若是修过,能够测验修过归去测验下。

查察 MySQL 客户端的事务提交体式号令:select @@autocommit;

点窜 MySQL 客户端的事务提交体式为手动提交号令:set @@autocommit = 0;

(注:0 透露手动提交,即使用 MySQL 客户端执行 SQL 号令后必需使用commit号令执行事务,不然所执行的 SQL 号令无效,若是想撤销事务则使用 rollback 号令。1 透露主动提交,即在 MySQL 客户端不在需要手动执行 commit 号令。)

MySQL 在主动提交模式下,每个 SQL 语句都是一个自力的事务。

注重:

1、手动设置set @@autocommit = 0,即设定为非主动提交模式,只对当前的mysql号令行窗口有效,打开一个新的窗口后,默认照样主动提交;

2、对于非主动提交模式,好比在号令行中添加一笔记录,退出号令行后在从新打开号令行,之前插入的记录是不在的。(用select * from + 表名 验证一下就能够了)

mysql的autocommit(主动提交)默认是开启,其对mysql的机能有必然影响,举个例子来说,若是你插入了1000条数据,mysql会commit1000次的,若是我们把autocommit封闭掉,经由法式来掌握,只要一次commit就能够了。

1,我们能够经由set来设置autocommitmysql> set global init_connect=\"set autocommit=0\"; //提醒你用权限更高的财户来设置

ERROR 1227 (42000): Access denied; you need (at least one of) the SUPER privilege(s) for this operation

mysql> set autocommit=0;

Query OK, 0 rows affected (0.00 sec)

mysql> select @@autocommit; //查察一下autocommit的设置

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

| @@autocommit |

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

| 0 |

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

1 row in set (0.00 sec)

2,我们能够点窜mysql的设置文件my.cnf来封闭autocommit[mysqld]

init_connect='SET autocommit=0' //在mysqld里面加上这些内容

用第二种方式关,有一点要注重,保持mysql用户的权限不克大于启动mysql的用户的权限,否则init_connect='SET autocommit=0'基本不会启感化,也不会报任何错误,汗一个先。看以下实例Type 'help;' or '\\h' for help. Type '\\c' to clear the current input statement.

mysql> select @@autocommit; //mysql是启动用户,封闭autocommit成功

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

| @@autocommit |

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

| 0 |

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

1 row in set (0.00 sec)

mysql> Ctrl-C -- exit!

Aborted

zhangy@ubuntu:~$ mysql -uroot

Welcome to the MySQL monitor. Commands end with ; or \\g.

Your MySQL connection id is 2

Server version: 5.5.2-m2-log Source distribution

Type 'help;' or '\\h' for help. Type '\\c' to clear the current input statement.

mysql> select @@autocommit; //用root财户启动,不成功。

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

| @@autocommit |

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

| 1 |

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

1 row in set (0.00 sec)

这个会不会是mysql的bug呢?我在网上找了找这方面的问题,还真有。部门内容如下:

If a user has SUPER privilege, init_connect will not execute

(otherwise if init_connect will a wrong query no one can connect to server).

Note, if init_connect is a wrong query, the connection is closing without any errors

and next command will clause 'lost connection' error.

里面有一点说的很清楚If a user has SUPER privilege, init_connect will not execute,若是用户有更高级的权限,init_connect基本不会执行。

为什么数据库可以连接但是载入表速度很慢

相关文章