Advice on optimising php/db code in moodle2+