diff --git a/lib/datalib.php b/lib/datalib.php
index <HASH>..<HASH> 100644
--- a/lib/datalib.php
+++ b/lib/datalib.php
@@ -1237,8 +1237,12 @@ function fix_course_sortorder($categoryid=0, $n=0, $safe=0, $depth=0, $path='') 
         $path  = $path . '/' . $categoryid;
         $depth = $depth + 1;
 
-        set_field('course_categories', 'path',  addslashes($path),  'id', $categoryid);
-        set_field('course_categories', 'depth', $depth, 'id', $categoryid);
+        if ($cat->path !== $path) {
+            set_field('course_categories', 'path',  addslashes($path),  'id', $categoryid);
+        }
+        if ($cat->depth != $depth) {
+            set_field('course_categories', 'depth', $depth, 'id', $categoryid);
+        }
     }
 
     // get some basic info about courses in the category
datalib: fix_course_sortorder() - minor optimisation

Don't update fields unnecessarily. Cuts 3 DB queries per category
on course/index page (<I> in a <I> category setup).

fix_course_sortorder() should be fixed to avoid recursion.