Dynamics NAV queries tuning: little ideas for the NAV Team

Lots of my daily work with Dynamics NAV is tuning existing systems or architecturing the right and most efficient way of performing certain complex queries.

Tuning a query on a NAV database is extremely important, expecially if the database is big and with lots of concurrent users.

On my blog I've written in the past how you can tune up your Dynamics NAV database (SQL Server) or how you can introduce tricks in order to perform complex works in a more efficient and performant way.

Now the subject of this post is totally for the NAV Dev Team in Microsoft...

There are two really simple tricks that I think could be really helpful for optimizing a NAV query on SQL Server, and they're based on simple concepts that are the basics for every SQL developer. Why not introduce them on NAV natively?

I'll try to explain here what I've on my mind...

1) Introducing the way to avoid "Select *" on SQL Queries:

When you write C/AL code in Dynamics NAV, your code will be translated into a SQL instruction automatically. Let's consider for example this C/AL code:

NAVQuery1

It's a simple loop on every Purchase Line with an Outstanding Quantity > 0 for retrieving the total Outstanding Amount for all the orders (it's only an example... ).

This code will be translated by NAV into a SQL code like this (check via Client Monitor):

SELECT  * FROM "EID$Purchase Line" WITH (READUNCOMMITTED)  WHERE (("Outstanding Quantity">0.0)) AND  "Document Type"=1 AND "Document No_"='9999.80G-53-RU0001' AND "Line No_">50000 ORDER BY "Document Type","Document No_","Line No_" OPTION (FAST 5)

NAV automatically translates every C/AL instruction into a SELECT * query. But in this sample, we want to retrieve only the "Outstanding Amount" field of the "Purchase Line" table, not all the fields.!!

The first thing that you learn when developing with databases is that SELECT * is a performance hit.

I think it could be wonderful if we can have a method called FIELDSTORETRIEVE that works like this:

NAVQuery2

This method accepts as parameters the names of the table fields we want to retrieve. The corresponding SQL query translated from NAV now will be this:

SELECT  "Outstanding Amount" FROM "EID$Purchase Line" WITH (READUNCOMMITTED)  WHERE (("Outstanding Quantity">0.0)) AND  "Document Type"=1 AND "Document No_"='9999.80G-53-RU0001' AND "Line No_">50000 ORDER BY "Document Type","Document No_","Line No_" OPTION (FAST 5)

Now the code is really more efficient...

2) Introducing a Select Distinct method:

In a table, some of the columns may contain duplicate values and sometimes you want to list only the different (distinct) values in a table.

In SQL, the DISTINCT keyword can be used to return only distinct (different) values.

SELECT DISTINCT column_name(s)
FROM table_name

What about NAV? In NAV there's not an easy and native way to obtain a SELECT DISTINCT query and you've to work via C/AL code (with impact on performances).

Why not having a method RETRIEVEDISTINCT like this?

NAVQuery3 

The code above will work like this: it filters the Customer table (I've placed dots for the filter criteria here), it retrieves the "No." and "Name" fields on the Customer table and it uses the DISTINCT keyword. The SQL code that will be generated from NAV will be something like this:

SELECT DISTINCT "No.","Name" FROM "EID$Customer" WITH (READUNCOMMITTED)  WHERE .....

Wonderful and easy I think...

This is surely not the entire list of what I'd like to have on NAV as soon as possible (I'm thinking also about JOIN, .NET Code, etc) but I think that these are really simple things that could be introduced in a wonderful Service Pack.

Someone in the NAV Team can listen to me?

Technorati Tag:

Print | posted on Wednesday, January 07, 2009 2:37 PM

Comments on this post

# re: Dynamics NAV queries tuning: little ideas for the NAV Team

Requesting Gravatar...
Freddy Kristiansen (NAV PM) was kind enough (as usual) to answer me via email.
This is the main content of his response (I think it could be interesting to everyone):

