是否使用“SET NAMES" [英] Whether to use "SET NAMES"
问题描述
在阅读 O'Reilly 的高性能 MySQL"时,我偶然发现了以下内容
In reading "High performance MySQL" from O'Reilly I've stumbled upon the following
另一个常见的垃圾查询是SETNAMES 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
是 mysqli_set_charset
和 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屋!