{"id":954,"date":"2018-05-08T19:06:16","date_gmt":"2018-05-08T23:06:16","guid":{"rendered":"https:\/\/www.devolve.net\/blog\/?p=954"},"modified":"2018-07-13T10:59:52","modified_gmt":"2018-07-13T14:59:52","slug":"finding-which-wordpress-requests-are-hardest-on-the-database","status":"publish","type":"post","link":"https:\/\/www.devolve.local\/finding-which-wordpress-requests-are-hardest-on-the-database\/","title":{"rendered":"Finding which WordPress requests are hardest on the database"},"content":{"rendered":"

Everyone says WordPress can handle north of a 100,000 or a million posts, no problem!<\/p>\n

BS<\/strong>.
\n<\/p>\n

Okay, if you have a dedicated database system with enough RAM to hold the entire dataset, then A)<\/strong> that’s cheating, since RDBMSs were created to handle the case where one doesn’t have the requisite memory<\/em>; and B)<\/strong> I don’t have that luxury right now. I routinely run into lots of performance issues despite a well-tuned LAMP system.<\/p>\n

mysql> select count(1) from wp_posts;\r\n+----------+\r\n| count(1) |\r\n+----------+\r\n|  1153099 |\r\n+----------+\r\n1 row in set (0.72 sec)\r\nmysql> select count(1) from wp_terms;\r\n+----------+\r\n| count(1) |\r\n+----------+\r\n|    65797 |\r\n+----------+\r\n1 row in set (0.04 sec)\r\n<\/pre>\n

Witness the awesome power of this addition to your functions.php<\/code> file, inspired by this SO<\/a> post:<\/p>\n

add_action('shutdown', 'my_sql_logger');\r\nfunction my_sql_logger() {\r\n  if (defined('SAVEQUERIES')) {\r\n    global $wpdb;\r\n    $log_file = fopen(ABSPATH.'\/sql_log.txt', 'a');\r\n    fwrite($log_file, \"\\n\/\/\/\/\/\/\/\/\/\/\\n\" . date(\"F j, Y, g:i:s a\\n\"));\r\n    fwrite($log_file, print_r($_SERVER['REQUEST_URI'], TRUE) . \"\\n\");\r\n    foreach($wpdb->queries as $q) {\r\n      if ((int)$q[1] >= 1) {\r\n        fwrite($log_file, $q[0] . \" - ($q[1] s)\" . \"\\n\");\r\n      }\r\n    }\r\n    fclose($log_file);\r\n  }\r\n}\r\n<\/pre>\n

Don’t forget to turn off the SAVEQUERIES<\/code> define in wp-config.php, or else your disk may fill up rather quickly.<\/p>\n","protected":false},"excerpt":{"rendered":"

Everyone says WordPress can handle north of a 100,000 or a million posts, no problem! BS.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[35,25,31,49],"_links":{"self":[{"href":"https:\/\/www.devolve.local\/wp-json\/wp\/v2\/posts\/954"}],"collection":[{"href":"https:\/\/www.devolve.local\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devolve.local\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devolve.local\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devolve.local\/wp-json\/wp\/v2\/comments?post=954"}],"version-history":[{"count":4,"href":"https:\/\/www.devolve.local\/wp-json\/wp\/v2\/posts\/954\/revisions"}],"predecessor-version":[{"id":958,"href":"https:\/\/www.devolve.local\/wp-json\/wp\/v2\/posts\/954\/revisions\/958"}],"wp:attachment":[{"href":"https:\/\/www.devolve.local\/wp-json\/wp\/v2\/media?parent=954"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devolve.local\/wp-json\/wp\/v2\/categories?post=954"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devolve.local\/wp-json\/wp\/v2\/tags?post=954"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}