Is Index Hinting available in NAV 2016? I don't find anything in the MSDN documentation on it. If not, is there a recommended alternate solution to controlling index selection in queries?
I have a situation where certain filter values are resulting in non-optimal query plans. The particular table (Item) has about 35,000 records. The filter is applied to a field (Item Category Code) that has a secondary index. If value "A" is applied (which returns about 50 records) the plan uses the expected index. However if value "B" is used (which returns about 5000 records) the plan does a clustered index scan. This is clearly an issue with the "B" filter not being selective enough.
I have tested the query in SQL using an index hint. It will use the expected index regardless of the filter value.
Any ideas or suggestions?
There are no bugs - only undocumented features.
0
Comments
No PM,please use the forum. || May the <SOLVED>-attribute be in your title!
In most cases rebuilding indexes improves performace because statistics also got updated during index rebuild operation
Dynamics NAV, MS SQL Server, Wherescape RED;
PRINCE2 Practitioner - License GR657010572SG
GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
Rebuilding the index was the first thing I tried. This is a testing environment, so the indexes are not being rebuilt regularly. But also the data is not changing frequently enough to require it. I will periodically run a reorg/rebuild/stats script.
My thinking is it's those updated statistics that are causing SQL to not use this index. If you filter on a value that returns a low percentage of records, it will use this index just fine. Only when you filter on a value returning a higher percentage of records does the problem show.
The index hint results in a very noticeable performance improvement.
Unfortunately I have not seen anyting about index hinting in 2016. But.. Perhaps instead of forcing index hint (which only works in 2009) you could fool the SQL server and fake the statistics to make clustered index scan much less 'attractive' option ?
Here is very interesting post from Brent Ozar Blog about messing with statistics - namely fooling the SQL Server into thinking that the table (or index) have much more rows than it actually have. The use it in query tuning to force SQL server to generate query plans just as if the table was massive - without actually creating a massive table.
My though is that you could try to manually update statistics on the table and force increase of record count and page count on clustered index statistic, so in theory query optimiser should select the desired index + clustered index lookup as less costly operation..
It's just an idea - never tried this myself.
Another, simpler way of dealing with it could be to cluster the table along different, non PK index.
Regards,
Slawek
Dynamics NAV, MS SQL Server, Wherescape RED;
PRINCE2 Practitioner - License GR657010572SG
GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
It appears that Index Hinting does work with NAV 2016. I found the DBConfig table does exist, so I gave it a try. It does give me the results I was looking for.
My feeling is changing the clustered index would just move the problem elsewhere.
I think I'm going to stick with the index hint. It provides the results needed, is easy to implement, and is somewhat supported by Microsoft.
Thanks for the ideas.
Either way - nice to know it works. Some other guy here blogged that index hinting was removed in 2013...
Dynamics NAV, MS SQL Server, Wherescape RED;
PRINCE2 Practitioner - License GR657010572SG
GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
However when I try with NAV 2016 and 2017, I don't see the results in the SQL application profiler or SQL extended events.
Brian, did you check the sql commands were containing the 'index hint' ? :
...WITH (READUNCOMMITTED, INDEX("$3"))....