ToC attempt, version 3
Performance improvement by optimizing SQL queries