CountRecords ( ForeignTable::Field )

Idea created by Chris Irvine on Mar 1, 2016

    Provide a direct and optimized function to count the number of related records. This function should make code easy to read, fast to run, and avoid crazy hacks.


    The Problem


    It is very common that a developer needs to count the number of foreign records in a related TO (table occurrence). The most obvious way to do this would be to Count ( ForeignTable::UniqueID ). However, because Count() is a text processing function, the client has to (part 1) query the server for related records and then (part 2) download those records to process their contents.


    There isn't anything wrong with the Count() function, it's just overkill when you only need the answer from part 1.


    Various Hacks


    A well known work around to this performance problem is the creation of an extra un-stored =Get ( FoundCount ) field in every table of interest. This works pretty well except that this schema bloat could be completely eliminated.


    Another option would be to use the server optimized ExecuteSQL ( "SELECT COUNT(*) ... ). This also works ok, but is way more complicated then we would like, requiring you to long-hand all the relationship edges.


    The Solution


    It's simple. CountRecords() should query through one or more related TOs returning the number of records that match the conditions defined on the graph. It should work just fine on a self-join (another TO referencing the same table). If someone accidentally references their current context as the target, then it would be appropriate to return the found count (akin to GTRR[current_TO; new layout] -> get ( found count ) )


    It shouldn't matter which field you pick from the foreign table. The parameter needs to be a real reference, not a text argument.