Skip to content

Instantly share code, notes, and snippets.

@karmiphuc
Created September 30, 2015 08:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karmiphuc/3a0559fdc102a4a25a7f to your computer and use it in GitHub Desktop.
Save karmiphuc/3a0559fdc102a4a25a7f to your computer and use it in GitHub Desktop.
Drop inactive tables (MyISAM engine) which haven't been accessed for 2 months
; Drop query
SET group_concat_max_len = 1024 * 1024 * 10;
SELECT CONCAT('DROP TABLE ',GROUP_CONCAT(CONCAT(table_schema,'.',table_name)),';') INTO @dropcmd FROM information_schema.tables
WHERE table_schema = 'test'
AND table_name LIKE '%temp\_data%'
AND DATE_ADD(update_time, INTERVAL +2 month) < CURDATE()
ORDER BY update_time DESC;
PREPARE str FROM @dropcmd; EXECUTE str; DEALLOCATE PREPARE str;
; Example Query
SELECT table_schema,
table_name,
update_time as LastAccessed
FROM information_schema.tables
WHERE table_schema = 'test'
AND table_name LIKE '%temp\_data%'
AND DATE_ADD(update_time, INTERVAL +2 month) < CURDATE()
ORDER BY update_time DESC;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment