<?php
global $wpdb;
$wpdb->query( "DELETE FROM $wpdb->postmeta WHERE post_id = 123" );

echo "Beginning memory usage: " . memory_get_usage() / 1000000 . "MB\n";

$count = 5000;

$metadata = array();
for ( $i = 0; $i <= $count; $i++ ) {
	$metadata[ 'foo' . $i ] = rand_long_str( $i );
}

$time = microtime( true );
$mem = memory_get_usage();

//add_metadatas( 'post', 123, $metadata );

$wpdb->query( 'START TRANSACTION;' );
for ( $i = 0; $i <= $count; $i++ ) {
	add_metadata( 'post', 123, 'foo' . $i, $i );
}
$wpdb->query( 'COMMIT;' );

$ntime = microtime( true );
$nmem = memory_get_usage();

echo "Time: " . ( $ntime - $time ) . "\n";
echo "Memory: " . ( ( $nmem - $mem ) / 1000000 ) . "MB\n";

function rand_long_str( $length ) {
	$chars = 'abcdefghijklmnopqrstuvwxyz';
	$string = '';

	for ( $i = 0; $i < $length; $i++ ) {
		$rand = rand( 0, strlen( $chars ) - 1 );
		$string .= substr( $chars, $rand, 1 );
	}

	return $string;
}

/**
 * Add multiple metadatas for the specified object. Similar to calling add_metadata for each metadata individually,
 * and is only applicable for unique meta data. If a meta key already exists for an object it will not be stored.
 *
 * @since x.x.x
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string $meta_type  Type of object metadata is for (e.g., comment, post, or user)
 * @param int    $object_id  ID of the object metadata is for
 * @param array  $meta_data  Metadata as an key/value pair array
 *
 * @return bool  If the metadata was stored successfully.
 */
function add_metadatas($meta_type, $object_id, $meta_data) {
	global $wpdb;

	if ( ! $meta_type || ! is_array( $meta_data ) || ! is_numeric( $object_id ) ) {
		return false;
	}

	$object_id = absint( $object_id );
	if ( ! $object_id ) {
		return false;
	}

	$table = _get_meta_table( $meta_type );
	if ( ! $table ) {
		return false;
	}

	$column = sanitize_key($meta_type . '_id');

	/**
	 * Filter whether to add metadatas of a specific type.
	 *
	 * The dynamic portion of the hook, `$meta_type`, refers to the meta
	 * object type (comment, post, or user). Returning a non-null value
	 * will effectively short-circuit the function.
	 *
	 * @since x.x.x
	 *
	 * @param null|bool $check      Whether to allow adding metadata for the given type.
	 * @param int       $object_id  Object ID.
	 * @param string    $meta_key   Meta key.
	 * @param mixed     $meta_value Meta value. Must be serializable if non-scalar.
	 * @param bool      $unique     Whether the specified meta key should be unique
	 *                              for the object. Optional. Default false.
	 */
	$check = apply_filters( "add_{$meta_type}_metadatas", null, $object_id, $meta_data );
	if ( null !== $check )
		return $check;


	$sql = false;

	$rows = array();
	if( ! empty( $meta_data ) ) {
		$sql = "INSERT INTO {$table} ({$column}, meta_key, meta_value) VALUES ";

		$comma = false;
		$value_string = '( %d, %s, %s)';
		foreach( $meta_data as $key => $value ) {
			if( true == $comma ) {
				$sql .= ', ';
			}

			$sql .= $value_string;

			$sql = $wpdb->prepare( $sql, $object_id, $key, $value );
			$comma = true;
		}
	}

	if( $sql ) {
		$result = $wpdb->query( $sql );
	} else {
		return false;
	}

	if ( ! $result ) {
		return false;
	}

	wp_cache_delete($object_id, $meta_type . '_meta');

	return true;
}

/**
 * Delete metadata for the specified object.
 *
 * @since x.x.x
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string $meta_type  Type of object metadata is for (e.g., comment, post, or user)
 * @param int    $object_id  ID of the object metadata is for
 * @param string $meta_keys  Metadata keys
 * @return bool True on successful delete, false on failure.
 */
function delete_metadatas($meta_type, $object_id, $meta_keys) {
	global $wpdb;

	if ( ! $meta_type || ! is_array( $meta_keys ) || ! is_numeric( $object_id ) ) {
		return false;
	}

	$object_id = absint( $object_id );
	if ( ! $object_id ) {
		return false;
	}

	$table = _get_meta_table( $meta_type );
	if ( ! $table ) {
		return false;
	}

	$type_column = sanitize_key($meta_type . '_id');

	// expected_slashed ($meta_key)
	$meta_key = array_map( 'wp_unslash', $meta_keys );

	/**
	 * Filter whether to delete metadata of a specific type.
	 *
	 * The dynamic portion of the hook, `$meta_type`, refers to the meta
	 * object type (comment, post, or user). Returning a non-null value
	 * will effectively short-circuit the function.
	 *
	 * @since x.x.x
	 *
	 * @param null|bool $delete     Whether to allow metadata deletion of the given type.
	 * @param int       $object_id  Object ID.
	 * @param string    $meta_keys  Meta keys.
	 */
	$check = apply_filters( "delete_{$meta_type}_metadatas", null, $object_id, $meta_keys );
	if ( null !== $check )
		return (bool) $check;

	/**
	 * Fires immediately before deleting metadata of a specific type.
	 *
	 * The dynamic portion of the hook, `$meta_type`, refers to the meta
	 * object type (comment, post, or user).
	 *
	 * @since x.x.x
	 *
	 * @param int    $object_id  Object ID.
	 * @param string $meta_keys  Meta keys.
	 */
	do_action( "delete_{$meta_type}_metas", $object_id, $meta_keys );

	$sql = "DELETE FROM {$table} WHERE {$type_column} = {$object_id} and meta_key IN( ";

	$comma = false;
	foreach( $meta_keys as $meta_key ) {
		if( true == $comma ) {
			$sql .= ', ';
		}

		$sql .= " `{$meta_key}`";
		$comma = true;
	}

	$sql .= " )";


	$count = $wpdb->query( $sql );

	if ( ! $count )
		return false;

	wp_cache_delete($object_id, $meta_type . '_meta');

	/**
	 * Fires immediately after deleting metadata of a specific type.
	 *
	 * The dynamic portion of the hook name, `$meta_type`, refers to the meta
	 * object type (comment, post, or user).
	 *
	 * @since x.x.x
	 *
	 * @param int    $object_id  Object ID.
	 * @param string $meta_keys  Meta key.
	 */
	do_action( "deleted_{$meta_type}_metas", $object_id, $meta_keys );

	return true;
}
