使用 Python 脚本更新地理数据库系统表的统计数据
DBMS 优化器使用数据库统计数据为正在执行的查询选择最优执行计划。您应该在表内容发生重大变化之后更新统计数据。对于 ArcSDE 地理数据库系统表,您应该在地理数据库添加新表或要素类、执行大量版本化编辑或压缩地理数据库之后更新统计数据。
由于更新数据库统计数据可能是 I/O 密集型操作,您可能希望在大多数用户从数据库注销时执行此操作。您可以编写 Python 脚本调用“分析数据集”工具来更新 ArcSDE 地理数据库系统表中的统计数据并安排该脚本在夜间运行。您可以使用 Windows 中的“计划任务”来设置运行脚本的时间。在 Linux 中,可以设置 cron 作业来运行该脚本。
步骤:
-
将以下脚本之一复制到已安装 Python 和下列 ArcGIS 客户端之一的计算机:
- ArcGIS for Desktop(Standard 或 Advanced)
- 具有 Geodatabase Update 扩展模块的 ArcGIS Engine
- ArcGIS Runtime
- ArcGIS for Server(标准版或高级版)
使用特定于您的站点的信息修改脚本。
以下脚本使用本地计算机上现有的数据库连接文件连接到数据库并运行脚本:
# Name: UStatsSysTbls.py # Description: Updates statistics on enterprise geodatabase # system tables using an existing .sde file. # Author: Esri # Import system modules import arcpy, os # set workspace workspace = arcpy.GetParameterAsText(0) # set the workspace environment arcpy.env.workspace = workspace # Execute analyze datasets for system tables arcpy.AnalyzeDatasets_management(workspace, "SYSTEM", "", "","","") print "Analyze Complete"
以下示例脚本包含连接到 Oracle 数据库以更新系统表中的统计数据所需的信息:
# Name: UStatsSysOracle.py # Description: Updates statistics on system tables in an enterprise geodatabase in Oracle. # Author: Esri # Import system modules import sys import arcpy import os # Provide connection information server = servername service = "5151 | sde:oracle:sid" account_authentication = OPERATING_SYSTEM_AUTH | DATABASE_AUTH #Leave username and password blank if using OPERATING_SYSTEM_AUTH username = gdb_admin_user_name password = gdb_admin_password version = sde.DEFAULT # Set local variables if os.name.lower() == "nt": slashsyntax = "\\" if os.environ.get("TEMP") == None: temp = "c:\\temp" else: temp = os.environ.get("TEMP") else: slashsyntax = "/" if os.environ.get("TMP") == None: temp = "/usr/tmp" else: temp = os.environ.get("TMP") Connection_File_Name = temp + slashsyntax + "connection.sde" # Check for the .sde file and delete it if present if os.path.exists(Connection_File_Name): os.remove(Connection_File_Name) #Variable defined within the script; other variable options commented out at the end of the line saveUserInfo = "SAVE_USERNAME" #DO_NOT_SAVE_USERNAME saveVersionInfo = "SAVE_VERSION" #DO_NOT_SAVE_VERSION print "Creating ArcSDE Connection File..." # Create ArcSDE Connection File # Usage: out_folder_path, out_name, server, service, database, account_authentication, username, password, save_username_password arcpy.CreateArcSDEConnectionFile_management(temp, "connection.sde", server, service, account_authentication, username, password, saveUserInfo, version, saveVersionInfo) # Update statistics on system tables arcpy.AnalyzeDatasets_management(Connection_File_Name, "SYSTEM","","","","") print "Analyze Complete"
以下示例脚本包含使用操作系统验证的 dbo 用户连接到 SQL Server 并更新系统表统计数据所需的信息:
# Name: UStatsSysSqlServer.py ## Description: Updates statistics on system tables in an enterprise geodatabase in SQL Server. # Author: Esri # Import system modules import sys import arcpy import os # Provide connection information server = servername service = "5151 | sde:sqlserver:sqlserver_instance" database = database_name account_authentication = OPERATING_SYSTEM_AUTH | DATABASE_AUTH #Leave username and password blank if using OPERATING_SYSTEM_AUTH username = gdb_admin_user_name password = gdb_admin_password version = sde.DEFAULT # Set local variables if os.name.lower() == "nt": slashsyntax = "\\" if os.environ.get("TEMP") == None: temp = "c:\\temp" else: temp = os.environ.get("TEMP") else: slashsyntax = "/" if os.environ.get("TMP") == None: temp = "/usr/tmp" else: temp = os.environ.get("TMP") Connection_File_Name = temp + slashsyntax + "connection.sde" # Check for the .sde file and delete it if present if os.path.exists(Connection_File_Name): os.remove(Connection_File_Name) #Variable defined within the script; other variable options commented out at the end of the line saveUserInfo = "SAVE_USERNAME" #DO_NOT_SAVE_USERNAME saveVersionInfo = "SAVE_VERSION" #DO_NOT_SAVE_VERSION print "Creating ArcSDE Connection File..." # Create ArcSDE Connection File # Usage: out_folder_path, out_name, server, service, database, account_authentication, username, password, save_username_password arcpy.CreateArcSDEConnectionFile_management(temp, "connection.sde", server, service, database, account_authentication, username, password, saveUserInfo, version, saveVersionInfo) # Update statistics on system tables arcpy.AnalyzeDatasets_management(Connection_File_Name, "SYSTEM","","","","") print "Analyze Complete"
在以下示例中,sde 用户连接到 PostgreSQL 数据库:
# Name: UStatsSyspg.py # Description: Updates statistics on system tables in an enterprise geodatabase in PostgreSQL. # Author: Esri # Import system modules import sys import arcpy import os # Provide connection information server = servername service = "5151 | sde:postgresql:servername" database = database_name account_authentication = DATABASE_AUTH username = gdb_admin_user_name password = gdb_admin_password version = sde.DEFAULT # Set local variables if os.name.lower() == "nt": slashsyntax = "\\" if os.environ.get("TEMP") == None: temp = "c:\\temp" else: temp = os.environ.get("TEMP") else: slashsyntax = "/" if os.environ.get("TMP") == None: temp = "/usr/tmp" else: temp = os.environ.get("TMP") Connection_File_Name = temp + slashsyntax + "connection.sde" # Check for the .sde file and delete it if present if os.path.exists(Connection_File_Name): os.remove(Connection_File_Name) #Variable defined within the script; other variable options commented out at the end of the line saveUserInfo = "SAVE_USERNAME" #DO_NOT_SAVE_USERNAME saveVersionInfo = "SAVE_VERSION" #DO_NOT_SAVE_VERSION print "Creating ArcSDE Connection File..." # Create ArcSDE Connection File # Usage: out_folder_path, out_name, server, service, database, account_authentication, username, password, save_username_password arcpy.CreateArcSDEConnectionFile_management(temp, "connection.sde", server, service, database, account_authentication, username, password, saveUserInfo, version, saveVersionInfo) # Update statistics on system tables arcpy.AnalyzeDatasets_management(Connection_File_Name, "SYSTEM","","","","") print "Analyze Complete"
在以下示例中,sde 用户连接到 DB2 数据库:
# Name: UStatsSysDb2.py # Description: Updates statistics on system tables in an enterprise geodatabase in DB2. # Author: Esri # Import system modules import sys import arcpy import os # Provide connection information server = servername service = "5151 | sde:db2" database = db_alias account_authentication = OPERATING_SYSTEM_AUTH | DATABASE_AUTH #Leave username and password blank if using OPERATING_SYSTEM_AUTH username = gdb_admin_user_name password = gdb_admin_password version = sde.DEFAULT # Set local variables if os.name.lower() == "nt": slashsyntax = "\\" if os.environ.get("TEMP") == None: temp = "c:\\temp" else: temp = os.environ.get("TEMP") else: slashsyntax = "/" if os.environ.get("TMP") == None: temp = "/usr/tmp" else: temp = os.environ.get("TMP") Connection_File_Name = temp + slashsyntax + "connection.sde" # Check for the .sde file and delete it if present if os.path.exists(Connection_File_Name): os.remove(Connection_File_Name) #Variable defined within the script; other variable options commented out at the end of the line saveUserInfo = "SAVE_USERNAME" #DO_NOT_SAVE_USERNAME saveVersionInfo = "SAVE_VERSION" #DO_NOT_SAVE_VERSION print "Creating ArcSDE Connection File..." # Create ArcSDE Connection File # Usage: out_folder_path, out_name, server, service, database, account_authentication, username, password, save_username_password arcpy.CreateArcSDEConnectionFile_management(temp, "connection.sde", server, service, database, account_authentication, username, password, saveUserInfo, version, saveVersionInfo) # Update statistics on system tables arcpy.AnalyzeDatasets_management(Connection_File_Name, "SYSTEM","","","","") print "Analyze Complete"
在以下示例中,sde 用户连接到 Informix 数据库:
# Name: UStatsSysIDS.py # Description: Updates statistics on system tables in an enterprise geodatabase in Informix IDS. # Author: Esri # Import system modules import sys import arcpy import os # Provide connection information server = servername service = "5151 | sde:informix" database = odbc_dsn account_authentication = OPERATING_SYSTEM_AUTH | DATABASE_AUTH #Leave username and password blank if using OPERATING_SYSTEM_AUTH username = gdb_admin_user_name password = gdb_admin_password version = sde.DEFAULT # Set local variables if os.name.lower() == "nt": slashsyntax = "\\" if os.environ.get("TEMP") == None: temp = "c:\\temp" else: temp = os.environ.get("TEMP") else: slashsyntax = "/" if os.environ.get("TMP") == None: temp = "/usr/tmp" else: temp = os.environ.get("TMP") Connection_File_Name = temp + slashsyntax + "connection.sde" # Check for the .sde file and delete it if present if os.path.exists(Connection_File_Name): os.remove(Connection_File_Name) #Variable defined within the script; other variable options commented out at the end of the line saveUserInfo = "SAVE_USERNAME" #DO_NOT_SAVE_USERNAME saveVersionInfo = "SAVE_VERSION" #DO_NOT_SAVE_VERSION print "Creating ArcSDE Connection File..." # Create ArcSDE Connection File # Usage: out_folder_path, out_name, server, service, database, account_authentication, username, password, save_username_password arcpy.CreateArcSDEConnectionFile_management(temp, "connection.sde", server, service, database, account_authentication, username, password, saveUserInfo, version, saveVersionInfo) # Update statistics on system tables arcpy.AnalyzeDatasets_management(Connection_File_Name, "SYSTEM","","","","") print "Analyze Complete"
- 修改脚本以包含连接信息后,可安排脚本在每晚特定的时间运行。
- 在 Windows 中,从控制面板打开“计划任务”并使用向导添加计划任务。当系统询问要运行哪个程序时,浏览至您的 Python 脚本。
- 在 Linux 中,创建一个 cron 文本文件,其中包含希望脚本运行的日期和时间信息,然后使用 crontab 程序将该文件加载到 cron 中。
例如,以下信息将 Python 脚本(名为 ustatssysids.py)设置为在每个星期六凌晨 1:30 运行:
30 1 * * 6 /usr/bin/ustatssysids.py
有关 cron 的使用信息,请参阅随 Linux 安装提供的 Linux 手册页。
5/10/2014