Without rewriting this query, is there a way to force the query optimizer to better estimate the amount of resources required, e.g. with query hints?

dba.stackexchange https://dba.stackexchange.com/questions/251494

Question

Execution Plan with bad row estimate

I know there's a couple of alternate ways to write this query, but I'm more curious if there's another approach that I could use to improve the performance by helping guide the optimizer in the right direction? For example, can I use any kind of query hints / options to improve performance here?

Currently it runs in about 40 minutes and returns about 30,000 records.

Note: I'm also interested if there's new features in 2019 that apply here that don't apply in 2008 R2 (I know one big difference between the two is the Cardinality Estimator).

Était-ce utile?

La solution

This query, as written, must compare every pair of rows, and the cost estimate is reasonably accurate.

On a supported version of SQL Server you might get a parallel plan, or there is an undocumented query hint that will force a parallel plan.

select t1.itemid, t2.itemid
from #temp T1  
inner join #temp as t2
  on t1.itemid like '%' + t2.itemid + '%'
  and t2.itemid <> t1.itemid
OPTION(USE HINT('ENABLE_PARALLEL_PLAN_PREFERENCE'))
Licencié sous: CC-BY-SA avec attribution
Non affilié à dba.stackexchange
scroll top