Query Optimization

Tennek
Member Posts: 11
hi all,
When I run the below query in the query analyzer and show the execution plan it is as follows:
select <- sort <- clus. index Seek.
Why is it using the cl. index and doing resorting when there is an index with these columns ??:
Status, Ending Date, Ending Time, Prod_ Order No_, Routing Reference No_, Routing No_, Operation No
br Kenneth
When I run the below query in the query analyzer and show the execution plan it is as follows:
select <- sort <- clus. index Seek.
SELECT * FROM "testdb$Prod_ Order Routing Line" WITH (READUNCOMMITTED) WHERE (("Status"=3)) and "Ending Date"<'2010-06-01 00:00:00' ORDER BY "Ending Date" DESC,"Ending Time" DESC,"Status" DESC,"Prod_ Order No_" DESC,"Routing Reference No_" DESC,"Routing No_" DESC,"Operation No_" DESC
Why is it using the cl. index and doing resorting when there is an index with these columns ??:
Status, Ending Date, Ending Time, Prod_ Order No_, Routing Reference No_, Routing No_, Operation No
br Kenneth
0
Comments
-
Well, there could be several reasons ...
Which NAV version do you use? Are you maintaining "statistics"?
It could be that this operation is a "Dynamic Cursor" operation - this might cause the SQL Server to pick an index which is mostly matching the ORDER BY clause, instead of the WHERE ...
The it depends on the "Selectivity" of the "Status" field ...
Last but not least, this is obviously some FIND('+') command in NAV. Could this be a FINDLAST instead? FINDLAST will not create a cursor, thus the query would be fully optimized on the WHERE. Further, as the result-"set" is just one record instead of "n" this would reduce the workload.
But the overall question should be: is the current execution a problem? Too many "Reads"? Long "CPU" or "Duration"?Jörg A. Stryk (MVP - Dynamics NAV)
NAV/SQL Performance Optimization & Troubleshooting
STRYK System Improvement
The Blog - The Book - The Tool0 -
Which NAV version do you use? Are you maintaining "statistics"?It could be that this operation is a "Dynamic Cursor" operation - this might cause the SQL Server to pick an index which is mostly matching the ORDER BY clause, instead of the WHERE ...
The it depends on the "Selectivity" of the "Status" field ...
Status, Ending Date, Ending Time, Prod_ Order No_, Routing Reference No_, Routing No_, Operation No
Could you please explain how the "Selectivity" of the "Status" field has to do with the index selection?
and is there some way of approving this if it's a "Dynamic Cursor" operation ?Last but not least, this is obviously some FIND('+') command in NAV.
and the main reason for asking is the number of reads. about 20000, 300-400 times a day according to profiling.
thank you..0 -
Tennek wrote:Could you please explain how the "Selectivity" of the "Status" field has to do with the index selection?
Just like the Open field in Entry tables. SQL rarely picks these indexes as it only reduces the dataset by 50%.
But the main problem is the awfull dynamic cursors from NAV6 (and NAV5 from some hotfix).
This makes SQL pick the index based on the ORDER BY instead of the WHERE clause.Tennek wrote:How do you know this comes from find(+) ?Regards
Peter0 -
Exactly.
But just for curiosity:
If you create theis index in SSMS ...USE "testdb" GO CREATE INDEX test01 ON "testdb$Prod_ Order Routing Line" ("Status", "Ending Date")
... does this help? (If not just DROP the index)Jörg A. Stryk (MVP - Dynamics NAV)
NAV/SQL Performance Optimization & Troubleshooting
STRYK System Improvement
The Blog - The Book - The Tool0 -
does this help? (If not just DROP the index)
That was also my first thought but i did not help. Still using the clustered index.
Based on both previous replies I added "Top 1" to the query (findlast). Then it is using the Non-clustered index !?!
Ending Date, Ending Time, Status, Prod_ Order No_, Routing Reference No_, Routing No_, Operation No
But anyway I think I got the picture.
Thanks a lot for the explanations. :thumbsup:
Br
Kenneth0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions