Msg 8623:The query processor ran out of internal resources and could not produce a query plan

StackOverflow https://stackoverflow.com/questions/20397137

  •  29-08-2022
  •  | 
  •  

Question

I have a very long query which uses distinct intersect, when the query grows too long and I execute it it throws above exception

 SELECT DISTINCT RTypeId FROM dbo.User_Res WHERE UserId = '1749' 
 INTERSECT SELECT DISTINCT RTypeId FROM dbo.User_Res WHERE UserId = '424' 
 INTERSECT SELECT DISTINCT RTypeId FROM dbo.User_Res WHERE UserId = '1906' 
 INTERSECT SELECT DISTINCT RTypeId FROM dbo.User_Res WHERE UserId = '725' 
 INTERSECT SELECT DISTINCT RTypeId FROM dbo.User_Res WHERE UserId = '1596'

Can you please help me with an alternative to this query?

Error msg:

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.

Était-ce utile?

La solution

So essentially what you want is to get RTypeIds' that are common to all the users?

You could build the same query in this way:

with userrt (rid, uid)
as (select distinct rtypeid, userid from User_Res where UserId in (1749, 424, 1906 ...)
select rid, count(uid) as cuid
from userrt
group by rid

Now only those result rows that have cuid == amount of userid's in limiting clause are the ones you're interested in (since they've been there for all users and are therefore common to all).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top