MySQL slow queries that could benefit from an index
-
I am helping someone with website performance, and noticed a couple slow queries that I believe came from this plugin (version 6.7.9). The first slow query is ran on the
ahm_user_download_countstable and doesn’t use the primary key or another index.# Query_time: 0.056898 Lock_time: 0.000002 Rows_sent: 1 Rows_examined: 192218
select count(ID) from wp_ahm_user_download_counts where user = '<ip_omitted>' and package_id = '647';
# Query_time: 0.224904 Lock_time: 0.000001 Rows_sent: 0 Rows_examined: 192218
UPDATEwp_ahm_user_download_countsSETdownload_count= '4' WHEREuser= '<ip_omitted>' ANDpackage_id= '647';Adding this index reduces the rows_examined to 1.
mysql> alter table wp_ahm_user_download_counts add index package_id_user (package_id,user);
Query OK, 0 rows affected (0.91 sec)
Records: 0 Duplicates: 0 Warnings: 0The other queries relate to the ahm_sessions table:
# Query_time: 0.212994 Lock_time: 0.000001 Rows_sent: 0 Rows_examined: 172761
selectvaluefrom wp_ahm_sessions where deviceID = '<omitted>' andname= 'uniq_<omitted>' and expire > 1755525194;
# Query_time: 0.201077 Lock_time: 0.000001 Rows_sent: 0 Rows_examined: 172761
DELETE FROMwp_ahm_sessionsWHEREdeviceID= '<omitted>' ANDname= 'uniq_<omitted>';
# Query_time: 0.180426 Lock_time: 0.000001 Rows_sent: 0 Rows_examined: 172762
DELETE FROMwp_ahm_sessionsWHEREname= 'download.114<ip_omitted>';An index like this seems sufficient, at least on this site there don’t appear to be many deviceID’s per name.
mysql> alter table wp_ahm_sessions add index name_expire (name,expire);
Query OK, 0 rows affected (0.09 sec)
Records: 0 Duplicates: 0 Warnings: 0With this index, the rows_examined also drops to 1 for all 3 different queries.
Thanks for your attention on this, as I hope these small improvements can help all of your users. If you have further questions please let me know.
The topic ‘MySQL slow queries that could benefit from an index’ is closed to new replies.