Keys

Quivelus
Member Posts: 24
Hi, I have two questions about keys and SQL (Working on SQL):
1)
Supose you have a key defined consisting of Field1, Field2 and Field3. Now you want to use this key to 'filter' on Field1 and Field3.
I know it is not best to use that key for this reason, but in some cases (with more keyfields) it can be useful to use the key instead of creating a new one.
SETCURRENTKEY("Field1","Field2","Field3");
SETRANGE("Field1",value1);
SETRANGE("Field2"); // Does this line matter?
SETRANGE("Field3",value3);
The question is wether the line with range on Field2 does matter?
2) Supose you have a key defined consisting of Field1, Field2 and Field3 (in that order!). With this key you define a SQLIndex (and you maintain an SQLIndex) Field3, Field2 and Field1 (in this order). In code it is probably best to write
SETCURRENTKEY("Field1","Field2","Field3");
SETRANGE("Field1",value1);
SETRANGE("Field2",value2);
SETRANGE("Field3",value3);
But is this automatically converted to the correct SQLIndex? Because the first field to 'filter' on there is Field3. How is this done by Navision?
Tx.
1)
Supose you have a key defined consisting of Field1, Field2 and Field3. Now you want to use this key to 'filter' on Field1 and Field3.
I know it is not best to use that key for this reason, but in some cases (with more keyfields) it can be useful to use the key instead of creating a new one.
SETCURRENTKEY("Field1","Field2","Field3");
SETRANGE("Field1",value1);
SETRANGE("Field2"); // Does this line matter?
SETRANGE("Field3",value3);
The question is wether the line with range on Field2 does matter?
2) Supose you have a key defined consisting of Field1, Field2 and Field3 (in that order!). With this key you define a SQLIndex (and you maintain an SQLIndex) Field3, Field2 and Field1 (in this order). In code it is probably best to write
SETCURRENTKEY("Field1","Field2","Field3");
SETRANGE("Field1",value1);
SETRANGE("Field2",value2);
SETRANGE("Field3",value3);
But is this automatically converted to the correct SQLIndex? Because the first field to 'filter' on there is Field3. How is this done by Navision?
Tx.
Bohr-ing.
0
Comments
-
Try to find the SQL Perform webcasts on the download pages.
These will give you a good idea into how this works.
It is completely different from Native and way to complex to explain in just one short reply.
Please let me know if any questions are there after viewing the webcast.0 -
Main part is: SETCURRENTKEY in NAV is just sort order for MS SQL. (but the "just" doesn't mean that it will not have impact to execution plan).0
-
Quivelus wrote:SETCURRENTKEY("Field1","Field2","Field3");SETRANGE("Field1",value1);SETRANGE("Field2"); // Does this line matter?SETRANGE("Field3",value3);The question is wether the line with range on Field2 does matter?
SELECT * FROM MyRecord WHERE (Field1 = Value1) AND (Field3 = Value3) ORDER BY Field1,Field2,Field3
Quivelus wrote:2) Supose you have a key defined consisting of Field1, Field2 and Field3 (in that order!). With this key you define a SQLIndex (and you maintain an SQLIndex) Field3, Field2 and Field1 (in this order). In code it is probably best to write SETCURRENTKEY("Field1","Field2","Field3");SETRANGE("Field1",value1);SETRANGE("Field2",value2);SETRANGE("Field3",value3);But is this automatically converted to the correct SQLIndex? Because the first field to 'filter' on there is Field3. How is this done by Navision?SELECT * FROM MyRecord WHERE (Field1 = Value1) AND (Field2 = Value2) AND (Field3 = Value3) ORDER BY Field1,Field2,Field3
The Index is built like this: Field3, Field2, Field1So, now trouble COULD arise: the index includes all fields from the WHERE clause which is good. But the sorting is quite the opposite way; thus SQL Server has to spent some work to re-sort the resultset after retrieving the records with the Index. Now it depends on the other available indexes and their selectivity - especially the Clustered Index - if SQL Server indeed will pick this Index, or another one - or starts scanning the full table (very bad).The higher the difference between Index and Sorting, the higher the probaility to cause Index Scans. That's why Index Hinting is stupid: You force SQL Server to use an Index which may not be the best one which could result in Index Scanning. Scanning takes longer than Seeking and brings higher potentials for blocking conflicts ...Hope this could help a little.Best regards,JörgJörg A. Stryk (MVP - Dynamics NAV)
NAV/SQL Performance Optimization & Troubleshooting
STRYK System Improvement
The Blog - The Book - The Tool0
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