Query wont compile/run

Since you are running Microsoft SQL Server 2014 - 12.0.2000, the very first RTM build including the new Cardinality Estimator I would strongly suggest you try updating to one of the latest CU's.

As stated in this blog post on msdn

You need to apply SP1 but you must also enable trace flag 4199 in order to activate the fix.

SQL Server 2014 Service Pack 1 made various fixes on new Cardinality Estimator (new CE). The release notes also documents the fixes.

You already have TF 4199 active so the improvements should become active automatically.

I would strongly advise you to update and use the new Cardinality Estimator but with all the more recent fixes instead of disabling the new CE alltogheter.

If you still have issues with this single query you can add a plan guide that says OPTION (QUERYTRACEON 9481) for this query alone.


I found that your query had many redundancies in the conditions, and you used cross joins that were good candidates for simple joins. This might confuse the planner. Perhaps you could try the following rearrangement of the query (it is functionally exactly the same but uses joins and removes all the redundant comparisons) to see what the planner comes up with in both production and test?

DECLARE @P1 AS BIGINT = 5637144576
DECLARE @P2 AS NVARCHAR(4) = N'1003'
DECLARE @P3 AS INT = 212
DECLARE @P4 AS BIGINT = 5638885273
--DECLARE @P5 AS BIGINT = 5637144576
DECLARE @P6 AS INT = 865
--DECLARE @P7 AS BIGINT = 5637144576
--DECLARE @P8 AS BIGINT = 5637144576

SELECT t1.balance01, t1.recid, t2.amountcur, t2.dataareaid, t2.recid, t3.recid, t3.voucher, t3.accountnum, 
       t3.approved, t3.closed, t3.dataareaid, t4.party, t4.dataareaid, t4.recid 
FROM ( 
      SELECT     virt.id AS dataareaid , t4.accountnum, t4.party, t4.partition, t4.recid 
      FROM       custtable t4 
      INNER JOIN virtualdataarealist VIRT ON (t4.dataareaid = virt.virtualdataarea)
      WHERE t4.partition = @P1
      UNION ALL 
      SELECT     t4.dataareaid , t4.accountnum, t4.party, t4.partition, t4.recid 
      FROM       custtable t4 
      INNER JOIN dataarea DAT ON ( t4.dataareaid = dat.id AND dat.isvirtual = 0)
      WHERE t4.partition = @P1
     ) t4 
JOIN custtrans     t3 ON (t3.partition = t4.partition AND t3.dataareaid = t4.dataareaid AND t3.accountnum = t4.accountnum)
JOIN custtransopen t2 ON (t2.partition = t4.partition AND t2.dataareaid = t4.dataareaid AND t2.refrecid = t3.recid)
JOIN spectrans     t1 ON (t1.partition = t4.partition AND t1.refcompany = t4.dataareaid AND t1.refrecid = t2.recid)

WHERE --t1.partition=@P1 AND -- redundant (already set in t1 join t2)
      t1.speccompany=@P2 AND 
      t1.spectableid=@P3 AND 
      t1.specrecid=@P4 AND
      t1.reftableid=@P6 AND
      -- AND t2.partition=@P5 -- already defined in the subselect
      -- AND t3.partition=@P7 -- redundant (already set in t2 join t3)
      -- AND t4.partition=@P8 -- redundant (already set in t3 join t4)

We solved the issue.

The issue was caused by the new Cardinality Estimator in SQL 2014. We disabled the new estimator by activating trace flag 9481 and removing the query plan from the cache then the query worked again.