The article discusses the optimisation of a Dataverse plugin's QueryExpression, initially using ColumnSet(true) to retrieve all readable attributes. In simplifying the query by selecting only necessary attributes, an unexpected bug emerged when the comparison logic using Entity.Id failed, as all entities returned with Guid.Empty as their ID. The root cause was tied to the use of Distinct = true, which neglects primary key values in its aggregated results. Adding the primary key back into the ColumnSet fixed the issue. Moreover, since the original scenario did not naturally lead to duplicate rows, using Distinct = true proved unnecessary, demonstrating a valuable lesson in performance optimization: always review whether each query component is necessary. Various lessons are drawn about improving performance without introducing errors, such as ensuring primary keys are included when relying on Entity.Id and reviewing the necessity of query components like Distinct = true.
Login now to access my digest by 365.Training