简体   繁体   English

在MySQL DB中存储base64编码值的最佳方法?

[英]Best way to store a base64 encoded value in MySQL DB?

I have a value I'd like to store in my DB, does the Collation make any difference how a string like this 我有一个值,我想存储在我的数据库中,Collat​​ion是否会像这样的字符串有什么不同

YToyOntzOjIwOiJUeXBlX29mX29yZ2FuaXNhdGlvbiI7czoyMDoiTWVtYmVyIG9mIFBhcmxpYW1lbnQiO3M6ODoiUG9zdGNvZGUiO3M6NzoiUEUxIDFKQSI7fQ==

Might be stored? 可能存储? Also, the only way I seem to be able to write these values to the DB is by using a BLOB this seems to be a really rather wrong way of storing it. 此外,我似乎能够将这些值写入数据库的唯一方法是使用BLOB这似乎是一种非常错误的存储方式。

The collation only makes a difference if you need to ORDER BY or search the column. 如果您需要ORDER BY或搜索列,则排序规则只会有所不同。 These base64 encoded items are probably not going to be searched or sorted. 这些base64编码的项目可能不会被搜索或排序。

If your encoded items are guaranteed to be less than 64K bytes in length, define your column like this: 如果保证编码项的长度小于64K字节,请按如下方式定义列:

   `columnname` TEXT CHARACTER SET ascii,

This is exactly what's needed for a base64 encoded variable; 这正是base64编码变量所需要的; the encoding process turns everything into displayable ASCII. 编码过程将所有内容都转换为可显示的ASCII。

If the items will be less than 16 megabytes in length, but some will be longer than 64k, use MEDIUMTEXT instead of TEXT . 如果项目的长度小于16兆字节,但有些项目长度超过MEDIUMTEXT ,则使用MEDIUMTEXT而不是TEXT

Edit years later. 多年后编辑

The OQ encoded string, decoded, is a serialized php object: 解码后的OQ编码字符串是一个序列化的php对象:

a:2:{s:20:"Type_of_organisation";s:20:"Member of Parliament";s:8:"Postcode";s:7:"PE1 1JA";}

Observation 1: lots of this stuff gets stored in text columns without encoding it, using the utf8 or utf8mb4 character set. 观察1:使用utf8或utf8mb4字符集,很多这些东西都存储在文本列中,而不对其进行编码。 Lots? 地段? Yes. 是。 WordPress stores option data this way. WordPress以这种方式存储选项数据。

Observation 2: If it can be translated to JSON, you could use the JSON data type in recent versions of MySQL. 观察2:如果它可以转换为JSON,您可以在最新版本的MySQL中使用JSON数据类型。 JSON searches still aren't sargable, but they are structured. JSON搜索仍然不是sargable,但它们是结构化的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM