Database optimizations
-
Hi,
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.
These queries were reported as slow by slow logger in MySQL.
UPDATE
wp_swp_analytics
SETpost_id
= 0,date
= ‘2023-01-27’,facebook
= ‘10659’,pinterest
= ‘1235’,total_shares
= ‘11894’ WHEREpost_id
= 0 ANDdate
= ‘2023-01-27’ — These can be removed from SET:post_id
= 0,date
= ‘2023-01-27’,SELECT * FROM wp_3_swp_analytics WHERE post_id = 18980 && date = ‘2023-01-27’ — Word “AND” should be used instead of deprecated “&&”.
The slow queries where fixed by creating an index:
ALTER TABLEwp_swp_analytics
CHANGEdate
date
DATE NOT NULL DEFAULT ‘0001-01-01’; — 0000-00-00 is not a supported value
ALTER TABLEwp_swp_analytics
ADD INDEXpost_id__date
(post_id
,date
); — Can be made date__post_id is both columns are always included in the WHERE filter in query
— ALTER TABLEwp_swp_analytics
ADD UNIQUEpost_id__date
(post_id
,date
); — This did not work because multiple rows exists for certain post_id and date; I do not think this is OK. “INSERT INTO … ON DUPLICATE KEY UPDATE …” can be used here. If ID is generated, than it can be removed and date+post_id can be defined as a primary key.Best regards,
D.
- The topic ‘Database optimizations’ is closed to new replies.