dsds661730652211 2013-08-13 00:22
浏览 797
已采纳

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

I have a value I'd like to store in my DB, does the Collation make any difference how a string like this

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.

  • 写回答

1条回答 默认 最新

  • dongtang5229 2013-08-13 01:10
    关注

    The collation only makes a difference if you need to ORDER BY or search the column. These base64 encoded items are probably not going to be searched or sorted.

    If your encoded items are guaranteed to be less than 64K bytes in length, define your column like this:

       `columnname` TEXT CHARACTER SET ascii,
    

    This is exactly what's needed for a base64 encoded variable; the encoding process turns everything into displayable ASCII.

    If the items will be less than 16 megabytes in length, but some will be longer than 64k, use MEDIUMTEXT instead of TEXT.

    Edit years later.

    The OQ encoded string, decoded, is a serialized php object:

    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. Lots? Yes. WordPress stores option data this way.

    Observation 2: If it can be translated to JSON, you could use the JSON data type in recent versions of MySQL. JSON searches still aren't sargable, but they are structured.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog