简体   繁体   中英

MySQL query uses too much CPU

I use the following query to find users that I need to send daily reminders based on their settings and timezone. It works but turned up that it uses about 50% CPU and its really heavy even when I add Limit 0,100.

(It even causes phpMyAdmin to crash or something)

Users table: 3000 records, Posts table: 12000+ records, Settings table: 3000 records, Reminders table: 80000 records (Keeps user_id and date to prevent duplicates)

   SELECT u.`id`, u.`fullname`, u.`email`, u.`hash`, s.`timezone`
   FROM `users` u
   LEFT JOIN `reminders` rm ON rm.`user_id` = u.`id` AND rm.`date` = CURDATE()
   LEFT JOIN `settings` s ON s.`user_id` = u.`id`
   LEFT JOIN `posts` p ON p.`user_id` = u.`id` AND p.`date` = DATE(CONVERT_TZ(UTC_TIMESTAMP, 'UTC', s.`timezone`))
   WHERE HOUR(CONVERT_TZ(UTC_TIMESTAMP, 'UTC', s.`timezone`)) = s.`notify_hour`
   AND s.`notify` = 1 AND u.`active` = 1 AND rm.`id` IS NULL AND p.`id` IS NULL
   GROUP BY u.`id` LIMIT 0,100

I run this query every 10 minutes and I'm sending reminders through sendgrid.com SMTP server. Can you please help me optimize this query so that it doesn't use this much resource?

Thank you (and sorry for my English)

Do you have the fields properly indexed? here is a suggestion:

Try to index the user_id field on all the tables it should make it faster.

Also the conversion of dates is eating up the cpu time, you should store the dates in UTC format in your database this way you will avoid a huge overhead

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM