Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Limit to a UNION?
Message
De
17/06/2005 13:48:35
 
 
À
17/06/2005 08:00:39
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Autre
Divers
Thread ID:
01024245
Message ID:
01024403
Vues:
19
>Hi all
>
>I'm executing a huge SQL string from c# and i seem to be hitting some sort of limit, the SQL consists of 1000's of UNION selects and I get the following error:
>
>Internal Query Processor Error: The query processor ran out of stack space during query optimization
>
>Is there a limit to the size of the SQL you can execute?
>
>Thanks
>Kev

Apply "dividi et impera" to the unions,
this reduce the Max(stackQuerySize):
SELECT * FROM
(SELECT ...
  UNION ... SELECT  
  UNION ... SELECT
 ) T1
UNION ...
SELECT * FROM
(SELECT * FROM
  UNION ... SELECT  
  UNION ... SELECT
 ) T1
UNION ...
SELECT * FROM
(SELECT * FROM
  UNION ... SELECT  
  UNION ... SELECT
 ) T1
UNION ...
SELECT * FROM
(SELECT * FROM
  UNION ... SELECT  
  UNION ... SELECT
 ) T1
try the query engine into Query Analyzer: CTRL+L
( if you see the query Plan, then the stack is sufficient )

If you want a more faster/efficient plannning,
increase the nesting subquery
( remember the subquery's 256 tables limit,
every union nesting add 2 temporary tables,
then with N levels you lose 2^N tables )
SELECT * FROM
(SELECT * FROM
    (       SELECT ...
  UNION ... SELECT  ) T2
  UNION ... SELECT * FROM
    (SELECT ....
    UNION ... SELECT
     ....           ) T2
   ) T1
UNION ... 
SELECT * FROM
(SELECT * FROM
    (       SELECT ...
  UNION ... SELECT  ) T2
  UNION ... SELECT * FROM
    (SELECT ....
    UNION ... SELECT
     ....           ) T2
   ) T1
....
But with this query type, it is easy go into a 701 error: memory insufficient!
Use a temp/var table, or change the query into a join type query.
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform