Listing only attribute indexes

4358
3
11-21-2013 02:09 AM
AleydisG__Pere
Occasional Contributor
I need a script to check the attribute indexes of every feature class in a geodatabase, to delete or create the indexes to fit my needs.

I've used the ListIndexes arcpy function but it lists not only attribute indexes but also spatial indexes.

Is there a way to list only attribute indexes?

import arcpy

fields =  "OBJECTID"
index_name =  "FDO_OBJECTID"
unique_vals = "UNIQUE"
order = "ASCENDING"
FSO_OID = False

try:

    gdb = arcpy.GetParameterAsText(0) 
    arcpy.env.workspace = gdb

    fdatasets = arcpy.ListDatasets()
    for fdataset in fdatasets:
        arcpy.AddMessage("Feature dataset: " + fdataset)
        arcpy.AddMessage('-' * 40)
        fcs = arcpy.ListFeatureClasses('','',fdataset)
        for fc in fcs:
            indexes = arcpy.ListIndexes(fc) # ----> Lists both attribute and spatial indexes!!!
            for index in indexes:
                if (index.name <> index_name):
                    arcpy.RemoveIndex_management(fc, index.name)
                else:
                    FSO_OID = True
            if FSO_OID == False:
                arcpy.AddIndex_management(fc, fields, index_name, unique_vals, order)

except:
    arcpy.GetMessages(2)
Tags (2)
0 Kudos
3 Replies
JamesCrandall
MVP Frequent Contributor
I need a script to check the attribute indexes of every feature class in a geodatabase, to delete or create the indexes to fit my needs.

I've used the ListIndexes arcpy function but it lists not only attribute indexes but also spatial indexes.

Is there a way to list only attribute indexes?

import arcpy

fields =  "OBJECTID"
index_name =  "FDO_OBJECTID"
unique_vals = "UNIQUE"
order = "ASCENDING"
FSO_OID = False

try:

    gdb = arcpy.GetParameterAsText(0) 
    arcpy.env.workspace = gdb

    fdatasets = arcpy.ListDatasets()
    for fdataset in fdatasets:
        arcpy.AddMessage("Feature dataset: " + fdataset)
        arcpy.AddMessage('-' * 40)
        fcs = arcpy.ListFeatureClasses('','',fdataset)
        for fc in fcs:
            indexes = arcpy.ListIndexes(fc) # ----> Lists both attribute and spatial indexes!!!
            for index in indexes:
                if (index.name <> index_name):
                    arcpy.RemoveIndex_management(fc, index.name)
                else:
                    FSO_OID = True
            if FSO_OID == False:
                arcpy.AddIndex_management(fc, fields, index_name, unique_vals, order)

except:
    arcpy.GetMessages(2)



What I don't know is if *all* spatial indexes have the word "Shape" in them.  If so, then you can just test for this string value in the index name:

'skip over any index name with string Shape in it
if not "Shape" in index.name:
   arcpy.RemoveIndex_management(fc, index.name)


Or alternatively, can you just skip over the ObjectID field to check for attribute indexes?  (I am not sure if that is sufficient because I don't know if all spatial indexes are found on that field).


'describe each featureclass and build a list of indexes on each
for fc in fcs:
   dsc = arcpy.Describe(fc)
   listOfIndexes = [idx.name for idx in dsc.Indexes][1:]



I question the validity of simply skipping over the OID field as this.  When I look at the properties of a FeatureClass and see it's spatial index name "FDO_Shape", it also shows up in the listOfIndexes list when run.  So, it appears to not catch it by simply skipping over the OID.

Sorry for a non-direct answer, but maybe you can sort it out from here.
0 Kudos
AleydisG__Pere
Occasional Contributor
Your answer is quite helpful because I've realized that the spatial index for personal geodatabases is always Shape_index and the spatial index for file geodatabases is always FDO_SHAPE. Sometimes they are uppercase and sometimes lowercase, but that's all.
It's as simple as skipping those index names. Thanks! 🙂

Now I have a new problem related to the attribute indexes based on the OBJECTID field. I can't delete any attr. index based on that field. I'm getting an error message. 😕

In fact, I can't add an attribute index based on the OBJECTID field either. :((
0 Kudos
JamesCrandall
MVP Frequent Contributor
Now I have a new problem related to the attribute indexes based on the OBJECTID field. I can't delete any attr. index based on that field. I'm getting an error message. 😕

In fact, I can't add an attribute index based on the OBJECTID field either. :((


Probably this is because the index already exists.  Try dropping it first... From http://resources.arcgis.com/en/help/main/10.1/index.html#/Add_Attribute_Index/00170000005z000000/

If an index name already exists, it must be dropped before it can be updated.
0 Kudos