In Dynamics 365 for Finance and Operations, SQL statements can often become performance bottlenecks. Therefore, it is very important to understand how Visual Studio handles database queries and to follow all the best practice recommendations in order to keep your system healthy and efficient.
In this recipe, we will discuss some of the best practices to use when writing database queries. For demonstration purposes, we will create a sample find method with different logic and queries and discuss each of them. The method will locate the CustGroup table record of the given customer account.
How to do it
As methods are not allowed on extensions, to demonstrate this recipe we need to over layer a CustGroup table. Add a CustGroup table in your project, and create the following method:
///
///
///
///
///
///
public static CustGroup AnsarifindByCustAccount(CustAccount _custAccount, boolean _forupdate = false)
{
CustTable custTable;
CustGroup custGroup;
if (_custAccount)
{
select firstOnly CustGroup from custTable
where custTable.AccountNum == _custAccount;
}
if (custTable.CustGroup)
{
if (_forupdate)
{
custGroup.selectForUpdate(_forupdate);
}
select firstOnly custGroup where
custGroup.CustGroup == custTable.CustGroup;
}
return custGroup;
}
In the same table, create another method with the following code snippet:
In this recipe, we have two different versions of the same method. Both methods are technically correct, but the second one is more efficient. Let's analyze each of them.
In the first method, we should pay attention to the following points:
Verify that the _custAccount argument is not empty; this will avoid the running of an unnecessary database query.
Use the firstOnly keyword in the first SQL statement to disable the effect of the read-ahead caching. If the firstOnly keyword is not present, the statement will retrieve a block of records, return the first one, and ignore the others. In this case, even though the customer account is a primary key and there is only one match, it is always recommended that you use the firstOnly keyword in the find() methods.
In the same statement, specify the field list--the CustGroup field--we want to retrieve, instructing the system not to fetch any other fields that we are not planning to use. In general, this can also be done on the Application Object Tree query objects by setting the Dynamic property of the Fields node to No in the query data sources and adding only the required fields manually. This can also be done in forms by setting the OnlyFetchActive property to Yes in the form's data sources.
Execute the selectForUpdate() method only if the _forupdate argument is set. Using the if statement is more efficient than calling the selectForUpdate() method with false.
The second method already uses all the discussed principles, plus an additional one:
Both the SQL statements are combined into one using an exists join. One of the benefits is that only a single trip is made to the database. Another benefit is that no fields are retrieved from the customer table because of the exists join. This makes the statement even more efficient.
There's more
To understand the preceding code in more depth, let's try to analyze the execution using one of the earlier recipes code. Carry out the following:
Add a new class in your project and write the following code:
Now, set your class and project as startup object/project. Save all your code and run the solution. You will get the following results:
Method 1, where we used different if statements to validate some conditions clearly shows that it took 1203 ms, while Method 2, where we used if statement wisely to optimize the code and execution time, took 344 ms. This is how you must use efficient code during development to reduce the time taken and speed up your execution time for better performance.
Stay Ahead of the Curve! Check out these trending topics and sharpen your skills.
Explore comprehensive X++ programming language tutorials and training resources to master the intricacies of X++ development. From beginner basics to advanced techniques, dive into our curated guides, tips, and hands-on exercises to enhance your proficiency in X++ programming.