Monday, 6 June 2016

Restricting the list of fields selected from database

Hi Everyone

Sometimes you need better control on how Acumatica selects data from database. And here Acumatica Framework provides you some ways to optimize data query.
Using PXFieldScope you can specify what fields will be selected form database. This can save some traffic and processors time in large and extra-large statements. Also it can improve performance in case you have plenty of data, but use very few fields from the selected tables. And of course excluding of some complex sub-selects may significantly improve data selecting on SQL servers side.

No RowSelecting events (e.g. from attributes) will be fired for the fields not fed into the FXFieldScope constructor.
Note, that for the head table all key fields should be included into the field scope declaration. 

For example ARStatement and ARStatementCycle tables both include a lot of decimal and string fields (like statement totals for various ages, aging bucket labels, etc.) Still, under some circumstances these field may be not needed to perform an operation on statements. If there is substantial amount of statements in the system, restricting the selected fields may boost performance significantly.

PXSelectBase<ARStatement> select = new PXSelectJoin<ARStatement,
    LeftJoin<ARStatementCycle, On<ARStatementCycle.statementCycleId,
        Equal<ARStatement.statementCycleId>>,
    LeftJoin<Customer, On<Customer.bAccountID,   
        Equal<ARStatement.statementCustomerID>>>>>(this);

using (new PXFieldScope(select.View,
    typeof(ARStatement.branchID),
    typeof(ARStatement.statementCycleId),
    typeof(ARStatement.statementDate),
    typeof(ARStatement.customerID),
    typeof(ARStatement.statementCustomerID),
    typeof(ARStatement.curyID),
    typeof(ARStatement.dontPrint),
    typeof(ARStatement.dontEmail),
    typeof(ARStatement.printed),
    typeof(ARStatement.emailed),
    typeof(ARStatementCycle.descr),
    typeof(Customer.printCuryStatements)))
{
    PXResultset<ARStatement> res = select.Select();
}


Big thanks to Acumatica Development team for this example.
Have a nice development!

No comments: