Reduce MySQL queries to one query to speed up

给你一囗甜甜゛ 提交于 2019-12-13 07:48:23

问题


In general_log file i have queries:

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

Is that possible to make 1 query instead of 7 queries? Will it speed up significantly?


回答1:


See if I am currently capturing traffic with the General Log:

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\

Now I turn off the capturing of the General Log below, because mine was capturing:

SET GLOBAL general_log=0; -- stop logging

I MOVE my log file to a backup directory, renaming it to GL_from_20160803_1420_to_20160806_1559

There is little ambiguity to the content and datetime range of capture that the above file embodies.

Set the new name for the log file capture (Begin segment for filename)

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

Run an app of mine that connects to the server, and General Log contains:

ChunkA:

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

Note: you may need to do

mysqladmin -u root -p flush-log

(prompted for password) in order to flush the logs from cache to the file. By the way, Sublime Text is awesome for auto-refresh of a text file currently loaded. Such as, a log file.

So my ChunkA above is the connection stub of a new connection coming in. It is driven by the commands of the program in use, whatever that may be. It is prior to your program commands that you are used to and code. If you are continuously creating new connections, executing code you write, and disconnecting, well these are all part of the baggage. You are not in control of optimizing them in any simple fashion.

What you should consider doing is turning OFF the General Query log in a production environment. And only enabling it during Debug and Test environment settings. Having it on increases unnecessary burden on you stack.




回答2:


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;


来源:https://stackoverflow.com/questions/38803078/reduce-mysql-queries-to-one-query-to-speed-up

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!