天天看點

怎麼對ArcSDE資料庫的要素類進行批量重建空間索引

在我們遇到很多有關于性能的問題,我們一般建議使用者重新常見空間索引,那麼如果使用者一個庫裡面有幾十個甚至上百個空間索引,那麼該怎麼處理呢?

ArcGIS10.1版本

RebuildIndexes:(這個功能隻有ArcGIS10.1才有的)

       這個功能主要是對使用者進行大範圍資料編輯,在原有資料基礎上做大量的資料加載或者資料删除後,為了提高資料性能,進行的操作。其實聽到這裡有點老生常談,我們原來的方法也是一個圖層一個圖層的進行重建索引,但是這個功能可以批量的重建索引,而且及支援屬性索引也支援空間索引,支援系統表以及版本的增量表的重建索引,比較友善。

怎麼對ArcSDE資料庫的要素類進行批量重建空間索引

ArcGIS10之前的版本

但是對ArcGIS10以及之前的版本并沒有批量重建索引的辦法,我們隻能另辟蹊徑了。

1:使用sde指令行的方法

我們可以使用sde指令,load_only_io和normal_io進行切換來進行空間索引的重建,具體請看幫助。

Switch between load only and normal I/O modes.
To modify a feature class's input/output mode, use the load_only_io and normal_io operations.

You must be the owner of the feature class to change it from normal I/O to load-only I/O mode.

It is recommended that you do not place a versioned feature class that uses binary storage in load only I/O mode because, when you switch back to normal I/O mode, the spatial index will be calculated on a versioned representation of the features. This representation may not match what is stored in the nonversioned f table to which the index gets applied. If this is the case, an error is returned.

When the sdelayer command is used to create a layer (i.e., if the register or add operations are used), the resultant feature class is automatically in normal I/O mode. The load-only I/O mode is provided to make bulk data loading processes more efficient. Use load-only mode when performing large inserts to avoid the continuous update of the feature class's indexes.

For feature classes that use a spatial grid index (SDEBINARY, SDELOB, WKB_GEOMETRY, or feature classes in DB2), if the grid fields are updated while the feature class is in load-only I/O mode, the spatial index is rebuilt with the new grid sizes when you reset the feature class to normal I/O mode. While rebuilding the spatial index table, the feature class is inaccessible to other users. Note: You can change the grid sizes while the feature class is in normal or load-only I/O mode. If you reset spatial indexes while the feature class is in normal I/O mode, the indexes on the spatial index table are dropped while the spatial index is being re-created.. 

When the feature class is in normal I/O mode, the envelope is automatically updated whenever a feature that extends the current envelope is added. The envelope is not updated while the feature class is in load-only I/O mode but is recalculated to the full extent when the feature class is reset to normal I/O mode.

These examples show the parcels feature class being moved into load only mode then back to normal I/O mode.

sdelayer -o load_only_io -l victoria,parcels -u av -p mo -i esri_40

sdelayer -o normal_io -l victoria,parcels -u av -p mo -i esri_40

When the feature class is returned to normal I/O mode, the spatial index table and database indexes are rebuilt. If the operation does not complete successfully for any reason, the feature class is left in load-only I/O mode.

When a feature class is in load-only I/O mode, the unique index is removed from the feature class's spatial column. When the index is absent, it is possible to enter nonunique values into the spatial column with an application not created with the ArcSDE C- or Java application programming interface (API). Therefore, no applications besides ArcSDE or applications created with the ArcSDE C- or Java API should ever update the spatial column. Database administrators should be aware of the increased vulnerability of the spatial column when the feature class is in load-only I/O mode.
           

我知道很多朋友肯定沒有認真看上面的英文解釋,但是沒有關系,大家隻需要知道,如果我們使用load_only_io模式,就是删除空間索引,我們使用normal_io模式就是建立空間索引就可以了。

那麼知道了這兩個模式,我們在對該模式做一個延伸介紹

注意:一般情況下,如果我們業務有變更的情況,比如我們插入一條記錄,如果是使用ArcGIS用戶端或者相關API進行操作,除了我們新添加一條記錄外,我們還同步的對空間索引進行更新,但是如果我們業務上有批量更新的情況,那麼我們除了變更資料表,而且我們還要同步的批量變更空間索引資訊,這樣對性能會有一些影響。那麼我們就可以在批量變更之前,将資料切換到load_only_io模式,然後進行批量變更,等變更業務徹底完成之後,再切換到normal_io模式,以達到重建索引的目的。

那麼對資料量比較多的要素類,我們可以使用sde指令編寫批處理檔案來對資料進行批量建立空間索引

@echo OFF
pause "按任意鍵開始"
sdelayer  -o load_only_io -l quxian,shape -i 5151 -s 192.168.220.165 -u sde -p sde
echo "圖層quxian已經删除了空間索引"
sdelayer  -o normal_io -l quxian,shape -i 5151 -s 192.168.220.165 -u sde -p sde
echo "圖層quxian已經建立了空間索引"

...

sdelayer  -o load_only_io -l quxian1,shape -i 5151 -s 192.168.220.165 -u sde -p sde
echo "圖層quxian1已經删除了空間索引"
sdelayer  -o normal_io -l quxian1,shape -i 5151 -s 192.168.220.165 -u sde -p sde
echo "圖層quxian1已經建立了空間索引"


pause "按任意鍵結束"
           

将以上資料儲存為.bat檔案,用的時候直接運作即可。

2:使用python腳本重建索引

如果大家留心會發現一個問題,雖然上面使用sde指令可以對要素類進行批量重建索引,但是它仍然需要使用者指定特定的要素類名稱,那麼如果及時個甚至上百個要素類,對使用者來說仍然是一個夢魇,那麼我們使用python腳本就可以很輕松的實作這個功能。

我們使用Python腳本可以對某個SDE連接配接下的資料集或者要素類進行周遊,然後對周遊的要素類使用GP工具,該GP工具可以删除和重建空間索引,就是這麼簡單

from arcpy import *

env.workspace=r'Database Connections\Connection to 192.168.100.111.sde'
        
for dataset in ListDatasets():
    for fc in ListFeatureClasses("","ALL",dataset):
        RemoveSpatialIndex_management(fc)
        AddSpatialIndex_management(fc)
           

執行過程中,我們可以看到右邊的資訊框的執行過程。

 -------------------------------------------------------------------------------------------------------

版權所有,文章允許轉載,但必須以連結方式注明源位址,否則追究法律責任!

-------------------------------------------------------------------------------------------------------