I’ve been using this semi-relational, or SemiSQL, if you’d like, approach to SQL since around 2009. At first, I’d store the data as In late 2015, with MySQL 5.7.8, our prayers were answered and the native support for Let’s have a look at the best practices using JSON in MySQL 8. SemiSQL vs NoSQLFor most cases, I’d recommend Mongo DB as NoSQL storage for JSON data. It’s a great and scalable database system featuring plenty of filtering, sorting, indexing, projecting and even aggregating functions, which can supply for basic relations. However, if your project uses heavy relational logic, MySQL with SemiSQL approach may suit you better. Or maybe your project already uses MySQL and switching databases is simply too much work. With MySQL’s Creating JSON storageCreating a key-value table for JSON storage is simple CREATE TABLE listings ( Read the full data SELECT id, data FROM listings;1 {"text": "Lorem ipsum", "title": "First Listing", "public": false, "address": "Klimentská 1215/25, 110 00, Prague 1"}2 {"text": "Dolor sit amet", "title": "Second Listing", "public": true, "address": "Rybná 716/24, 110 00, Prague 1"} Read partial data, preserving JSON types SELECT id, data->'$.title' as 'title', data->'$.public' as 'public' Read partial data, unquoting JSON types SELECT These I do not recommend to unquote other types than strings, especially booleans, as this would break logical operations. SELECT Updating dataYou can update the whole data object simply via UPDATE listings SET data = '{ ... }' WHERE id = 1; Or update single keys via UPDATE listings You can also use JSON data for filters, like this: UPDATE listings For multi–row update, I prefer to # for updating the whole data packagesINSERT INTO listings (id, data) VALUES Indexes and virtual (generated) columnsThe true beauty of MySQL’s JSON functions lies with generated columns. ALTER TABLE listingsADD title VARCHAR(255)
Now you can select data like this SELECT id, title, data->>'$.text' 'text', country, city IndexesAlways a good idea to add indexes, even redundant ones. Especially if your table hits considerable more reads than writes, what, in most cases, happen. ALTER TABLE listings All indexes require Projection columnsList–view projectionLet’s say your UPDATE listings You’d want to load the Let’s make a projection for what you need only, using a ALTER TABLE listingsADD listing JSON AS Virtual columns are not stored, obviously, and are only generated when selected. SELECT listing Fulltext projectionsLet’s extract a fulltext string from ALTER TABLE listingsADD ft VARCHAR(255) AS Now you can perform fulltext–optimized search like this: SELECT id, listing Datetime and timestampThese are not ALTER TABLE listingsADD dt DATETIME Column Overall table structureIf TLDR, the full structure of a smart JSON-based table for semi-relational purposes would look like this CREATE TABLE listings ( Quirks to watch forRetypingCasting JSON types into MySQL types can be tricky from time to time. Especially using {"public": true} SELECT data->'$.public' true
Order inside JSON objectsWill not be preserved. If I got you worried now, order in arrays
will be preserved, of course Not a big deal, but better to keep this in mind. Which database is best for JSON data?MongoDB is the most popular document database used by companies like Google, Facebook, and Forbes. MongoDB stores data in a binary encoded JSON format (BSON) to offer better data types support, improved indexing and querying.
What is the drawback of JSON in MySQL?The drawback? If your JSON has multiple fields with the same key, only one of them, the last one, will be retained. The other drawback is that MySQL doesn't support indexing JSON columns, which means that searching through your JSON documents could result in a full table scan.
Which is faster JSON or MySQL?From the tests I did Inserting or storing data MySQL was quicker and the code is cleaner even though you still have to design the database layout and schema it's still worth it to use MySQL. Reading and fetching data JSON was quicker, i was surprised!
Which MySQL version support JSON?MySQL provides supports for native JSON data type from version 5.7. 8 that stores JSON document in an internal format, which enables quick and efficient read access to document objects.
|