Whenever you open a layout (or maintenance runs) the CMS will remove expired files from the Layout to keep things tidy.
The query being run is attempting to get a list of those potentially expired files.
Can we get a count of records from the tables involved?
SELECT COUNT(*) FROM `media`
SELECT COUNT(*) FROM `media` WHERE IFNULL(media.expires, 0) <> 0
SELECT COUNT(*) FROM `tag`
SELECT COUNT(*) FROM `lkmediagroup`