Optimizing search with multiple linked rows

With accelerated tables on, data is fetched at the time of the query. With it off, the client would fetch the linked data on request.

The number of columns in the tables isn’t enough to cause issues, and I don’t see anything in your code that seems like it’s not right.

Using q.fetch_only like you are might be as good as you’re going to get without denormalizing your database structure.

Edit: unless you can use q.fetch_only to tell it not to fetch the location column for the customer link inside the location row. The circular reference might be taking extra time?