Why would my additional MakeTableView_management() calls take drastically longer?

1682
11
01-25-2017 07:38 AM
Karyn_Kerdolff
New Contributor III

I'm working on a Python script that involves selecting parcels from a feature class and I've run into some odd behavior in my sanity checks. In the code below I loop through the list of specified tax ID numbers and use the MakeTableView trick to get a count of elements matching a specific "where" clause. If I only give it one tax ID, the script finishes in a couple of seconds. If I give it two, it gets stuck on MakeTableView for about 4 minutes, and this seems to increase by about 4 minutes for each additional tax ID. I've tried explicitly deleting the resulting table view as soon as I'm finished with it, but that made no difference.

Ultimately, I changed the script to use a search cursor and the "where" clause to give me the same functionality, but this is just bouncing around the back of my head—why would the subsequent MakeTableView calls perform so much slower than the first?

Edit for clarity: TID is a list of tax ID's obtained by splitting a multi-value GetParameterAsText().

# Add all desired parcels to selection
for tid in TID:
   if tid and tid != "#":
      # Make sure the parcel ID is formatted correctly
      if not re.match(pattern, tid):
         raise GPException("Input Parcel IDs must be in the format " +
                  "YY-YYY-YYYY, where Y is a single digit number." +
                  " For example, 06-019-0009.")
      # Make sure parcel ID is a valid parcel
      where = TID_field + " = '" + tid + "'"
      arcpy.MakeTableView_management(parcels, "in_memory/temp", where)
      if arcpy.GetCount_management("in_memory/temp") < 1:
         raise GPException("Cannot find parcel ID " + tid + " in parcel " +
                 "list.")
      arcpy.Delete_management("in_memory/temp")    
      # Finally, add it to the selection if it's passed all the checks
      arcpy.SelectLayerByAttribute_management(parcels, "ADD_TO_SELECTION", where)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
Tags (1)
0 Kudos
11 Replies
JoshuaBixby
MVP Esteemed Contributor

A comment on syntax.  With Make Table View or any of the similar tools, the second field is the name of the layer to be created, just the name.  In your code above, you are using "in_memory/temp", which appears to work, but I suggest against adding "in_memory/" in front of the name for 2 reasons:  1)  it doesn't do anything, i.e., views are already created in memory and adding "in_memory/" doesn't really do anything besides make the name of the view longer, and 2) adding "in_memory/" implies a workspace is needed by the tool when it isn't, which creates readability and potential code maintenance issues.

Karyn_Kerdolff
New Contributor III

Good to know, thank you!

0 Kudos