Performance of storing multiple meta fields vs one JSON encoded field

This question already has an answer here:

  • Custom post meta field effect on the performance on the post

    3 answers

Solutions Collecting From Web of "Performance of storing multiple meta fields vs one JSON encoded field"

If i create an array and convert it into JSON, will it be faster if i call 12 meta fields one by one or call the JSON, parse it and then have the variables?

Virtually no difference. The meta API means that all meta keys/fields/values are pulled into memory from the database at most once per post. So this:

$keys = [ 'this', 'that', 'then' ];
$data = [];
foreach ( $keys as $key )
    $data[ $key ] = get_post_meta( $post->ID, $key, true );

…will be just as efficient as:

$json = get_post_meta( $post->ID, 'json', true );
$data = json_decode( $json );

…well, slightly optimized (reduce the get_post_meta overhead – by which I mean PHP processing, WordPress filters etc. not database queries):

$meta = get_post_meta( $post->ID ); // No key, all metadata returned
$data = [];
foreach ( $keys as $key ) {
    $data[ $key ] = isset( $meta[ $key ] ) ? maybe_unserialize( $meta[ $key ][0] ) : null;
}