From c3068c892514080d8bb1b1029bfb2182b6eac79a Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Sat, 28 Nov 2015 13:19:09 +0100 Subject: [PATCH 1/3] Optimize tables on a daily base that are written very often --- include/cron.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/include/cron.php b/include/cron.php index 7b2244b55e..46d56f077e 100644 --- a/include/cron.php +++ b/include/cron.php @@ -189,6 +189,14 @@ function cron_run(&$argv, &$argc){ q('DELETE FROM `photo` WHERE `uid` = 0 AND `resource-id` LIKE "pic:%%" AND `created` < NOW() - INTERVAL %d SECOND', $cachetime); } + // Optimize some tables that are written often + q("OPTIMIZE TABLE `cache`"); + q("OPTIMIZE TABLE `session`"); + q("OPTIMIZE TABLE `config`"); + q("OPTIMIZE TABLE `pconfig`"); + q("OPTIMIZE TABLE `photo`"); + q("OPTIMIZE TABLE `workerqueue`"); + set_config('system','cache_last_cleared', time()); } From a34dbf76a421b8094e3ed956fd80584355c35180 Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Sat, 28 Nov 2015 14:35:59 +0100 Subject: [PATCH 2/3] Maybe it would take too long to optimize the photo table on larger systems --- include/cron.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/cron.php b/include/cron.php index 46d56f077e..7927769de8 100644 --- a/include/cron.php +++ b/include/cron.php @@ -194,8 +194,8 @@ function cron_run(&$argv, &$argc){ q("OPTIMIZE TABLE `session`"); q("OPTIMIZE TABLE `config`"); q("OPTIMIZE TABLE `pconfig`"); - q("OPTIMIZE TABLE `photo`"); q("OPTIMIZE TABLE `workerqueue`"); + //q("OPTIMIZE TABLE `photo`"); // Could take too long set_config('system','cache_last_cleared', time()); } From 3f66e0a9f6d0581112df2fb8b65eb64fddb23b5a Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Sat, 28 Nov 2015 15:43:25 +0100 Subject: [PATCH 3/3] Automatically optimize tables that are smaller than a definable size --- include/cron.php | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/include/cron.php b/include/cron.php index 7927769de8..d95d8bc601 100644 --- a/include/cron.php +++ b/include/cron.php @@ -189,13 +189,26 @@ function cron_run(&$argv, &$argc){ q('DELETE FROM `photo` WHERE `uid` = 0 AND `resource-id` LIKE "pic:%%" AND `created` < NOW() - INTERVAL %d SECOND', $cachetime); } - // Optimize some tables that are written often - q("OPTIMIZE TABLE `cache`"); - q("OPTIMIZE TABLE `session`"); - q("OPTIMIZE TABLE `config`"); - q("OPTIMIZE TABLE `pconfig`"); - q("OPTIMIZE TABLE `workerqueue`"); - //q("OPTIMIZE TABLE `photo`"); // Could take too long + // maximum table size in megabyte + $max_tablesize = intval(get_config('system','optimize_max_tablesize')) * 1000000; + if ($max_tablesize == 0) + $max_tablesize = 100 * 1000000; // Default are 100 MB + + // Optimize some tables that need to be optimized + $r = q("SHOW TABLE STATUS"); + foreach($r as $table) { + + // Don't optimize tables that needn't to be optimized + if ($table["Data_free"] == 0) + continue; + + // Don't optimize tables that are too large + if ($table["Data_length"] > $max_tablesize) + continue; + + // So optimize it + q("OPTIMIZE TABLE `%s`", dbesc($table["Name"])); + } set_config('system','cache_last_cleared', time()); }