是否使用"SET NAMES" [英] Whether to use "SET NAMES"
问题描述
在阅读O'Reilly的高性能MySQL"时,我偶然发现了以下内容
In reading "High performance MySQL" from O'Reilly I've stumbled upon the following
另一个常见的垃圾查询是SET NAMES UTF8,这是错误的方法 反正做事(它不会改变 客户库的字符集;它 仅影响服务器).
Another common garbage query is SET NAMES UTF8, which is the wrong way to do things anyway (it does not change the client library's character set; it affects only the server).
我有点困惑,因为我曾经在每个脚本的顶部放置"SET NAMES utf8",以便让数据库知道我的查询是utf8编码的.
I'm a bit confused, because I used to put "SET NAMES utf8" on the top of every script to let the db know that my queries are utf8 encoded.
任何人都可以评论以上引用,或者,更正式地说,是什么建议或最佳实践,以确保我的数据库工作流可识别unicode.
Can anyone comment the above quote, or, to put it more formally, what are your suggestions / best practices to ensure that my database workflow is unicode-aware.
如果相关的话,我的目标语言是php和python.
My target languages are php and python if this is relevant.
推荐答案
mysql_set_charset()
是一个选项-但仅限于 ext/mysql
.对于 ext/mysqli
,它是 PDO
::mysql
,您需要指定一个连接参数.
mysql_set_charset()
would be an option - but an option limited to the ext/mysql
. For ext/mysqli
it is mysqli_set_charset
and for PDO
::mysql
you need to specify a connection parameter.
由于使用此函数会导致MySQL API调用,因此应认为它比发出查询要快得多.
As using this function results in a MySQL API call, it should be considered much faster than issuing a query.
关于性能,确保脚本和MySQL服务器之间基于UTF-8通讯的最快方法是正确设置MySQL服务器.由于SET NAMES x
是与
In respect of performance the fastest way to ensure a UTF-8-based communiction between your script and the MySQL server is setting up the MySQL server correctly. As SET NAMES x
is equivalent to
SET character_set_client = x;
SET character_set_results = x;
SET character_set_connection = x;
而SET character_set_connection = x
在内部也会执行SET collation_connection = <<default_collation_of_character_set_x>>
,您还可以设置这些服务器变量在您的my.ini/cnf
中静态地放置.
whereas SET character_set_connection = x
internally also executes SET collation_connection = <<default_collation_of_character_set_x>>
you can also set these server variables statically in your my.ini/cnf
.
请注意在同一MySQL服务器实例上运行的其他应用程序可能需要的问题,并且需要其他字符集.
Please be aware of possible problems with other applications running on the same MySQL server instance and requiring some other character set.
这篇关于是否使用"SET NAMES"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!