Index: src/wp-includes/class-wp-tax-query.php
===================================================================
--- src/wp-includes/class-wp-tax-query.php	(revision 37640)
+++ src/wp-includes/class-wp-tax-query.php	(working copy)
@@ -614,21 +614,30 @@
 
 		$resulting_field = sanitize_key( $resulting_field );
 
-		switch ( $query['field'] ) {
-			case 'slug':
-			case 'name':
-				foreach ( $query['terms'] as &$term ) {
-					/*
-					 * 0 is the $term_id parameter. We don't have a term ID yet, but it doesn't
-					 * matter because `sanitize_term_field()` ignores the $term_id param when the
-					 * context is 'db'.
-					 */
-					$term = "'" . esc_sql( sanitize_term_field( $query['field'], $term, 0, $query['taxonomy'], 'db' ) ) . "'";
-				}
+		$key          = md5( serialize( $query ) . $resulting_field );
+		$last_changed = wp_cache_get( 'last_changed', 'terms' );
+		if ( ! $last_changed ) {
+			$last_changed = microtime();
+			wp_cache_set( 'last_changed', $last_changed, 'terms' );
+		}
+		$cache_key = "transform_query:$key:$last_changed";
+		$terms     = wp_cache_get( $cache_key, 'terms' );
+		if ( false === $terms ) {
+			switch ( $query['field'] ) {
+				case 'slug':
+				case 'name':
+					foreach ( $query['terms'] as &$term ) {
+						/*
+						 * 0 is the $term_id parameter. We don't have a term ID yet, but it doesn't
+						 * matter because `sanitize_term_field()` ignores the $term_id param when the
+						 * context is 'db'.
+						 */
+						$term = "'" . esc_sql( sanitize_term_field( $query['field'], $term, 0, $query['taxonomy'], 'db' ) ) . "'";
+					}
 
-				$terms = implode( ",", $query['terms'] );
+					$terms = implode( ",", $query['terms'] );
 
-				$terms = $wpdb->get_col( "
+					$terms = $wpdb->get_col( "
 					SELECT $wpdb->term_taxonomy.$resulting_field
 					FROM $wpdb->term_taxonomy
 					INNER JOIN $wpdb->terms USING (term_id)
@@ -635,23 +644,25 @@
 					WHERE taxonomy = '{$query['taxonomy']}'
 					AND $wpdb->terms.{$query['field']} IN ($terms)
 				" );
-				break;
-			case 'term_taxonomy_id':
-				$terms = implode( ',', array_map( 'intval', $query['terms'] ) );
-				$terms = $wpdb->get_col( "
+					break;
+				case 'term_taxonomy_id':
+					$terms = implode( ',', array_map( 'intval', $query['terms'] ) );
+					$terms = $wpdb->get_col( "
 					SELECT $resulting_field
 					FROM $wpdb->term_taxonomy
 					WHERE term_taxonomy_id IN ($terms)
 				" );
-				break;
-			default:
-				$terms = implode( ',', array_map( 'intval', $query['terms'] ) );
-				$terms = $wpdb->get_col( "
+					break;
+				default:
+					$terms = implode( ',', array_map( 'intval', $query['terms'] ) );
+					$terms = $wpdb->get_col( "
 					SELECT $resulting_field
 					FROM $wpdb->term_taxonomy
 					WHERE taxonomy = '{$query['taxonomy']}'
 					AND term_id IN ($terms)
 				" );
+			}
+			wp_cache_add( $cache_key, $terms, 'terms' );
 		}
 
 		if ( 'AND' == $query['operator'] && count( $terms ) < count( $query['terms'] ) ) {
