|
发布日期:2007-5-24
作 者:微软
摘 要:如何提高ACCESS的查询性能
正 文:当我们为了提高access查询性能时,可通过以下方法来实现.
一、代码部分
要提高查询性能, 请尝试下列操作:
•
压缩数据库
压缩数据库时, 能够加速查询。 压缩数据库, 时记录对表是重组以便记录位于相邻数据库页, QUALIFIER 是表主键。 因为只有最小数量的数据库页现在有要读取以检索记录, 如果想这改善了顺序扫描表中的记录性能。 压缩数据库, 后运行每个查询以编译查询以便每个查询将现在具有表更新统计。
•
索引字段
索引是用于设置查询字段和索引字段条件两边联接的任何字段。 或者, 创建这些字段间关系。 外键上如果一个不存在当您创建关系, Jet 数据库引擎创建索引。 否则, Jet 数据库引擎使用现有索引。
注意 : 则Jet 数据库引擎自动优化如果 Access 表很小并且如果联接字段已经索引联接查询该查询联接硬盘上 Access 表和 ODBC 服务器表。 可以通过从服务器请求需要记录此时, Access 提高性能。 请确保根据联接字段, 从不同来源加入表索引。
•
选择小是适当数据类型
表, 中定义字段时选择小数据类型是适合字段中数据。 Also, make sure that fields that you plan to use in joins have the same data types or compatible data types, such as Autonumber and Number (if the FieldSize property is set to Long Integer).
•
仅添加字段, 必须
当您创建查询, 只添加字段, 必须。 用于设置条件, 字段中单击以清除 显示 复选框如果您不希望以显示这些字段。
•
SQL 语句另存为查询
If the RecordSource property for a form or for report is set to an SQL statement, save the SQL statement as a query and then set the RecordSource property to the name of the query.
•
避免计算字段
避免查询中计算字段。 如果将一个包含计算字段到另一个查询, 查询可能会影响性能顶级查询中计算字段中表达式。 在以下示例, 用于查询 Q 2 用作查询 Q 1: 输入
Q 1: SELECT IIF([MyColumn]="Yes","Order Confirmed","Order Not Confirmed") AS X FROM MyTable; Q 2: SELECT * FROM Q 1 WHERE X = " 订单已确认 " ; 因为 Q 1 中 IIF 表达式能优化, Q 2 也能优化。 如果在子查询, 嵌套表达式能优化, 能优化所有查询。
一个替代方法来构造查询是如下:
Q 1: SELECT * FROM MyTable WHERE MyColumn = " 是 " ; 如果表达式所需输出, 中尝试置于表达式控件在窗体或报表上。 For example, you can change the previous query to a parameter query that prompts for the value of MyColumn, and then base a form or a report on the query. On the form or on the report, you can then add a calculated control that displays "Hello" or "Goodbye," depending on the value that is in MyColumn.
构造查询如下:
PARAMETERS [ 要看到确认订单, 请输入 Yes。 要查看确认订单, 输入 ] " 否 "。 文本; SELECT * FROM MyTable WHERE MyColumn = [ 要看到确认订单, 请输入 Yes。 要查看确认, 订单输入 ] " 否 "。; 请在窗体或报表, 上计算控件中键入:
= IIF([MyColumn]="yes","order confirmed","order not confirmed")
•
指定分组依据
按, 联接字段中值分组记录时为正在作为域, 您将汇总上 (计算聚合) 相同的表字段指定 GroupBy。 For example, in the Northwind.mdb sample database, if you create a query that totals the Quantity field in the Order Details table and then groups by OrderID, you can specify Group By for the OrderID field in the Order Details table. If you specify Group By for the OrderID field in the Orders table, Access must join all the records first and then perform the aggregate, instead of performing the aggregate and then joining only the required fields.
分组为提高速度, 在尽可能使用尽可能少的字段上。 如果可另外, 使用 首 函数。
请如果总计查询包含联接, 考虑分组在一个查询记录, 然后将该查询添加单独的查询执行联接。 以某些这时, 可能是提高性能。
•
避免限制性的查询条件
如果可避免限制性的查询条件在计算字段和非索引字段上。 使用条件表达式, 可优化。
•
表之间联接中用于字段中测试您的性能
If you use criteria to restrict the values in a field that is used in a join between tables with a one-to-many relationship, test whether the query runs faster with the criteria placed on the "one" side or on the "many" side of the join. 通过条件添加到位于 " 一 " 方是 " 多 " 方的联接上代替联接字段某些查询, 中您可能意识提高性能。
•
索引排序字段
索引字段, 使用进行排序。
•
使用生成表查询来创建表
如果您数据很少更改, 使用生成表查询来从查询结果创建表。 作为基础使用生成表查询代替对于窗体、 报表, 或其他查询。 请确保您添加索引以根据准则, 阅读本文。
•
避免使用域聚合函数
避免使用域合计函数, 如 DLookUp 函数以从表, 查询中不是访问数据。 域聚合函数是专用于 Access, 也就是说 Jet 数据库引擎无法优化使用域聚合函数的查询。 相反, 查询添加到表, 此函数正在访问或创建查询。
•
使用固定列标题
如果要创建交叉表查询, 应尽量使用固定列标题。
•
使用运算符
使用 Between...And 运算符, In 运算符, = 运算符对索引字段并。
•
优化服务器上
用于对 ODBC 数据源, 批量更新查询通过 FailonError 属性设置为 Yes 优化服务器上性能。
|