Improve performance of applications index.

Some tweaks to avoid unnecessary db queries when building the index. This reduces the number of queries per index row from around 20 to 4.  Some of the 20 queries were hitting the query cache, but that still slows things down. On my dev VM, this improved the rendering time from around 1550 ms to around 600ms.

I'm sure there's still room for improvement, but this is a start.