Select to view content in your preferred language

Automate Rebuild Spatial Index

2901
11
10-18-2022 09:40 PM
ManishGohil
Occasional Contributor

To reduce unusual Memory and CPU consumption we rebuild a spatial index  of feature class by following steps in below URL.
Is there any way to automate these steps through any tool or services or anything?

URLhttps://pro.arcgis.com/en/pro-app/latest/help/data/geodatabases/overview/modifying-a-spatial-index.h...

11 Replies
AmeliaBradshaw
Esri Contributor

Hello Manish, I saw that you had posted this as both an Idea and a question. This really fits better into the Question space, so I moved the Idea to the ArcGIS Pro Questions board. I now see you had already posted a question on this! 

I have a question for you first. Are you working in an enterprise geodatabase? If so, you can use this tool: Rebuild Indexes (Data Management). If you are working in a different type of workspace, you could the tool Add Spatial Index (Data Management). This tool not only creates a spatial index, but can recreate an existing one as well. 

Let me know if you have further questions! 

0 Kudos
ManishGohil
Occasional Contributor

@AmeliaBradshaw - Thank you for your response. We are using Enterprise Geodatabase. We will try to use the rebuild indexes tool and try to schedule it.

0 Kudos
RandyCasey
Frequent Contributor

Depending on your definition of "automation", you have a couple of options. If you are looking for full automation, where the process runs on a particular date/time, a Python script set up to run from Windows Task Manager is probably your best bet. It's not an easy setup though and depending on your environment, you may have to deal with database locks due to versioning, stopping and starting ArcGIS Server services, etc. If you are looking for a semi-automated process, where you run a tool on a routine basis, you can use Model Builder to build a process. That may allow you to work with database locks and versioning a bit more easily, but it will still take some work.

0 Kudos
ManishGohil
Occasional Contributor

@RandyCasey Thank you for your response. We are looking for a process that runs on a particulate date and time. Do we have to restart services after rebuilding indexes? We have to do it for 5 feature classes only.

0 Kudos
RandyCasey
Frequent Contributor

I have never needed to restart a service, but I have needed to take services offline in order to release database locks, which will stop an index rebuild.

0 Kudos
PrestonEllison
Occasional Contributor

@RandyCasey 

When we first started rebuilding/analyzing data sets i set up a little model builder and it worked successfully. After some point the model threw the "cannot acquire a lock" error and stopped working. We are able to run the GP tool without an issue and we have recreated the model but still run into the error. Any idea what is happening here?

0 Kudos
RandyCasey
Frequent Contributor

Well, I would need a little more info, I'm afraid. For instance, are you running the Model Builder against a file GDB or an Enterprise GDB? In either scenario, are you using them as part of a published service? If EGDBs: is it a multi-user environment?

In my experience, when you run into an issue where running a stand-alone GP tool will work, but fails in automation, it's generally due to a conflict in your sequence of operations; i.e., you run an Append GP Tool, then try to rebuild the spatial index, but the Append locked the GDB for edit and has not released the lock yet, so the spatial index tool fails because it also needs an exclusive edit lock. I've run into this kind of scenario most often when I have ArcGIS Pro open and running, and not so much when I am running a Python script from an external IDE. This is due to ArcGIS Pro holding GDB/Feature Classes in memory (presumably to make them more accessible) and until the stack refreshes, the locks persist. It is possible, with a bit of work, to run a Model Builder from an external Python script, which may be a work-a-round to scenarios like the one I described above.

Full disclosure: I don't work with Model Builder very often, although I do have uses for it when I need it. I am primarily a Python scripter for my automation needs, which I have found to be much more reliable for automation than Model Builder, as running a custom GP Tool seems to just work more consitently. With functions like arcpy.da.editor, that open and close edit sessions, it allows more flexibility in my sequence of operations. It would be really great if ESRI would build a GP Tool for that function, so users could add it to a Model Builder, but I digress.

0 Kudos
PrestonEllison
Occasional Contributor

Thanks for the response. Yes this is an Enterprise GDB/branch versioned. I also run this model (use to at least) from an empty instance of pro with no feature classes loaded. I do have the enterprise actively connected though. 

I dont have a great working knowledge of python, i can open existing code and monkey with it a bit to fit my connections/parameters but it still is pretty foreign to me. Do you see any issues with exporting the model builder to a python script and running it from there? Ultimately i would like to set up a windows task scheduler.

0 Kudos
RandyCasey
Frequent Contributor

This is just a cursory guess, but I am working from my extensive experience here: your issue is due to the fact you are running your model against a branched EGDB dataset. Whether SQL or Oracle, your database is locking each time it is being accessed, even if it's just doing a select query. ArcGIS Pro is holding that EGDB in its memory stack with a lock and that lock will persist so long as the ArcGIS Pro session is active. In my decade of experience, there is no effortless way around an EGDB lock; you must either temporarily remove versioning while running the model, or you'll have to develop a Python/arcpy solution using arcpy.da.Editor. If you are looking to use Windows Task Scheduler, you will have to have a Python script to run, there just isn't a simpler solution that I know of. I know these are not the answers you were looking for, but in my expert opinion, they're the only ones I can see available to you.

Yes, you can output your Model Builder to a Python script, but it probably will not work as you expect right out of the box and will need some tweaks to get it working. You will still have to deal with your DB lock issue, even if you are not using ArcGIS Pro, which will involve adding either something to remove versioning (not my first option), or control the edit locks with arcpy.da.Editor (the better option). This is because you are dealing with an EGDB, which has it's own independent rules that you have to work around.

One benefit of outputting your Model Builder to code though, is that you get to see how a Python script is formatted using the GP tools you are already using. That's actually how I leared to use Python and arcpy over a decade ago. I would highly recommend investing some time in taking the free Python web courses offered by ESRI. A few hours of your time learning these skills could save you hundreds of hours down the road.

0 Kudos