Adding Fields: Performance Tips

Two approaches to help increase performance when adding numerous fields to a table or feature class.

1. Always load or create the table or feature class in-memory:

import arcpy
# Add fields to a feature class loaded into memory.
fc = r'c:\data\boston.gdb\parcels'
arcpy.management.MakeFeatureLayer(fc, 'parcels_lyr')
for f in out_fields:
    arcpy.AddField_management('parcels_layer',
                              f.name,
                              field_type=f.type,
                              field_length=f.length,
                              field_alias=f.aliasName)

 

import os
import arcpy

# Add the fields to an in_memory table.
tmp_table = os.path.join('in_memory', 'table_template')
arcpy.management.CreateTable(*os.path.split(tmp_table))
for f in out_fields:
    arcpy.AddField_management(tmp_table,
                              f.name,
                              field_type=f.type,
                              field_length=f.length,
                              field_alias=f.aliasName)

# Create the actual output table.
arcpy.CreateTable_management(out_path,
                             out_table_name,
                             template=tmp_table)
arcpy.Delete_management(tmp_table)

2. Use the data access and NumPy modules. The data access module function named ExtendTable() joins the contents of a NumPy structured array to a table based on a common attribute field.
This is the faster approach, however, the types of fields you can add using numpy are limited. There is no support for adding blobs, raster, and date fields.  In addition, the field alias can not be defined or altered.

import arcpy
import numpy

fc = r"c:\data\water.gdb\wells"

narray = numpy.array([],
numpy.dtype([('_ID', numpy.int),
             ('WELL_ID', numpy.int),
             ('DESC', '|S100'),
             ('DEPTH', numpy.float),
             ]))

arcpy.da.ExtendTable(fc, "OID@", narray, "_ID")