I agree with the goal of what you are trying to achieve - but I don't think we can make it the way you describe.
Over time it is my belief that NAV must move to a .net language (like C# - note that this doesn't mean that we will be working in Visual Studio) and in .net you would have constructs like LINQ to achieve exactly this. If we do something beforehand - it must be something we can convert into the right .net construct - and I am not sure that your ideas are comparable.
Note that the above it my ideas (which might never happen) - not something that is planned right now.
That said - I will forward your suggests to the architect on the Server team - so that he can read the post himself.

/Freddy
Left by Stefano Demiliani on Jan 09, 2009 9:27 AM

# re: Dynamics NAV queries tuning: little ideas for the NAV Team

Requesting Gravatar...
This is the 4th year that I'm working on NAV and I think that the architecture of this very old product is not good anymore. There is no good reason to keep working on this.
Left by Alessandro Cavalieri on Jan 14, 2009 4:08 PM

# re: Dynamics NAV queries tuning: little ideas for the NAV Team

Requesting Gravatar...
A small fix to your code (you used both good as bad examples) and I see to much code where FINDFIRST-REPEAT-UNTIL is used:

if you loop records, you SHOULDN'T use
IF FINDFIRST THEN
REPEAT
...
UNTIL NEXT = 0;

BUT
IF FINDSET(...,...) THEN
REPEAT
...
UNTIL NEXT = 0;

The reason is that a FINDFIRST selects only one record and returns it. When you fire the NEXT, NAV needs to send a new query to SQL.
Left by Alain Krikilion on Jan 28, 2009 2:07 PM

# re: Dynamics NAV queries tuning: little ideas for the NAV Team

Requesting Gravatar...
Yes Alan, you're right... I always use FINDSET (exactly like on my 2nd sample) but on this piece of code I was not so precise :)
Left by Stefano Demiliani on Jan 28, 2009 2:13 PM

# re: Dynamics NAV queries tuning: little ideas for the NAV Team

Requesting Gravatar...
As Freddy says the new LINQ feature that comes out with the .NET 3.5 framework namespacing could be a good starting point in order to improve the SQL querying offred by Dynamics NAV. However, still we have to deal with the good old native database and C/AL language programming.
From my personal point of view, the .NET platform usage and integration is the road ahead for all Dynamics NAV products.
Left by Duilio Tacconi on Feb 11, 2009 11:42 AM

# re: Dynamics NAV queries tuning: little ideas for the NAV Team

Requesting Gravatar...
How can i write Purchline.Filedstoretrieve method ?
Left by Nilanjan Chakraborty on Aug 01, 2009 4:28 AM

# re: Dynamics NAV queries tuning: little ideas for the NAV Team

Requesting Gravatar...
Nilanjan, unfortunately this is not possible (you could use ADO from NAV if you want a particular SQL Query). The above method "FieldsToRetrieve" was only my proposal...
Left by Stefano Demiliani on Aug 03, 2009 12:11 PM

# re: Dynamics NAV queries tuning: little ideas for the NAV Team

Requesting Gravatar...

I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with more information? It is extremely helpful and beneficial to your readers Nice article.The code works fine and is a good starting point for me making a custom module.Thanks for the module..
Left by Boni Poker on Mar 16, 2010 11:51 AM

# replica jewelry

Requesting Gravatar...
Nilanjan, unfortunately this is not possible (you could use ADO from NAV if you want a particular SQL Query). The above method "FieldsToRetrieve"
Left by replica jewelry on Apr 29, 2010 4:37 AM

# re: Dynamics NAV queries tuning: little ideas for the NAV Team

Requesting Gravatar...
cient way of performing certain complex queries.

Tuning a query on a NAV database is extremely important, expecially if the database is big and with lots of concurrent users.
Left by wholesale laptop adapter on May 23, 2010 2:39 PM

Your comment:

 (will show your gravatar)
 
Please add 4 and 6 and type the answer here: