database - 使用 Oracle 提示 "FIRST_ROWS"提高 Oracle 数据库性能

标签 database oracle performance query-hints

我有一个在 Oracle 数据库服务器上运行的语句。该语句有大约 5 个连接,那里没有什么异常。它看起来很像下面:

SELECT field1, field2, field3, ...  
FROM table1, table2, table3, table4, table5  
WHERE table1.id = table2.id AND table2.id = table3.id AND ...  
      table5.userid = 1

问题(有趣的是)是 userid = 1 的语句需要 1 秒才能返回 590 条记录。 userid = 2 的语句大约需要 30 秒才能返回 70 条记录。

我不明白为什么差异这么大。

似乎为userid = 1的语句选择了不同的执行计划,为userid = 2选择了不同的执行计划。

在我实现 Oracle Hint FIRST_ROW 之后,性能明显提高。两个语句(对于 id 1 和 2)都在 1 秒内产生返回值。

SELECT /*+ FIRST_ROWS */
       field1, field2, field3, ...  
FROM table1, table2, table3, table4, table5  
WHERE table1.id = table2.id AND table2.id = table3.id AND ...  
      table5.userid = 1

问题:

  1. 当 userid = 2(未使用 hint 时)性能不佳的可能原因是什么?
  2. 为什么一条语句和另一条语句的执行计划不同(当未使用提示时)?
  3. 在决定将此提示添加到我的查询时,有什么我应该注意的吗?

最佳答案

1) What are possible reasons for bad performance when userid = 2 (when hint is not used)?

因为 Oracle 认为使用来自 (userid=1) 的计划的中间结果集之一将非常大 - 可能是错误的。

2) Why would execution plan be different for one vs another statement (when hint is not used)?

基于直方图的索引

3) Is there anything that I should be careful about when deciding to add this hint to my queries?

只要返回的记录数量很少,这个提示就应该是故障安全的——与插入优化器使用特定索引不同,这种方法允许 Oracle 在索引发生变化时选择不同的计划。

关于database - 使用 Oracle 提示 "FIRST_ROWS"提高 Oracle 数据库性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10981505/

相关文章:

c# - MySQL - 升级主键?

php - 奇怪的 php mysql 变量类型问题

c++ - 如何在 Windows 平台上从原生 C++ 处理 SQL Server?

sql - 如何在同一个 SQLPLUS INSERT INTO 语句中同时使用 SELECT 查询和 RETURNING

c++ - 为什么将 std::endl 与 ostringstream 一起使用会影响输出速度?

python - 如何一次性执行多条SQL语句?

sql - Oracle 作业 - LPAD/RPAD 错误?

r - 按升序/降序快速对 data.table 进行排序

java - 日志记录类名、方法名和行号的性能影响

sql - 索引是否与 oracle 中的组功能一起使用?