scripts: Add scripts to defragment MySQL tables

scripts/defragtables.py:
- If run without argument, all tables EXCEPT stats are defragmented
  (quick run)
- If run with argument e.g. "1", all table INCLUDING stats are
  defragmented (will take about one hour)

scripts/defragtable.py <space-separated list of tables>:
- Defragments the specified tables; will crash if table does not
  exist

Signed-off-by: Adrian Schmutzler <freifunk@adrianschmutzler.de>
This commit is contained in:
Adrian Schmutzler 2018-04-12 22:13:06 +02:00
parent fc9a494078
commit 7955e60f2f
3 changed files with 75 additions and 0 deletions

View File

@ -48,3 +48,32 @@ def neighbor_color(quality,rt_protocol):
elif quality < 230:
color = "#79ff7c"
return color
def defrag_table(mysql,table,sleep):
minustime=0
allrows=0
start_time = time.time()
qry = "ALTER TABLE `%s` ENGINE = InnoDB" % (table)
mysql.execute(qry)
mysql.commit()
end_time = time.time()
if sleep > 0:
time.sleep(sleep)
writelog(CONFIG["debug_dir"] + "/deletetime.txt", "Defragmented table %s: %.3f seconds" % (table,end_time - start_time))
print("--- Defragmented table %s: %.3f seconds ---" % (table,end_time - start_time))
def defrag_all(mysql,doall=False):
alltables = ('gw','gw_admin','gw_netif','hoods','netifs','router','router_events','router_gw','router_ipv6','router_neighbor','router_netif','users')
stattables = ('router_stats','router_stats_gw','router_stats_neighbor','router_stats_netif','stats_global','stats_gw','stats_hood')
for t in alltables:
defrag_table(mysql,t,1)
if doall:
for t in stattables:
defrag_table(mysql,t,60)
writelog(CONFIG["debug_dir"] + "/deletetime.txt", "-------")

24
scripts/defragtable.py Executable file
View File

@ -0,0 +1,24 @@
#!/usr/bin/python3
# Execute manually
import os
import sys
sys.path.insert(0, os.path.abspath(os.path.dirname(__file__) + '/' + '..'))
from ffmap.misc import defrag_table, writelog
from ffmap.config import CONFIG
from ffmap.mysqltools import FreifunkMySQL
import time
start_time = time.time()
mysql = FreifunkMySQL()
i = 1
while i < len(sys.argv):
defrag_table(mysql,sys.argv[i],1)
i = i + 1
mysql.close()
writelog(CONFIG["debug_dir"] + "/deletetime.txt", "-------")
print("--- Total defrag duration: %.3f seconds ---" % (time.time() - start_time))

22
scripts/defragtables.py Executable file
View File

@ -0,0 +1,22 @@
#!/usr/bin/python3
# Execute manually
import os
import sys
sys.path.insert(0, os.path.abspath(os.path.dirname(__file__) + '/' + '..'))
from ffmap.misc import defrag_all
from ffmap.mysqltools import FreifunkMySQL
import time
start_time = time.time()
mysql = FreifunkMySQL()
if(len(sys.argv)>1):
defrag_all(mysql,sys.argv[1])
else:
defrag_all(mysql,False)
mysql.close()
print("--- Total defrag duration: %.3f seconds ---" % (time.time() - start_time))