Tuesday, January 10, 2012

Tip - Faster Update Cursors

There are many ways to limit the number of fields an update cursor can update.  The cursor object itself has a where clause option.  Another way, which seems at first glance to be faster than just a where clause on the cursor, is to use a Layer object with a definition query defined on the Layer object, then reference the object instead of the feature class or feature layer itself.

import arcpy
from arcpy import mapping
fclayer = "fclayer"
arcpy.MakeFeatureLayer_management(source, fclayer)
layer = mapping.Layer(fclayer)
layer.definitionQuery = "UIDField = 'unique'"
uRows = arcpy.UpdateCursor(layer)
row = None
for row in uRows:
    row.URL = url
del row
del uRows
del layer
del fclayer

For more information check out the help page here.