{"id":16416825,"date":"2023-01-28T11:21:01","date_gmt":"2023-01-28T11:21:01","guid":{"rendered":"https:\/\/wordpress.org\/support\/topic\/database-optimizations\/"},"modified":"2023-01-28T11:21:01","modified_gmt":"2023-01-28T11:21:01","slug":"database-optimizations","status":"publish","type":"topic","link":"https:\/\/wordpress.org\/support\/topic\/database-optimizations\/","title":{"rendered":"Database optimizations"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Hi,<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I noticed some problems with database queries on my server and I am writing how I fixed them. The blog I am managing has lots of posts.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">These queries were reported as slow by slow logger in MySQL.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">UPDATE <code>wp_swp_analytics<\/code> SET <code>post_id<\/code> = 0, <code>date<\/code> = &#8216;2023-01-27&#8217;, <code>facebook<\/code> = &#8216;10659&#8217;, <code>pinterest<\/code> = &#8216;1235&#8217;, <code>total_shares<\/code> = &#8216;11894&#8217; WHERE <code>post_id<\/code> = 0 AND <code>date<\/code> = &#8216;2023-01-27&#8217; &#8212; These can be removed from SET: <code>post_id<\/code> = 0, <code>date<\/code> = &#8216;2023-01-27&#8217;,<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">SELECT * FROM wp_3_swp_analytics WHERE post_id = 18980 &amp;&amp; date = &#8216;2023-01-27&#8217; &#8212; Word &#8220;AND&#8221; should be used instead of deprecated &#8220;&amp;&amp;&#8221;.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The slow queries where fixed by creating an index:<br \/>ALTER TABLE <code>wp_swp_analytics<\/code> CHANGE <code>date<\/code> <code>date<\/code> DATE NOT NULL DEFAULT &#8216;0001-01-01&#8217;; &#8212; 0000-00-00 is not a supported value<br \/>ALTER TABLE <code>wp_swp_analytics<\/code> ADD INDEX <code>post_id__date<\/code> (<code>post_id<\/code>, <code>date<\/code>); &#8212; Can be made date__post_id is both columns are always included in the WHERE filter in query<br \/>&#8212; ALTER TABLE <code>wp_swp_analytics<\/code> ADD UNIQUE <code>post_id__date<\/code> (<code>post_id<\/code>, <code>date<\/code>); &#8212; This did not work because multiple rows exists for certain post_id and date; I do not think this is OK. &#8220;INSERT INTO &#8230; ON DUPLICATE KEY UPDATE &#8230;&#8221; can be used here. If ID is generated, than it can be removed and date+post_id can be defined as a primary key.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Best regards,<br \/>D.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"template":"","class_list":["post-16416825","topic","type-topic","status-publish","hentry"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/16416825","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic"}],"about":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/types\/topic"}],"version-history":[{"count":0,"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/16416825\/revisions"}],"wp:attachment":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/media?parent=16416825"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}