![]() If we had to fetch a user with all of its posts, what we usually do is fetch the user from database and then using the Eloquent relations defined in the User model, we fetch all the posts related to the users. Now, being used to with ORM (Object Relational Mapping) such as Laravel Eloquent, what we usually do is define two models for each of the two entities and define relations between the two relations through Eloquent’s relations. Suppose we want to track the user and their actions who visit our website, such as some users only see the pages and others would visit the page and buy the products.Let’s consider that we have two different relations, namely users and posts. To overcome these issues, MySQL allows us to use a JSON data type field for storing the user's configurations or preferences that saves the table's space and stores the records separately, which will be the same as the number of visitors who visits the website. If the user's list will be large and more configuration/preference keys, this method is not good. Generally, we used to create a separate table that contains user_id, key, and value fields or saves it as a formatted string so that it can be parsed at runtime. Suppose we are creating a web application and want to save a user's configurations or preferences in the table. Let us understand it with the help of an example. We will use the JSON data type in MySQL because of its use-cases, where we can use a make-shift approach. If we want to retrieve data from the JSON column, the MySQL optimizer searches compatible indexes that match the JSON expressions. Also, the JSON column cannot be indexed directly because it creates an index by extracting a scalar value from the JSON column. NOTE: It is to note that we cannot store a non-null default value in the JSON column. We can define the JSON data type column in the MySQL table using the following index: ![]() ![]() The storage space required for the JSON document is roughly the same as the storage requirements for LONGBLOB and LONGTEXT. The binary format allows searching values within JSON documents directly with a key or array indexes without reading whole values. Optimized/Fast storage format means when the server reads a JSON value stored in binary format, it does not need to parse from a text representation.The JSON columns allow us to store the automatic validation of JSON documents.The following are the advantages that the JSON data type gives over storing JSON-format strings: This data type can store JSON documents more accurately than the JSON text format we had used in the past MySQL versions. 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. This useful feature allows us to retrieve the data in a large system quickly. Since it manages the individual values in a name-value pair chain that acts as a data array, we can retrieve the whole field using a single command. A collection of name/value pairs chain, which acts as a data array.Generally, the JSON data type supports two structures: It can also be parsed and generate by machines easily. It is a lightweight data-interchange format similar to other data types and can be easily read and write by humans. JSON abbreviated as JavaScript Object Notation.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |