• Resolved olive67

    (@olive67)


    Hello,
    I created custom post type to store information about car colors and created a search form to be able to search on 3 different criteria : color code, color name or color category. I have about 1000 entries in the DB and the response time is really very slow, even if i make a request where their are only some results.
    Below is my code (PHP is new for me) which creates the request and shows the result :
    There is probably something to improve to make these requests quicker but didn’t find an answer yet by searching on the we 🙁
    Thanks in advance for your help !

    global $post;
    
    	/* if(isset($_POST['categorie_couleur']) && !empty($_POST['categorie_couleur'])){
    	$Col1_Array = $_POST['categorie_couleur'];
    	$pp_categorie_couleur_list = '';
            foreach($Col1_Array as $selectValue){
                    $pp_categorie_couleur_list = $pp_categorie_couleur_list.$selectValue.",";
    		}
    		// pour supprimer la dernière virgule
    		$pp_categorie_couleur=substr($pp_categorie_couleur_list, 0, -1);
    	} */
    
    	theme_post_wrapper(
    		array(
    			'id' => theme_get_post_id(),
    			'class' => theme_get_post_class(),
    			'title' => theme_get_meta_option($post->ID, 'theme_show_page_title') ? get_the_title() : '',
    			'heading' => theme_get_option('theme_single_article_title_tag'),
    			'before' => theme_get_metadata_icons('edit', 'header'),
    			'content' => do_shortcode(pp_recherche_options_couleur($_POST['recherche_couleur'],$_POST['type_recherche_couleur'])
    			)
    		)
    	);
    
    /* fonction permettant de checker si une URL existe */
    function URLIsValid($URL)
    {
        $exists = true;
        $file_headers = @get_headers($URL);
        $InvalidHeaders = array('404', '403', '500');
        foreach($InvalidHeaders as $HeaderVal)
        {
                if(strstr($file_headers[0], $HeaderVal))
                {
                        $exists = false;
                        break;
                }
        }
        return $exists;
    }
    
    /*
    * Function pp_recherche_options_couleur
    * Génère la requête sur base des information spécifiées dans le formulaire et retourne le résultat
    */
    function pp_recherche_options_couleur ($recherche_couleur, $type_recherche_couleur) {
    
    	//echo "recherche couleur : ".$recherche_couleur."<br/>";
    	//echo "type recherche couleur : ".$type_recherche_couleur."<br/>";
    	switch ($type_recherche_couleur) {
    		case "nom":
    			$pp_args= array('post_type' => 'code_couleur_ext',
    							'post_status' => 'publish',
    							'posts_per_page' => -1,
    							//'no_found_rows' => true,
    							'cache_results' => false,
    							'fields' => 'ids',
    							'meta_query' => array (
    								'relation' =>'OR',
    								array(
    									'key' => 'pp_color-description_courte',
    									'value' => $recherche_couleur,
    									'type' => 'char',
    									'compare' => 'LIKE'
    									),
    								array(
    									'key' => 'pp_color-description_courte_en',
    									'value' => $recherche_couleur,
    									'type' => 'char',
    									'compare' => 'LIKE'
    									),
    								array(
    									'key' => 'pp_color-description_courte_de',
    									'value' => $recherche_couleur,
    									'type' => 'char',
    									'compare' => 'LIKE'
    									)
    							)
    						 );
    			break;
    		case "code":
    			$pp_args= array('post_type' => 'code_couleur_ext',
    							'post_status' => 'publish',
    							'posts_per_page' => -1,
    							//'no_found_rows' => true,
    							'cache_results' => false,
    							'fields' => 'ids',
    							'meta_query' => array (
    								array(
    									'key' => 'pp_color-code',
    									'value' => $recherche_couleur,
    									'type' => 'char',
    									'compare' => '='
    									)
    								)
    							);
    			break;
    		case "categorie":
    			$pp_args= array('post_type' => 'code_couleur_ext',
    							'post_status' => 'publish',
    							'posts_per_page' => -1,
    							//'no_found_rows' => true,
    							'cache_results' => false,
    							'fields' => 'ids',
    							'meta_key' => 'pp_color-categorie_couleur',
    							'meta_value' => $recherche_couleur,
    							'meta_compare' => '='
    							);
    			break;
    
    	}
    
    	// Pour debug - affichage des valeurs des paramètres
    
    	$pp_query = new WP_Query($pp_args);
    	$pp_result = '';
    	$pp_home_url=home_url();
    
    	// Affichage de la requete
    	switch ($type_recherche_couleur) {
    		case "nom":
    			$pp_result =$pp_result."<p class='none'><b>Recherche réalisée</b> : Nom de couleur contenant <b><i>".$recherche_couleur."</b></i></p>";
    			break;
    		case "code":
    			$pp_result =$pp_result."<p class='none'><b>Recherche réalisée</b> : Code couleur = ".$recherche_couleur."</p>";
    			break;
    		case "categorie":
    			$pp_result =$pp_result."<p class='none'><b>Recherche réalisée</b> : Catégorie couleur = ".$recherche_couleur."</p>";
    			break;
    
    	}
    	//affiche le nombre de résultats trouvés
    		$pp_result = $pp_result."<p class='none'><b>Résultats trouvés</b> : ".$pp_query->found_posts."</p>";
    	// lien pour retour vers la page de recherche
    	$pp_result = $pp_result."<p><a href='".$pp_url_home."/espace-technique/codes-porsche/codes-peinture/'>Retour sur la page de recherche</a></p>";
    
    	if ($pp_query->have_posts()) {
    
    		while ($pp_query->have_posts()) : $pp_query->the_post();
    
    			$pp_couleur_code = get_post_meta(get_the_ID(), 'pp_color-code', true );
    			$pp_couleur_code_description_courte = get_post_meta(get_the_ID(), 'pp_color-description_courte', true );
    			$pp_couleur_code_description_courte_en = get_post_meta(get_the_ID(), 'pp_color-description_courte_en', true );
    			$pp_couleur_code_description_courte_de = get_post_meta(get_the_ID(), 'pp_color-description_courte_de', true );
    			$pp_couleur_nom_fichier_couleur = get_post_meta(get_the_ID(), 'pp_color-nom_fichier_couleur', true );
    			$pp_couleur_nom_fichier_voiture = get_post_meta(get_the_ID(), 'pp_color-nom_fichier_voiture', true );
    			$pp_couleur_notes = get_post_meta(get_the_ID(), 'pp_color-notes', true );
    
    			$pp_couleur_url = $pp_home_url."/wp-content/uploads/colors/".$pp_couleur_nom_fichier_couleur;
    			$pp_couleur_car_url = $pp_home_url."/wp-content/uploads/colors/".$pp_couleur_nom_fichier_voiture;
    
    			$pp_result = $pp_result."<table border='1' width='100%'>";
    			$pp_result = $pp_result."<tr>";
    			$pp_result = $pp_result."<td rowspan='4' bgcolor='#646464' style='color:#E0DEDF;text-align:center' width='6%' >".$pp_couleur_code."</td>";
    			// FR
    			$pp_result = $pp_result."<td width='6%'> <img src='".$pp_home_url."/wp-content/uploads/drapeaux/fr.png'/></td>";
    			$pp_result = $pp_result."<td width='22%'>".$pp_couleur_code_description_courte."</td>";
    			if (URLIsValid($pp_couleur_url)){
    				$pp_result = $pp_result."<td rowspan='3' width='23%'><img src='".$pp_couleur_url."' border='1'/></td>";
    			}
    			else
    			{
    				$pp_result = $pp_result."<td rowspan='3' width='23%'> </td>";
    			}
    			if (URLIsValid($pp_couleur_car_url)){
    				$pp_result = $pp_result."<td rowspan='3' width='43%'><img src='".$pp_couleur_car_url."'/></td>";
    			}
    			else
    			{
    				$pp_result = $pp_result."<td rowspan='3' width='43%'> </td>";
    			}
    			$pp_result = $pp_result."</tr>";
    			// EN
    			$pp_result = $pp_result."<tr>";
    			$pp_result = $pp_result."<td width='6%'> <img src='".$pp_home_url."/wp-content/uploads/drapeaux/gb.png'/></td>";
    			$pp_result = $pp_result."<td width='22%'>".$pp_couleur_code_description_courte_en."</td>";
    			$pp_result = $pp_result."</tr>";
    			// DE
    			$pp_result = $pp_result."<tr>";
    			$pp_result = $pp_result."<td width='6%'> <img src='".$pp_home_url."/wp-content/uploads/drapeaux/de.png'/></td>";
    			$pp_result = $pp_result."<td width='22%'>".$pp_couleur_code_description_courte_de."</td>";
    			$pp_result = $pp_result."</tr>";
    			// Modèles concernés
    			$pp_result = $pp_result."<tr>";
    			$pp_result = $pp_result."<td colspan='4' style='padding:8px'>";
    			$pp_result = $pp_result."[wpspoiler name='Modèles concernés...' open='false']";
    			$pp_modeles_concernes = get_field('modeles_concernes',get_the_ID());
    			//var_dump( $pp_modeles_concernes );
    			$pp_result = $pp_result."<ul>";
        		foreach( $pp_modeles_concernes as $pp_modele_concerne) {
            		$pp_result = $pp_result."<li>";
            		$pp_cat_name = get_cat_name($pp_modele_concerne);
               		$pp_result = $pp_result.$pp_cat_name;
            		$pp_result = $pp_result."</li>";
    			}
        		$pp_result = $pp_result."</ul>";
        		// temporaire en attendant d'associer les modèles aux couleurs
        		$pp_result = $pp_result."<p>".$pp_couleur_notes."</p>";
    
        		$pp_result = $pp_result."[/wpspoiler]";
    			$pp_result = $pp_result."</td>";
    			$pp_result = $pp_result."</tr>";
    			$pp_result = $pp_result."</table>";
    			$pp_result = $pp_result."<br/>";
    		endwhile;
    		$pp_result = $pp_result."<p><a href='".$pp_url_home."/espace-technique/codes-porsche/codes-peinture/'>Retour sur la page de recherche</a></p>";
    	}
    	else
    	{
    		$pp_result = "<h3>Aucun résultat n'a été trouvé avec les critères spécifiés...</h3>";
    		$pp_result = $pp_result."<a href='".$pp_url_home."/espace-technique/codes-porsche/codes-peinture/'>Retour sur la page de recherche</a>";
    
    	}
    return ($pp_result);
    }

