Massive data optimization query SQL