200 joins is actually very common if you fall into the EAV trap. If you have one entity with 200 columns, there's 200 joins for you!
Of course, SQL Server has no problem with 200 joins, but quite possibly it's miscalculating the amount of memory needed. This is especially likely for hash joins
, which trade memory for better performance. So a first step would be to replace all joins with loop joins, for example inner loop join
. A loop join requires very little memory.
If that doesn't work out, look at the execution plan. The real plan will probably not make it past a memory error, but you can see the estimated execution plan:
SET SHOWPLAN_ALL ON
From the documentation:
When SET SHOWPLAN_ALL is ON, SQL
Server returns execution information
for each statement without executing
it, and Transact-SQL statements are
not executed
This could give a clue about what SQL is planning to do.