Viewing 5 replies - 1 through 5 (of 5 total)
  • Is the slow response because of your PHP or your SQL, it could be either (or both) ?

    Some of your PHP code is a bit clumsy, for instance:

    $pp_result = $pp_result."<tr>";
    	$pp_result = $pp_result."<td width='6%'> <img src='".$pp_home_url . "/wp-content/uploads/drapeaux/gb.png'/></td>";
    	$pp_result = $pp_result."<td width='22%'>".$pp_couleur_code_description_courte_en."</td>";
    	$pp_result = $pp_result."</tr>";

    Would be better expressed using .= and single quotes ‘ rather than double quotes ” (which require the text to be scanned and substituted) and you can run lines together too. So I would write the above as:

    $pp_result .= '<tr>'
    	  .'<td width="6%"> <img src="'
    	   . $pp_home_url . '/wp-content/uploads/drapeaux/gb.png"/>
    	    </td>'
    	  .'<td width="22%">' . $pp_couleur_code_description_courte_en
    	  .'</td>'
    	 .'</tr>';

    If you are doing enough of this it might make a difference.

    Also the communication from PHP to SQL and back is itself an expensive process, it is often quicker to do one big query rather than several little ones, (even if you might be fetching more data on some items than you will be using).

    Try and capture some of your SQL queries and run them in phpmyadmin.
    Also check if you have created SQL index/keys to best support your queries, you are probably doing lots more queries than inserts, so indexes are good for performance.

    One thing that might make a difference is to eliminate the multiple calls to get_post_meta() and replace with a single call and loop through the results, picking out the values you need.

    Even though the metadata is cached, you still go through several levels of function calls and filter checks for each call.

    You can also replace ‘get_the_ID()’ with ‘$post->ID’ to eliminate that function call.

    Thread Starter olive67

    (@olive67)

    Hello,
    Thanks you for your quick feed-back and answers 🙂
    – I adapted PHP code as suggested about string concatenation
    – I replaced the multiple get_the_ID calls by a variable in my “While” loop
    – I replaced the multiple get_post_meta calls by one call and extracted data from generated associative array

    –> unfortunately, no big performance improvements 🙁

    – When you speak about indexes, has this to be created via PhpMyAdmin for the fields on which requests are done ? Could you give me more details with exemple ?

    – I checked also in the log and the generated SQL query is :

    SELECT   wp_posts.ID FROM wp_posts  INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)
    INNER JOIN wp_postmeta AS mt1 ON (wp_posts.ID = mt1.post_id)
    INNER JOIN wp_postmeta AS mt2 ON (wp_posts.ID = mt2.post_id) WHERE 1=1  AND wp_posts.post_type = 'code_couleur_ext' AND ((wp_posts.post_status = 'publish')) AND ( (wp_postmeta.meta_key = 'pp_color-description_courte' AND CAST(wp_postmeta.meta_value AS CHAR) LIKE '%red%')
    OR  (mt1.meta_key = 'pp_color-description_courte_en' AND CAST(mt1.meta_value AS CHAR) LIKE '%red%')
    OR  (mt2.meta_key = 'pp_color-description_courte_de' AND CAST(mt2.meta_value AS CHAR) LIKE '%red%') ) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC;

    It takes some time too to execute in PhpMyadmin but is much more quicker than via PHP.

    – here my new php code with your improvements suggestions :

    global $post;
    
    	/* if(isset($_POST['categorie_couleur']) && !empty($_POST['categorie_couleur'])){
    	$Col1_Array = $_POST['categorie_couleur'];
    	$pp_categorie_couleur_list = '';
            foreach($Col1_Array as $selectValue){
                    $pp_categorie_couleur_list = $pp_categorie_couleur_list.$selectValue.",";
    		}
    		// pour supprimer la dernière virgule
    		$pp_categorie_couleur=substr($pp_categorie_couleur_list, 0, -1);
    	} */
    
    	theme_post_wrapper(
    		array(
    			'id' => theme_get_post_id(),
    			'class' => theme_get_post_class(),
    			'title' => theme_get_meta_option($post->ID, 'theme_show_page_title') ? get_the_title() : '',
    			'heading' => theme_get_option('theme_single_article_title_tag'),
    			'before' => theme_get_metadata_icons('edit', 'header'),
    			'content' => do_shortcode(pp_recherche_options_couleur($_POST['recherche_couleur'],$_POST['type_recherche_couleur'])
    			)
    		)
    	);
    
    /* fonction permettant de checker si une URL existe */
    function URLIsValid($URL)
    {
        $exists = true;
        $file_headers = @get_headers($URL);
        $InvalidHeaders = array('404', '403', '500');
        foreach($InvalidHeaders as $HeaderVal)
        {
                if(strstr($file_headers[0], $HeaderVal))
                {
                        $exists = false;
                        break;
                }
        }
        return $exists;
    }
    
    /*
    * Function pp_recherche_options_couleur
    * Génère la requête sur base des information spécifiées dans le formulaire et retourne le résultat
    */
    function pp_recherche_options_couleur ($recherche_couleur, $type_recherche_couleur) {
    
    	//echo "recherche couleur : ".$recherche_couleur."<br/>";
    	//echo "type recherche couleur : ".$type_recherche_couleur."<br/>";
    	switch ($type_recherche_couleur) {
    		case "nom":
    			$pp_args= array('post_type' => 'code_couleur_ext',
    							'post_status' => 'publish',
    							'posts_per_page' => -1,
    							//'no_found_rows' => true,
    							'cache_results' => false,
    							'fields' => 'ids',
    							'meta_query' => array (
    								'relation' =>'OR',
    								array(
    									'key' => 'pp_color-description_courte',
    									'value' => $recherche_couleur,
    									'type' => 'char',
    									'compare' => 'LIKE'
    									),
    								array(
    									'key' => 'pp_color-description_courte_en',
    									'value' => $recherche_couleur,
    									'type' => 'char',
    									'compare' => 'LIKE'
    									),
    								array(
    									'key' => 'pp_color-description_courte_de',
    									'value' => $recherche_couleur,
    									'type' => 'char',
    									'compare' => 'LIKE'
    									)
    							)
    						 );
    			break;
    		case "code":
    			$pp_args= array('post_type' => 'code_couleur_ext',
    							'post_status' => 'publish',
    							'posts_per_page' => -1,
    							//'no_found_rows' => true,
    							'cache_results' => false,
    							'fields' => 'ids',
    							'meta_query' => array (
    								array(
    									'key' => 'pp_color-code',
    									'value' => $recherche_couleur,
    									'type' => 'char',
    									'compare' => '='
    									)
    								)
    							);
    			break;
    		case "categorie":
    			$pp_args= array('post_type' => 'code_couleur_ext',
    							'post_status' => 'publish',
    							'posts_per_page' => -1,
    							//'no_found_rows' => true,
    							'cache_results' => false,
    							'fields' => 'ids',
    							'meta_key' => 'pp_color-categorie_couleur',
    							'meta_value' => $recherche_couleur,
    							'meta_compare' => '='
    							);
    			break;
    
    	}
    
    	// Pour debug - affichage des valeurs des paramètres
    
    	$pp_query = new WP_Query($pp_args);
    	$pp_result = '';
    	$pp_home_url=home_url();
    
    	// Affichage de la requete
    	switch ($type_recherche_couleur) {
    		case "nom":
    			$pp_result .= '<p class="none"><b>Recherche réalisée</b> : Nom de couleur contenant <b><i>'.$recherche_couleur.'</b></i></p>';
    			break;
    		case "code":
    			$pp_result .= '<p class="none"><b>Recherche réalisée</b> : Code couleur = '.$recherche_couleur.'</p>';
    			break;
    		case "categorie":
    			$pp_result .= '<p class="none"><b>Recherche réalisée</b> : Catégorie couleur = '.$recherche_couleur.'</p>';
    			break;
    
    	}
    	//affiche le nombre de résultats trouvés
    		$pp_result .= '<p class="none"><b>Résultats trouvés</b> : '.$pp_query->found_posts.'</p>'
    					// lien pour retour vers la page de recherche
    					  .'<p><a href="'.$pp_url_home.'/espace-technique/codes-porsche/codes-peinture/">Retour sur la page de recherche</a></p>';
    
    	if ($pp_query->have_posts()) {
    
    		while ($pp_query->have_posts()) : $pp_query->the_post();
    			$pp_post_ID = get_the_ID();
    			$pp_couleur_ = get_post_meta($pp_post_ID);
    			//var_dump($pp_couleur_);
    			$pp_couleur_code = $pp_couleur_['pp_color-code'][0];
    			$pp_couleur_code_description_courte = $pp_couleur_['pp_color-description_courte'][0];
    			$pp_couleur_code_description_courte_en = $pp_couleur_['pp_color-description_courte_en'][0];
    			$pp_couleur_code_description_courte_de = $pp_couleur_['pp_color-description_courte_de'][0];
    			$pp_couleur_nom_fichier_couleur = $pp_couleur_['pp_color-nom_fichier_couleur'][0];
    			$pp_couleur_nom_fichier_voiture = $pp_couleur_['pp_color-nom_fichier_voiture'][0];
    			$pp_couleur_notes = $pp_couleur_['pp_color-notes'][0];
    
    			$pp_couleur_url = $pp_home_url."/wp-content/uploads/colors/".$pp_couleur_nom_fichier_couleur;
    			$pp_couleur_car_url = $pp_home_url."/wp-content/uploads/colors/".$pp_couleur_nom_fichier_voiture;
    
    			$pp_result .= '<table border="1" width="100%">'
    							.'<tr>'
    								.'<td rowspan="4" bgcolor="#646464" style="color:#E0DEDF;text-align:center" width="6%" >'.$pp_couleur_code.'</td>'
    								// FR
    								.'<td width="6%"> <img src="'.$pp_home_url.'/wp-content/uploads/drapeaux/fr.png"/></td>'
    								.'<td width="22%">'.$pp_couleur_code_description_courte.'</td>';
    			if (URLIsValid($pp_couleur_url)){
    				$pp_result .= '<td rowspan="3" width="23%"><img src="'.$pp_couleur_url.'" border="1"/></td>';
    			}
    			else
    			{
    				$pp_result .= '<td rowspan="3" width="23%">&nbsp;</td>';
    			}
    			if (URLIsValid($pp_couleur_car_url)){
    				$pp_result .= '<td rowspan="3" width="43%"><img src="'.$pp_couleur_car_url.'"/></td>';
    			}
    			else
    			{
    				$pp_result .= '<td rowspan="3" width="43%">&nbsp;</td>';
    			}
    			$pp_result .= 	'</tr>'
    							// EN
    							.'<tr>'
    								.'<td width="6%"> <img src="'.$pp_home_url.'/wp-content/uploads/drapeaux/gb.png"/></td>'
    								.'<td width="22%">'.$pp_couleur_code_description_courte_en.'</td>'
    							.'</tr>'
    							// DE
    							.'<tr>'
    								.'<td width="6%"> <img src="'.$pp_home_url.'/wp-content/uploads/drapeaux/de.png"/></td>'
    								.'<td width="22%">'.$pp_couleur_code_description_courte_de.'</td>'
    							.'</tr>'
    							// Modèles concernés
    							.'<tr>'
    								.'<td colspan="4" style="padding:8px">'
    									.'[wpspoiler name="Modèles concernés..." open="false"]'
    									 // $pp_modeles_concernes = get_field('modeles_concernes',$pp_post_ID);
    									 // var_dump( $pp_modeles_concernes );
    									 // $pp_result .= '<ul>';
        								 // foreach( $pp_modeles_concernes as $pp_modele_concerne) {
            							// 	$pp_cat_name = get_cat_name($pp_modele_concerne);
            							// 	$pp_result .= '<li>'.$pp_cat_name.'</li>';
    									// }
        								 // $pp_result .= '</ul>' */
        								// temporaire en attendant d associer les modèles aux couleurs
        								.'<p>'.$pp_couleur_notes.'</p>'
        								.'[/wpspoiler]'
    								.'</td>'
    							.'</tr>'
    						.'</table>'
    						.'<br/>';
    		endwhile;
    		$pp_result .= '<p><a href="'.$pp_url_home.'/espace-technique/codes-porsche/codes-peinture/">Retour sur la page de recherche</a></p>';
    	}
    	else
    	{
    		$pp_result = "<h3>Aucun résultat n'a été trouvé avec les critères spécifiés...</h3>"
    		  			.'<a href="'.$pp_url_home.'/espace-technique/codes-porsche/codes-peinture/">Retour sur la page de recherche</a>';
    
    	}
    return ($pp_result);
    }

    Thread Starter olive67

    (@olive67)

    Hello,
    I can also see these errors in the DB logs :

    140721 20:50:08 InnoDB: ERROR: the age of the last checkpoint is 15107185,
    InnoDB: which exceeds the log group capacity 15095808.
    InnoDB: If you are using big BLOB or TEXT rows, you must set the
    InnoDB: combined size of log files at least 10 times bigger than the
    InnoDB: largest such row.

    Any idea if this can be linked to the queries ? Maybe some parameters of the MySQL DB to modify ?

    Thread Starter olive67

    (@olive67)

    Hello,
    I checked again my code and the biggest performance issue was due to the multiple calls to the function URLIsValid.
    I removed that in my code and performance is now perfect 🙂

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘WP_Query performance issues’ is closed to new replies.