Title: Slow SQL query
Last modified: August 21, 2016

---

# Slow SQL query

 *  [Frejon](https://wordpress.org/support/users/frejon/)
 * (@frejon)
 * [12 years, 7 months ago](https://wordpress.org/support/topic/slow-sql-query-1/)
 * Hi,
 * New here at the forum (except for reading sometimes without being logged in) 
   and need some help 🙂
 * I need to get a custom menu from another site (sharing the same MySql but different
   databases).
 * I’m doing it this way:
 *     ```
       function get_main_site_menus($custom_menu) {
   
       		global $wpdb;
       		$mydb = new wpdb('root','xxx','xxx','localhost');
   
       		$menu_items = '';
   
       		$custom_menu_items = $mydb->get_results("
       			SELECT p2.post_title, p2.guid
       	            FROM wp_posts p1
       	            LEFT JOIN wp_term_relationships AS TR
       	            ON TR.object_id = p1.ID
       	            LEFT JOIN wp_postmeta AS PM
       	            ON pm.post_id = p1.ID
       	            LEFT JOIN wp_posts AS p2
       	            ON p2.ID = PM.meta_value
       	            WHERE p1.post_type = 'nav_menu_item'
       	            AND TR.term_taxonomy_id = ( SELECT wp_terms.term_id FROM wp_terms WHERE wp_terms.slug = '$custom_menu')
       	            AND pm.meta_key = '_menu_item_object_id'
       	            ORDER BY p1.menu_order ASC
       			");
       		if ($custom_menu_items) {
       		    foreach($custom_menu_items as $custom_menu_item) {
       				$menu_items .= '
       <li><a>guid . '">' . $custom_menu_item->post_title . '</a></li>
       ';
       			}
       		}
   
       		return $menu_items;
   
       	}
       ```
   
 * The problem is that this slows down the page with about two seconds of loadtime.
   Why is this function or SQL query that slow? What can I do to improve it?
 * Thanks in advance for any help!

The topic ‘Slow SQL query’ is closed to new replies.

## Tags

 * [menu](https://wordpress.org/support/topic-tag/menu/)
 * [MySQL](https://wordpress.org/support/topic-tag/mysql/)
 * [query](https://wordpress.org/support/topic-tag/query/)
 * [sql](https://wordpress.org/support/topic-tag/sql/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 0 replies
 * 1 participant
 * Last reply from: [Frejon](https://wordpress.org/support/users/frejon/)
 * Last activity: [12 years, 7 months ago](https://wordpress.org/support/topic/slow-sql-query-1/)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
