问题来了:
我们有一个 MySQL 数据库,我们在其中存储 JSON 字符串.直到现在这都没问题,但突然我们的客户要求我们对这些对象进行搜索/排序/其他操作.由于项目处于相当晚的状态,去 Mongo 不是团队负责人想要做出的选择,所以我问有没有一种快速的方法来使用这些 JSON 字符串?
We have a MySQL database in which we store JSON strings. Until now this was no problem, but suddenly our client requested we do search/order/other operations with these objects. Since the projects is at a fairly late state, going to Mongo is not a choice the team leader wants to make, so I'm asking is there a quick way to use these JSON strings?
JSON as Intention
在 MySQL 中无法使用 JSON.一些 DBMS 可能支持 JSON,但这是不可能的,此外,任何类型的支持"都只是关于执行一些特定于 JSON 的操作,而不是关于对您的架构进行建模(这两件事完全不同)更多,从完整意义上讲,MySQL 的模型概念(即关系)与 JSON 不同的是:作为关系 DBMS,它遵循 关系数据模型,和JSON是完全不同的格式.您将其存储为纯字符串值,因此,如果不使用字符串函数,则无法以其他方式对其进行任何操作.因此,即使使用 JSON,您也不会在关系模型中执行此操作,因此无法维护关系特性,例如参照完整性.
There is no way to work with JSON in MySQL. Some DBMS may support JSON, but that's out of the question, and, besides, any sort of such "support" is only about performing some JSON-specific operations, but not about modelling your architecture (and those two things are completely different) More, in full sense, model concept (i.e. relations) for MySQL is different than JSON is: as a Relational DBMS, it follows relational data model, and JSON is completely different format. You will store it as plain string value, thus, impossible to do anything with it in some other way, if not use string functions. So even working with JSON you will do that not inside relational model, thus, it won't be possible to maintain relational features, such as referential integrity, for instance.
解决方案
您有多种选择:
CREATE FUNCTION
声明).当然,这将需要特定的技能和时间.坏事:错误.即使您能够比重构架构或迁移到 Mongo 更快地创建这些功能,您也永远无法确定这些功能的质量.没有办法在本地测试该代码.但是,我可能会提示用户级功能的情况 - 您可以使用 mysql-unit
测试您存储的代码,如果您的 MySQL 是 5.6 或更高版本(好吧,我已经编写了这个工具,但是..它也可能包含错误)CREATE FUNCTION
statement). This will require specific skills and time, of course. The bad things: bugs. Even if you'll be able to create those functions faster than re-structure your architecture or migrate to Mongo, you will never be certain of quality of those functions. There's no way to test that code natively. However, I may give hint for the case of user-land functions - you may use mysql-unit
to test your stored code, if your MySQL is 5.6 or higher (well, I've written this tool, but.. it may contain bugs too)标准"功能
最后,如果您运行的是 MySQL 5.7,那么预发布 JSON 函数可能会带来一线希望 - 因此,您可以尝试使用 JSON 功能的 alfa 版本,该功能目前存在于 MySQL 5.7.但我不会(强烈)建议在实际项目中使用它,因为这些功能既没有经过充分测试也没有完全完成.但是,要安装这些功能,您需要下载相应的软件包,然后将它们插入您的服务器,例如:
Finally, if you're running MySQL 5.7, then there may be a ray of hope with pre-release JSON functions - so, you may try to use alfa-version of JSON functionality, which currently exists for MySQL 5.7. But I would not (strongly) recommend to use that in real project since those functions are neither well-tested nor complete at all. But, to install those function, you'll need to download corresponding package, then plug them into your server, like:
CREATE FUNCTION json_append RETURNS string SONAME 'libmy_json_udf.so';
CREATE FUNCTION json_valid RETURNS integer SONAME 'libmy_json_udf.so';
CREATE FUNCTION json_extract RETURNS string SONAME 'libmy_json_udf.so';
CREATE FUNCTION json_replace RETURNS string SONAME 'libmy_json_udf.so';
CREATE FUNCTION json_remove RETURNS string SONAME 'libmy_json_udf.so';
CREATE FUNCTION json_set RETURNS string SONAME 'libmy_json_udf.so';
CREATE FUNCTION json_merge RETURNS string SONAME 'libmy_json_udf.so';
CREATE FUNCTION json_search RETURNS string SONAME 'libmy_json_udf.so';
CREATE FUNCTION json_contains_key RETURNS integer SONAME 'libmy_json_udf.so';
之后你就可以尝试它们了.
And after that you'll be able to try them.
这篇关于有没有办法在SQL中使用json对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!