How to determine number of records using arcpy.da.SearchCursor

32654
13
Jump to solution
03-21-2014 10:46 AM
BruceBacia
Occasional Contributor
Does anyone know of a quick way to return the number of records in a query constrained Search Cursor using the da module?

rows = arcpy.da.SearchCursor(someFC,someFields,someQuery)  print (len(rows))  #I'm sure this used to work with the old cursors, but doesn't work with da.SearchCursor
Tags (2)
13 Replies
JasonScheirer
Regular Contributor II
@Chris -- because of that last, millions-of-records result. There's a certain amount of time required to set up/tear down a GP tool (which is what's causing the first two results in your benchmarks to look the way they do), but a for loop in a core GP tool looping over rows in its native ArcObjects/C++ implementation will be orders of magnitude faster than that of a for loop in Python, not to mention GetCount can do other tricks that the Python code isn't doing like seeing if there is a selection set on the table or layer, and if so just asking for the length of the list of selected records without needing to open a cursor on the actual data.
ChrisSnyder
Regular Contributor III
Jason - thanks for the explanation. For the smaller record counts, I too noticed the slight overhead (set up/tear down a GP tool) of the GetCount tool, which is why I started using the cursor method (which didn't seem to have the extra overhead that GetCount does). In my case, I had some code that needed to inspect a recursive feature count hundreds/thousands of times in a loop, and I found the cursor method (because the record count was relatively small) was the best performer (BTW: using the .fidSet describe property was the worse).

Per the count of the selected set, you can of course do it like:

arcpy.MakeFeatureLayer_management(fc, "fl1")
arcpy.SelectLayerByAttribute_management("fl1", "NEW_SELECTION", "LAND_COVER_CD in (41,42)")
result = len([r[0] for r in arcpy.da.SearchCursor("fl1", ["OID@"])])
#Or this if you don't need an actual "selected set"...
result = len([r[0] for r in arcpy.da.SearchCursor(fc, ["OID@"], "LAND_COVER_CD in (41,42)")])
0 Kudos
BruceBacia
Occasional Contributor
Thanks for all of the helpful posts.  This script works with Workflow Manager, and I think that with my current workflow(below), if I used GetCount, I would have to make 2 feature layers.  So I'll go with len([r for r in arcpy.da.SearchCursor....])

Workflow
-Use .sde connection file to connect to default version of database.
-Get what should be a unique polygon attribute (but isn't always unique)  from WMX for a SQL query.
-Make feature layer of whole feature class without SQL query (The whole feature class is created because the polygon may or may not yet exist in default - rec and posts are scheduled nightly)
-Change version (based on WMX user)
-Create SearchCursor on version, using SQL query to limit records, and get len(SearchCursor) (Here I could create another feature layer using the SQL query, in order to be able to use GetCount.  Seems inefficient?)
-Based on number of records in search cursor(>1, 1, or 0), do stuff.
0 Kudos
Leandro-Zamudio
Occasional Contributor

I'd suggest you to encapsulate it into a function. I developed a tiny function called SearchCounter derivated from a Validator in a previous blog post.

def SearchCounter(cursor):

     counts = 0

     for counter in cursor:

          counts += 1;

     cursor.reset()

     return counts

#Previous Code

#...

RecordCount = SearchCounter(arcpy.da.SearchCursor(Table,Fields,Query))

#...

#End Code

Greetings

0 Kudos