This causes the number of MySQL queries to be: NumberOfTableA * (5 + NumberOfTableB * (4)). There can be around 4000 TableA's in the query, and around 5 TableB's for each TableB in the query, so there can be about 100,000 MySQL queries.

What's the best way to build the structure given above? My initial impression is that all I can do is implement a few joins to reduce the number of MySQL queries, changing to the following pseudo-code (where + indicates a join):

This would have the number of MySQL queries to be: NumberOfTableA * (1 + NumberOfTableB * 2), which would reduce the number of MySQL queries from 100,000 to 44,000.

My initial impression is that this is far as I can go with optimizing this situation. I realize I could create one massive join through one MySQL query, but that would create so much redundant information in columns that I can't see that being a good idea. Although the number of MySQL queries would be only one, the total size of the transmission from the server to the client would be much much much larger. Additionally, the c++ program would need to on each result figure out if it had moved into a different TableA or TableB result, which doesn't seem like good coding at all.