Forum Discussion

bboissard's avatar
bboissard
Copper Contributor
Oct 29, 2024

SQL Server failing to execute extremely complex queries

A query is failing with this error message on a customer environment:

The query processor ran out of internal resources and could not produce a query plan.
This is a rare event and only expected for extremely complex queries or queries that reference a very large number of tables or partitions.
Please simplify the query.
If you believe you have received this message in error, contact Customer Support Services for more information.

Sorry I cannot post the actual SQL query, which must be massive, but basically it is a union of a lot of tables.
Maybe around 600 tables, maybe several thousands, I don't know exactly how many.
For technical reasons, we cannot really simplify this query.

In the documentation:
https://learn.microsoft.com/en-us/sql/sql-server/maximum-capacity-specifications-for-sql-server?view=sql-server-ver16
It is specified that the maximum number of tables in a select is limited only by available resources.
Is it possible to increase the size of available internal resources somehow?

  • rodgerkong's avatar
    rodgerkong
    Iron Contributor
    Basicly, memory size limit the scale of query, the frenquacy and number of CPU cores limit the speed of query. As your description, it looks like memory size is bottleneck.
    • bboissard's avatar
      bboissard
      Copper Contributor

      rodgerkong 
      It makes sense, but is it the true cause of this error?
      I suggested the customer to increase the memory, using 'max server memory (MB)' and/or increasing the amount of RAM on the machine.

      Maybe we will do some tests configuring a small 'max server memory (MB)' then performing a query on a lot of tables to see if we can reproduce the customer error message.

      • bboissard's avatar
        bboissard
        Copper Contributor

        So I made some tests.
        I managed to reproduce the customer error message querying a bit more than 380 tables.
        Below that the query is successful.

        Then I reduced the 'max server memory' to see if I could reproduce the error even with less table if there is less memory available.
        But I faced a different error message:
        There is insufficient system memory in resource pool 'default' to run this query.

        So my conclusion is that the "internal resources" limit reached by the customer might not be linked to the amount of RAM available.

  • rodgerkong's avatar
    rodgerkong
    Iron Contributor

    hmm... so, the "internal resources" might be kinds of handles, threads...

    Could you rerun the test in a VM, and reduce the memory by decrease the memory allocated to the VM?

  • Mike_Lemay's avatar
    Mike_Lemay
    Copper Contributor

    If you can't fix this with hardware you will have to consider fixing the query.

    If it's a lot of unions maybe you could break the query into sections copying 10  (or so) unioned tables into temp tables, then union the staged tables in a final result set.

Resources