|
| 1 | +--- |
| 2 | +title: Improve FetchXML request performance | Microsoft Docs |
| 3 | +description: Learn how developers can improve FetchXML request performance when using Common Data Service. |
| 4 | +author: NHelgren |
| 5 | +manager: annbe |
| 6 | +ms.service: powerapps |
| 7 | +ms.topic: article |
| 8 | +ms.date: 09/28/2020 |
| 9 | +ms.author: nhelgren |
| 10 | +ms.reviewer: "pehecke" |
| 11 | +search.audienceType: |
| 12 | + - developer |
| 13 | +search.app: |
| 14 | + - PowerApps |
| 15 | + - D365CE |
| 16 | +--- |
| 17 | + |
| 18 | +# Improve FetchXML request performance |
| 19 | + |
| 20 | +An option available in FetchXML requests called *LateMaterialize* allows you to break up such |
| 21 | +requests into smaller more usable segments which can improve the performance of long running FetchXML requests. |
| 22 | + |
| 23 | +> [!NOTE] |
| 24 | +> Performance improvements depend on the data distribution for each |
| 25 | +> participating entity and linked entity. Late materialization may not always |
| 26 | +> provide a performance benefit. It is best used if you are experiencing |
| 27 | +> performance issues with your existing fetch request. |
| 28 | +
|
| 29 | +Executing a traditional fetch for a given number of the top entity records will pull all |
| 30 | +the columns in the select list that meet the filter criteria. Let’s say you are |
| 31 | +pulling the top 500 records on an entity that has 100 columns and 100K rows |
| 32 | +that meet the filter criteria, this request can cause issues in two ways: |
| 33 | + |
| 34 | +- The 99.5K rows will pull all columns, even though you only need to populate |
| 35 | + the select list for 500 rows when returning to the client. |
| 36 | + |
| 37 | +- Query Optimizer can generate an arbitrary order when retrieving the child |
| 38 | + columns, resulting in an undesired data order. |
| 39 | + |
| 40 | +Using `LateMaterialize` allows you to create a fetch that will: |
| 41 | + |
| 42 | +- First pull only the primary ID of the top number of records specified. |
| 43 | + |
| 44 | +- Select only the columns of data needed based on the primary IDs that were |
| 45 | + retrieved. For example, where only 5 columns are needed for display in the form. |
| 46 | + |
| 47 | +By pulling only the needed data after the primary IDs are collected, the |
| 48 | +retrieval is much faster as data that is not needed for the current operation is |
| 49 | +excluded. |
| 50 | + |
| 51 | +This is most beneficial when: |
| 52 | + |
| 53 | +- The entity you are querying has one or more links to other entities for column data. |
| 54 | + |
| 55 | +- There are many columns in the entity. |
| 56 | + |
| 57 | +- The entity contains logical attributes. |
| 58 | + |
| 59 | +## Syntax |
| 60 | + |
| 61 | +```xml |
| 62 | +<fetch version="1.0" output-format="xml-platform" latematerialize="true" |
| 63 | + mapping="logical" distinct="true"> |
| 64 | + |
| 65 | + <entity name="[entity]"> |
| 66 | + <attribute name="[attribute]" /> |
| 67 | + |
| 68 | + <link-entity name="[entity]" from="[linked entity]" to="[linked entityid]" |
| 69 | + link-type="outer" alias="[alias]"> |
| 70 | + <attribute name="[name of linked entity column]" /> |
| 71 | + </link-entity> |
| 72 | + |
| 73 | + <filter type=[filter type]> |
| 74 | + <condition attribute="[column]" operator="[operator]" value="[value]"/> |
| 75 | + </filter> |
| 76 | + </entity> |
| 77 | + |
| 78 | +</fetch> |
| 79 | +``` |
| 80 | + |
| 81 | +## Sample |
| 82 | + |
| 83 | +```XML |
| 84 | +<fetch version="1.0" output-format="xml-platform" latematerialize="true" |
| 85 | + mapping="logical" distinct="true"> |
| 86 | + |
| 87 | + <entity name="account"> |
| 88 | + <attribute name="accountnumber" /> |
| 89 | + <attribute name="createdby" /> |
| 90 | + <attribute name="ownerid" /> |
| 91 | + |
| 92 | + <link-entity name="account" from="accountid" to="parentaccountid" |
| 93 | + link-type="outer" alias="oaccount"> |
| 94 | + <attribute name="createdby" /> |
| 95 | + |
| 96 | + <link-entity name="account" from="accountid" to="accountid" link-type="outer" |
| 97 | + alias="oaccount1"> |
| 98 | + <attribute name="createdby" /> |
| 99 | + <attribute name="accountid" /> |
| 100 | + <attribute name="name" /> |
| 101 | + </link-entity> |
| 102 | + </link-entity> |
| 103 | + |
| 104 | + <link-entity name="account" from="accountid" to="accountid" link-type="outer" |
| 105 | + alias="oaccount2"/> |
| 106 | + |
| 107 | + <filter type='and'> |
| 108 | + <condition attribute="statecode" operator="eq" value="2"/> |
| 109 | + </filter> |
| 110 | + </entity> |
| 111 | + |
| 112 | +</fetch> |
| 113 | +``` |
| 114 | + |
| 115 | +### See Also |
| 116 | + |
| 117 | +[Use FetchXML to construct a query](use-fetchxml-construct-query.md) |
0 commit comments