Wrong key selection in report

Belias
Member Posts: 2,998
Hi everyone, do you know why this chunk of code
SELECT * FROM "mydb"."dbo"."mycompany$Serial No_ Summary" WITH (READUNCOMMITTED) WHERE (("Patient Code"=@P1)) ORDER BY "Item No_","Last Source Code","Last Source Type","Code" OPTION (OPTIMIZE FOR UNKNOWN)
if i unmaintain key
Moreover, there are no sumindexes fields that make sql choose another key.
The sqlindex property has been set on any key.
The piece of code is in a RTC report; version NAV 2009R2 build 32275
SerialNoSummary.RESET; SerialNoSummary.SETCURRENTKEY("Patient Code","Property No.","Property Type"); SerialNoSummary.SETRANGE("Patient Code",ContactOtherRC."No.") IF SerialNoSummary.FINDSET THEN BEGIN REPEAT //do some stuff, No db reads/writes involed... UNTIL SerialNoSummary.NEXT = 0; END;generates this query?
SELECT * FROM "mydb"."dbo"."mycompany$Serial No_ Summary" WITH (READUNCOMMITTED) WHERE (("Patient Code"=@P1)) ORDER BY "Item No_","Last Source Code","Last Source Type","Code" OPTION (OPTIMIZE FOR UNKNOWN)
if i unmaintain key
in the table, the same code chooses the right key, which isItem No.,Last Source Code,Last Source Type,Code
which is obviously enabled and maintained.Patient Code,Property No.,Property Type
Moreover, there are no sumindexes fields that make sql choose another key.
The sqlindex property has been set on any key.
The piece of code is in a RTC report; version NAV 2009R2 build 32275
0
Answers
-
CORRECTION: after unmaintaining the sql index, the report runs actually fast (about 10x faster), but sql do the order by clause with the Item No.,Last Source Code,Last Source Type,Code key :-k0
-
Problem solved. It turned out to be a dataitem with a sourcetableview <> from the the link with the parent dataitem.
I really miss some performance troubleshooter for RTC. Sql profiler is not enough....at least at my level of knowledge of it :whistle:
Sometimes it's really hard to understand what piece of code/property generated a bad query.0 -
SETCURRENTKEY should match the ORDER BY clause
SETFILTER/SETRANGE should match the WHERE clause, although there are some funky things going on there sometimes, where you see the same field filtered more than once.
If you have a SETCURRENTKEY on field 1 and 2, and you see an ORDER BY clause on two different fields, then you are not looking at the C/AL that generated the SQL.
Whether SQL Server actually uses a certain index or not doesn't affect the WHERE and ORDER BY clause.
If I remember correctly, Kriki (here on Mibuso) and Waldo (on his blog) both wrote a few detailed posts about the correlation between C/AL and the queries, see if you can find those.0 -
DenSter wrote:SETCURRENTKEY should match the ORDER BY clause
SETFILTER/SETRANGE should match the WHERE clause, although there are some funky things going on there sometimes, where you see the same field filtered more than once.DenSter wrote:If you have a SETCURRENTKEY on field 1 and 2, and you see an ORDER BY clause on two different fields, then you are not looking at the C/AL that generated the SQL.SETCURRENTKEY(field1,field2); SETRANGE(field3,'A'); FINDSET;
just becase i'm dumb or i do really need to order my table like this...then WHERE and ORDER BY "does not match"...and it's C/AL code that generated the "bad" query :-kDenSter wrote:Whether SQL Server actually uses a certain index or not doesn't affect the WHERE and ORDER BY clause.DenSter wrote:If I remember correctly, Kriki (here on Mibuso) and Waldo (on his blog) both wrote a few detailed posts about the correlation between C/AL and the queries, see if you can find those.0 -
Belias wrote:DenSter wrote:SETCURRENTKEY should match the ORDER BY clause
SETFILTER/SETRANGE should match the WHERE clause, although there are some funky things going on there sometimes, where you see the same field filtered more than once.Belias wrote:DenSter wrote:If you have a SETCURRENTKEY on field 1 and 2, and you see an ORDER BY clause on two different fields, then you are not looking at the C/AL that generated the SQL.SETCURRENTKEY(field1,field2); SETRANGE(field3,'A'); FINDSET;
just becase i'm dumb or i do really need to order my table like this...then WHERE and ORDER BY "does not match"...and it's C/AL code that generated the "bad" query :-kBelias wrote:DenSter wrote:Whether SQL Server actually uses a certain index or not doesn't affect the WHERE and ORDER BY clause.0
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