3

What is wrong with this query that causes MySQL cpu usage to spike? The cpu usage is like this until the db runs out of memory.

$sql = "SELECT c.* 
    FROM   hatt_postmeta a 
           inner join hatt_postmeta b 
                   ON a .post_id = b .post_id 
           inner join hatt_postmeta c 
                   ON a .post_id = c .post_id 
    WHERE  ( a .meta_key = 'ht_size_width' 
             AND a .meta_value = 165 ) 
           AND ( b .meta_key = 'ht_size_ratio' 
                 AND b .meta_value = 70 ) 
           AND c .meta_key = 'ht_size_diameter' 
    GROUP  BY c .meta_value 
    ORDER  BY c .meta_value ASC";

$result= $wpdb->get_results( $sql );

cpu spike 100 percent

Update: every query is getting 100% cpu usage

100 percent for each process

Wreeecks
  • 2,186
  • 1
  • 33
  • 53
  • Take a look at https://stackoverflow.com/questions/1282232/mysql-high-cpu-usage/11293475#11293475 – PerroVerd Feb 03 '16 at 12:06
  • @PerroVerd thank for your reply, I've already sorted it out. It turns out that there's a lot of orphan records that needs to deleted. Orphan records can really make the query slow. – Wreeecks Feb 04 '16 at 03:27

1 Answers1

1

I've fixed this by deleting orphaned terms. Orphans can cause poor SQL performance, so you can install some plugin to clean it out for you. Below is a list of plugins that you can use. Hope this helps.

http://www.wpoptimus.com/611/wordpress-plugins-cleanup-optimize-database-improve-performance

Wreeecks
  • 2,186
  • 1
  • 33
  • 53