@@ -733,6 +733,15 @@ class wpdb {
733733 */
734734 public $ error = null ;
735735
736+ /**
737+ * Tables names cache for multisite.
738+ *
739+ * @since 6.9.0
740+ *
741+ * @var array<string, array<string, string>>
742+ */
743+ private $ tables_names_blog_cache = array ();
744+
736745 /**
737746 * Connects to the database server and selects a database.
738747 *
@@ -1042,7 +1051,7 @@ public function set_prefix( $prefix, $set_table_names = true ) {
10421051 *
10431052 * @since 3.0.0
10441053 *
1045- * @param int $blog_id
1054+ * @param int $blog_id blog id.
10461055 * @param int $network_id Optional. Network ID. Default 0.
10471056 * @return int Previous blog ID.
10481057 */
@@ -1055,13 +1064,29 @@ public function set_blog_id( $blog_id, $network_id = 0 ) {
10551064 $ this ->blogid = $ blog_id ;
10561065
10571066 $ this ->prefix = $ this ->get_blog_prefix ();
1067+ $ key_cache = $ this ->blogid . '~ ' . $ this ->siteid ;
10581068
1059- foreach ( $ this ->tables ( 'blog ' ) as $ table => $ prefixed_table ) {
1060- $ this ->$ table = $ prefixed_table ;
1061- }
1069+ if ( ! array_key_exists ( $ key_cache , $ this ->tables_names_blog_cache ) ) {
1070+
1071+ $ temp = array ();
1072+
1073+ foreach ( $ this ->tables ( 'blog ' ) as $ table => $ prefixed_table ) {
1074+ $ temp [ $ table ] = $ prefixed_table ;
1075+ $ this ->$ table = $ prefixed_table ;
1076+ }
1077+
1078+ foreach ( $ this ->tables ( 'old ' ) as $ table => $ prefixed_table ) {
1079+ $ temp [ $ table ] = $ prefixed_table ;
1080+ $ this ->$ table = $ prefixed_table ;
1081+ }
1082+
1083+ $ this ->tables_names_blog_cache [ $ key_cache ] = $ temp ;
10621084
1063- foreach ( $ this ->tables ( 'old ' ) as $ table => $ prefixed_table ) {
1064- $ this ->$ table = $ prefixed_table ;
1085+ } else {
1086+
1087+ foreach ( $ this ->tables_names_blog_cache [ $ key_cache ] as $ table => $ prefixed_table ) {
1088+ $ this ->$ table = $ prefixed_table ;
1089+ }
10651090 }
10661091
10671092 return $ old_blog_id ;
0 commit comments