Avatar of marrowyung
marrowyung
 asked on

SQL server Memory Grants and High Row Estimates

Dear all,

reading this :

http://www.brentozar.com/archive/2013/08/query-plans-what-happens-when-row-estimates-get-high/

she said :

"Here’s the general shape of our plan. Just note that the bars connecting operators are narrow– SQL Server has pretty low (and accurate) estimates of how much data will be flowing through these pipes:""

"The plan estimated cost is 1.1 and it estimates it’ll bring back 229 rows. In my test environment it takes 16 ms to run and does several thousand logical reads across the many tables it joins. This means the query optimizer didn’t consider every possible plan, but hey, our runtime was pretty darn good anyway."

how can she know "This means the query optimizer didn’t consider every possible plan, but hey, our runtime was pretty darn good anyway"  ?
Microsoft SQL ServerMicrosoft SQL Server 2008SQL

Avatar of undefined
Last Comment
marrowyung

8/22/2022 - Mon
lcohan

<<
how can she know "This means the query optimizer didn’t consider every possible plan, but hey, our runtime was pretty darn good anyway"  ?
>>

(my) answer to that would be (as well):

16ms (or milliseconds) runtime is pretty darn good no matter what plan and how many logical reads SQL choses - isn't it?

I think thats' what they refer to - I mean is fast regardless SQL consider or not each and any plan and choosing the best - maybe the fastes of all would be 12 (or lets say 9!!) milliseconds comparing to 16ms - would you "feel" that diference? I won't - and pleas don't consider percentage wise but AS IS in milliseconds.
marrowyung

ASKER
yeah, got it

"Now, this query is already a little troubled. It has a lot of joins, including outer joins. It shows an optimization timeout in the plan."

can you tell which operator told her the timeout problem ? which the optimizer give up the planning ?

"It loaded the the temp tables up in memory turn by turn and then had to go through a long, painful process of reading from them. It did this three times, in sequence."

why 3 times? I don't get it !

if I recall correctly, if we see spool operator we need to add index of it or increase memory for the SQL server ?

basically can't see why just add a function to the join will make the query START to do a lot of row by row operation but not the one without function, why ?

"TSQL anti-patterns."

what is that   ? no RBAR programming and use set-base operation ? anything else ?
marrowyung

ASKER
so in the link, the second one make the optimizer overestimated is because the join column has function (RTRIM()) on it? so it is not because of the RBAR problem, right?

so this RTRIM: Returns a character string after truncating all trailing blanks, can't apply to join column and just because of this ? not because the join column with function can't be index?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER CERTIFIED SOLUTION
lcohan

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
marrowyung

ASKER
"WHERE or JOINs for that matter can cause costly scans instead of index seeks and lot of details about that can be found at links below:"

so you don't do this and it is 100% sure that we can't include any function (any exception ? ) can't be use on ANY column in the join and where clause ?
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
marrowyung

ASKER
what I worrying is :

"This means the query optimizer didn’t consider every possible plan"

how can she make this conclude that because of that SQL sever don't consider every possible plan ! this is the real thing.

or here just demo the function on any of the join column can cause SQL server OVER estimated the number of ROW it process and also it will force RBAR operation, so do not use function on join and where column?

"Because SQL Server had to do a lot of heavy row-by-row comparisons and wasn’t sure exactly how many might feed out of each of them, it decided to build “spool” temporary tables behind the scenes. It loaded the the temp tables up in memory turn by turn and then had to go through a long, painful process of reading from them. It did this three times, in sequence"

how can she know there are 3 times in sequence ?
Vitor Montalvão

how can she know there are 3 times in sequence ?
Check her article again and in the figure just before she wrote those line you can see 3 nested loops. Is about that she was talking about.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
marrowyung

ASKER
I think you are talking about the 2 x picture above it, which above the table spool operator,  right?
Vitor Montalvão

Well, the picture exactly before the nested loops are covered by the Table Pool information so yes, to be exact is the picture even before that one :)
marrowyung

ASKER
tks all.'
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes