skimhub / salesforce-python-toolkit

Automatically exported from code.google.com/p/salesforce-python-toolkit
GNU Lesser General Public License v3.0
0 stars 0 forks source link

Performance fix for enterprise.retrieve #13

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Do a retrieve of 113 records by Id list.
2. It takes 2 minutes.
3. It took 113 API calls.

What is the expected output? What do you see instead?
The expected output is correct, but slow and expensive.
The attached code appears to product the same results, uses more memory,
but faster and less expensively. 

   def retrieve(
        self,
        fieldList,
        sObjectType,
        ids,
        ):
        '''
    Currently, this uses query() to emulate the retrieve() functionality, as suds' unmarshaller
    borks on the sf: prefix that Salesforce prepends to all fields other than Id and type (any
    fields not defined in the 'sObject' section of the Enterprise WSDL). This code used to
    do a single API query for each Id. This was both slow and expensive.
    '''
        if len(ids) == 0:
            return []
        idsin = ["'%s'" % id for id in ids]
        idsin = ",".join(idsin)
        idsin = "(%s)" % idsin
        sObjects = []
        queryString = 'SELECT Id, ' + fieldList + ' FROM ' \
            + sObjectType + ' WHERE Id in %s ' % idsin
        queryResult = self.query(queryString)
        resultsById = {}
        for record in queryResult.records:
            if record:
                sObject = self.generateObject(sObjectType)
                for (k, v) in record:
                    setattr(sObject, k, v)
            resultsById[sObject.Id] = sObject
        for id in ids:
            sObject = resultsById.get(id, None)
            if sObject:
                sObjects.append(sObject)
            else:
                sObjects.append(None)

        return self._handleResultTyping(sObjects)

Original issue reported on code.google.com by sean.t...@gmail.com on 6 Jul 2011 at 2:48

GoogleCodeExporter commented 8 years ago
You're absolutely right; I envisioned folks using query() for larger result 
sets, but it's certainly unacceptable to burn API calls (not to mention time!) 
like that.  Will verify patch and commit.

Thanks!

David

Original comment by da...@loggly.com on 6 Jul 2011 at 5:40

GoogleCodeExporter commented 8 years ago

Original comment by dlanst...@gmail.com on 6 Jul 2011 at 5:41

GoogleCodeExporter commented 8 years ago
Excellent. Thanks so much for the good work here. It's much appreciated.

Original comment by sean.t...@gmail.com on 7 Jul 2011 at 12:16