I have also found fs_order_files to be very slow and I only have about 350 files. I did a quick fix by only updating rows where the sort key or depth actually change. Time went from 11 seconds to 1 for the test cases I ran.
ns_log warning "entering fs_order at [ns_time]"
set ordering_query "select file_id, fs_level_gen(file_id,1) as the_level, s
ort_key as old_sort_key, depth as old_depth
from fs_files
order by fs_connect_by(file_id), file_title"
set order_depth_list [database_to_tcl_list_list $db $ordering_query]
set sort_key 1
set changed_count 0
foreach id_depth $order_depth_list {
set id [lindex $id_depth 0]
set depth [expr [lindex $id_depth 1]-1]
set old_sort_key [lindex $id_depth 2]
set old_depth [lindex $id_depth 3]
if {$old_sort_key != $sort_key || $old_depth != $depth} {
incr changed_count
ns_db dml $db "update fs_files set sort_key=$sort_key, depth=$depth
where file_id=$id"
}
incr sort_key
}
ns_log warning "Done with fs_order at [ns_time] changed $changed_count files out of a total of $sort_key"