I am trying to execute a humongous query with close to 200 inner joins on a database. It gives me the following error
Msg 701, Level 17, State 123 Line 1
I am running the database on a Dual core 2.7 GHz machine with 2GB of RAM.
Is there any way I can get this query to execute?
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 exampleinner 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:
From the documentation:
This could give a clue about what SQL is planning to do.