Title: Slow DB Query
Last modified: July 25, 2022

---

# Slow DB Query

 *  [edat](https://wordpress.org/support/users/edat/)
 * (@edat)
 * [3 years, 9 months ago](https://wordpress.org/support/topic/slow-db-query-2/)
 * Hi Team
 * Every time I open open a product to edit in the back end, the YMM plugin runs
   the following SQL Statement and it take around 7-9 seconds to run and returns
   the same values for every product I edit.
 *     ```
       SELECT DISTINCT IF(LENGTH(postmeta.meta_value)>0, postmeta.meta_value, postmeta.post_id) as product_sku, ymm.make, ymm.model, ymm.year_from, ymm.year_to
       FROM wp_posts AS posts JOIN wp_postmeta AS postmeta
       ON postmeta.post_id = posts.ID
       AND postmeta.meta_key = '_sku' JOIN wp_ymm ymm
       ON ymm.product_id = posts.ID
       WHERE posts.post_type = 'product'
       ORDER BY posts.ID
       LIMIT 1
       ```
   
 *     ```
       product_sku    make                model                  year_from    year_to
        DP900          HARLEY DAVIDSON     1340 FLHR Road King    1994         1994
       ```
   
 * The Caller of SQL is Pektsekye_Ymm_Model_Db->getVehicleData()
 * Can anyone tell me why this SQL is executed and returns the same value every 
   time no matter what product I’m editing. I’m wondering if there is some type 
   of error, it just seems to take way to long to open a product to edit.
    -  This topic was modified 3 years, 9 months ago by [edat](https://wordpress.org/support/users/edat/).
    -  This topic was modified 3 years, 9 months ago by [edat](https://wordpress.org/support/users/edat/).

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

 *  Thread Starter [edat](https://wordpress.org/support/users/edat/)
 * (@edat)
 * [3 years, 9 months ago](https://wordpress.org/support/topic/slow-db-query-2/#post-15855873)
 * Never mind my question about the same product being selected, that was my mistake,
   but it is taking upwards of 10 seconds to get a result.
 * My wp_ymm table has about 296,000 rows.
 *  Plugin Author [Pektsekye](https://wordpress.org/support/users/pektsekye/)
 * (@pektsekye)
 * [3 years, 9 months ago](https://wordpress.org/support/topic/slow-db-query-2/#post-15856352)
 * Hello,
 * >it just seems to take way to long to open a product to edit.
 * Try to replace the function hasYmmData() with this:
 *     ```
         public function hasYmmData() 
         {
           return $this->_db->hasVehicleData();
         }
       ```
   
 * in the files:
    wp-content/plugins/ymm-search/Block/Adminhtml/Product/Edit/Restriction.
   php wp-content/plugins/ymm-search/Block/Adminhtml/Ymm/Selector.php
 * Then add a new function:
 *     ```
       public function hasVehicleData()     
           {
             $value = $this->_wpdb->get_var("SELECT EXISTS (SELECT 1 FROM {$this->_mainTable})");
             return $value == 1;         
           }
       ```
   
 * to the file:
    wp-content/plugins/ymm-search/Model/Db.php
 * Stanislav
 *  Thread Starter [edat](https://wordpress.org/support/users/edat/)
 * (@edat)
 * [3 years, 9 months ago](https://wordpress.org/support/topic/slow-db-query-2/#post-15856576)
 * Thank you, that have speed up the process.

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

The topic ‘Slow DB Query’ is closed to new replies.

 * ![](https://ps.w.org/ymm-search/assets/icon-256x256.png?rev=1750193)
 * [Year Make Model Search for WooCommerce](https://wordpress.org/plugins/ymm-search/)
 * [Support Threads](https://wordpress.org/support/plugin/ymm-search/)
 * [Active Topics](https://wordpress.org/support/plugin/ymm-search/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/ymm-search/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/ymm-search/reviews/)

 * 3 replies
 * 2 participants
 * Last reply from: [edat](https://wordpress.org/support/users/edat/)
 * Last activity: [3 years, 9 months ago](https://wordpress.org/support/topic/slow-db-query-2/#post-15856576)
 * Status: not resolved