add functionality to determine sql query cost.
don't display graphs-link if the cost is too high

- Currently this uses a cost of 500 as "too high"
  We do not know if this is actually a sane value.
fix resource chain for logics.
Use DISTINCT to optimize expensive sql-queries.