# Get row counts of all tables in file geodatabase, ideally from metadata

I need some help with finding a way to list all the row counts for many tables (feature classes as well as non-spatial ones) in a file geodatabase. At the moment it's done manually by viewing the description tab in ArcCatalog and copying and pasting but I think there should be a way to bring out that information using arcpy. (EDIT, see further below: there is possibly also a way to do it via OGR in QGIS).

The file geodatabase structure is as follows:

my_geodb.gdb
|----Feature_Dataset1
|----Table1
|----Table2
|----Table3
|----Table4
|...


I have gotten as far as doing this with a bit of help from here:

import arcpy
arcpy.env.workspace = my_geodb.gdb
oldws = arcpy.env.workspace               #saves this as main workspace

featureDatasets = arcpy.ListDatasets()    #lists all datasets in geodb
for fd in featureDatasets:
arcpy.env.workspace = oldws + "\\" + fd
featureClasses = arcpy.ListFeatureClasses()
for fc in featureClasses:
count = arcpy.GetCount_management(fc).getOutput(0)
print fc, count


It's just getting the row counts from the metadata that I'm having problems with, my arcpy API knowledge is a bit inadequate for that. The above code works but it's really slow, and I was wondering how I can pull these row counts directly from the metadata since it's displayed there already in the ArcCatalog.

I'm finding this and this might help.

EDIT:

Trying to go with the way of ogr in qgis now. Referencing this.

Still need to work out the code to do so.

• See if you can list the feature dataset names as your next step. If not, then I think that is where you are currently stuck and the question for you to research/ask should probably be that. – PolyGeo Aug 4 '16 at 20:54
• Cool, done with that. – weiji14 Aug 4 '16 at 21:03
• Now list the feature classes in each feature dataset and use GetCount to count their rows. – PolyGeo Aug 4 '16 at 21:05
• I need the row counts for feature classes as well as non-spatial tables (updated the post to reflect that). But yes thank you for pointing out the track :) – weiji14 Aug 4 '16 at 21:12
• Just a small correction, GetCount counts the rows whether your subject is table or feature class (which is simply a table with renderable feature in a field). As far as I know Object Count element is part of auto update trigger of ArcGIS metadata creation once you run any process on a feature class therefore, to you, extracting the row number from there is more plausible. I think the essence of your questions is having performance issues with GetCount function once apply to large datasets and alternative way of accessing this information (through metadata possibly). – fatih_dur Aug 5 '16 at 1:09

Finally got the code working, referencing the OGR API.

import osgeo
from osgeo import ogr
geoDatabases = [r'\\path\to\mygeodb.gdb']         #insert list of geodatabases here

for geodb in geoDatabases:
print "Row counting on", geodb.split('\\')[-1]
ogdb= ogr.Open(geodb)                         #Opens filegdb using ogr driver

noOfLyrs = ogdb.GetLayerCount()               #counts no. of feature classes in geodatabase
for fcIdx in range(0, noOfLyrs):              #loop through feature classes
fc = ogdb.GetLayer(fcIdx)                 #gets feature class
print fc.GetName(), fc.GetFeatureCount()  #prints feature class name and feature count


Over 4 geodatabases with about 70 feature classes and a total of >170,000,000 rows, the arcpy script I posted in the question takes probably half an hour to run, whereas this 10-line script using ogr (ran in qgis) takes 20 seconds. So yes, I'm happy with this open source solution, though definitely interested in a pure arcpy/xml based method.