-1

在 general_log 文件中,我有疑问:

160806  9:53:26      11 Connect     dbname@localhost on 
     11 Query       SET NAMES utf8
     11 Query       SET character_set_client="utf8"
     11 Query       SET character_set_connection="utf8"
     11 Query       SET character_set_database="utf8"
     11 Query       SET character_set_results="utf8"
     11 Query       SET character_set_server="utf8"
     11 Query       SET character_set_system="utf8"
     11 Init DB     dbname

是否可以进行 1 个查询而不是 7 个查询?它会显着加速吗?

4

2 回答 2

0

https://dev.mysql.com/doc/refman/5.7/en/set-names.html says:

[The SET NAMES] statement sets the three session system variables character_set_client, character_set_connection, and character_set_results to the given character set. Setting character_set_connection to charset_name also sets collation_connection to the default collation for charset_name.

So you can skip several of the statements after SET NAMES. That leaves:

SET NAMES utf8;
SET character_set_database="utf8";
SET character_set_server="utf8";
SET character_set_system="utf8";

You should read the manual on these variables:

character_set_database

The character set used by the default database. The server sets this variable whenever the default database changes. If there is no default database, the variable has the same value as character_set_server.

There's no need for you to set this.

character_set_server

The server's default character set.

There's no need for you to set this in your session context. Set it globally in your my.cnf file and leave it alone. The only thing it is for is to establish a default when you use CREATE DATABASE and don't explicitly define a character set for the named database.

character_set_system

The character set used by the server for storing identifiers. The value is always utf8.

There's really no way for you to change this, ever.

That leaves only:

SET NAMES utf8;
于 2017-09-25T15:45:39.623 回答
0

查看我当前是否正在使用常规日志捕获流量:

SELECT @@general_log;   -- 1 if capturing, 0 if not
-- for me, a 1. This means I have been capturing (good for development. Poor idea for Production)

SELECT @@general_log_file; -- file name for General Log if capturing.
-- for me: GeneralLogBegin_20160803_1420.log

SELECT @@datadir; -- the location of the general_log, and other logs
-- for me: C:\ProgramData\MySQL\MySQL Server 5.6\Data\

现在我关闭了下面的常规日志的捕获,因为我正在捕获:

SET GLOBAL general_log=0; -- stop logging

我将我的日志文件移动到备份目录,将其重命名为GL_from_20160803_1420_to_20160806_1559

上述文件体现的捕获内容和日期时间范围几乎没有歧义。

设置日志文件捕获的新名称(文件名的开始段)

SET GLOBAL general_log_file='GeneralLogBegin_20160806_1559.log';
SET GLOBAL general_log=1; -- Start logging again

运行我的一个连接到服务器的应用程序,General Log 包含:

块A:

160806 16:08:37   170 Connect   MrSmithers@www.xxx.yyy.zzz on stackoverflow
          170 Query SHOW VARIABLES
          170 Query SELECT TIMEDIFF(NOW(), UTC_TIMESTAMP())
          170 Query SHOW COLLATION
          170 Query SET NAMES latin1
          170 Query SET character_set_results=NULL
          170 Init DB   my_db_name

注意:你可能需要做

mysqladmin -u root -p flush-log

(提示输入密码)以便将日志从缓存刷新到文件。顺便说一句,Sublime Text非常适合自动刷新当前加载的文本文件。例如,日志文件。

所以我的ChunkA上面是一个新连接的连接存根。它是由正在使用的程序的命令驱动的,不管它是什么。它在您习惯和编码的程序命令之前。如果您不断地创建新的连接、执行您编写的代码并断开连接,那么这些都是包袱的一部分。您无法控制以任何简单的方式优化它们。

您应该考虑在生产环境中关闭通用查询日志。并且仅在调试和测试环境设置期间启用它。拥有它会增加你的堆栈不必要的负担。

于 2016-08-06T20:25:15.433 回答