Integrations and APIs for Service

Get Involved. Join the Conversation.

Topic

    Allan Schrum
    ROQL::queryObjects() vs ROQL::query()
    Topic posted September 8, 2015 by Allan SchrumGold Crown: 30,000+ Points, last edited December 10, 2015 
    962 Views
    Title:
    ROQL::queryObjects() vs ROQL::query()
    Content:

    A common topic is: when do I use QueryObjects versus using QueryCSV in Connect Web Services for SOAP. The equivalent question in Connect for PHP is: when do I use ROQL::queryObjects() versus using ROQL::query(). I'll use ROQL::queryObjects() and ROQL::query() for this discussion as it comes up more in the context of Connect for PHP.

    First, what is the difference between ROQL::query() and ROQL::queryObjects()? Perhaps some pseudo code will demonstrate how ROQL::queryObjects() works:

    $result = RNCPHP\ROQL::queryObjects( "SELECT Contact FROM Contact WHERE Organization.LookupName = 'Oracle' " );

    becomes

    $result = array();
    $rrs = RNCPHP\ROQL::query( "SELECT ID FROM Contact WHERE Organization.LookupName = 'Oracle' " );
    while ( $rrs && $rr = $rrs->next() )
    {
        while ( $row = $rr->next() )
        {
            $result[] = RNCPHP\Contact::fetch( $row['ID'] );
        }
        unset( $rr );
    }
    unset( $rrs);

    As you can see there is a flurry of activity created by the ROQL::queryObjects() request. So what is the advantage of this approach? Sure, you have the whole object, but do you need the whole object? If you are only trying to find out the first and last name of the Contact this seems like a large penalty to pay when a simple ROQL::query() would suffice:

    $rrs = RNCPHP\ROQL::query( "SELECT Name.First, Name.Last FROM Contact WHERE Organization.LookupName = 'Oracle' ");

    Another point I often hear is: I like to use objects - I support object oriented programming. Therefore, ROQL::queryObjects() is the API to use.

    In general, I agree, but I temper that with how the objects are used for the purpose at hand. Suppose I wanted to make The Cool Web Page that shows all those Contacts that belong to Oracle? I just want to display their first and last name in a paged format with 500 people per page. I'll ORDER BY ID so that I get a repeatable output that pages easily. For that first page to be displayed, what would the two different approaches look like? And what is the cost of each?

    Using ROQL::queryObjects() it would be something like (and I'll leave the HTML output as an exercise for you):

    $results = RNCPHP\ROQL::queryObjects( "SELECT Contact FROM Contact WHERE Organization.LookupName = 'Oracle' ORDER BY ID" );

    Now that I have my result I iterate over each object and output each first and last name. How much work was done? The resource that really embodies the concept of "work" is the database, so let's look at it from that point of view. Using the pseudo-code above, I need 1 query for the list of IDs, followed by 500 Contact::fetch() calls, each call returning information using a query from the database. Thus this approach has a "cost" of 501 queries. That seems like a lot given this is a page of simple data. What's the cost of the ROQL::query() approach?

    $rrs = RNCPHP\ROQL::query( "SELECT Name.First, Name.Last FROM Contact WHERE Organization.LookupName = 'Oracle' ORDER BY ID" );

    One query. Same information. That is 501 vs. 1 in terms of cost. As there is nothing for free it would make sense that the approach that costs less is the approach that should be chosen. ROQL::query() wins!

    So usage is the big driver of why we would want to use ROQL::queryObjects() instead of ROQL::query(). My "Rule of Thumb" for its use is this: are you calling save() on these objects?

    If you are trying to save() these objects by making bulk changes, then ROQL::queryObjects() make sense. Otherwise, use ROQL::query() as you'll then get the data you need with the most efficient set of queries possible.

    Regards,

    -Allan

     

    Version:
    All since 10.5 when Connect was first released