python - Oracle 11g - 即使有 NOCACHE 提示,查询似乎也会缓存

标签 python oracle oracle11g cx-oracle

我正在使用 cx_Oracle 模块在 Python 中进行一些数据库基准测试。为了对结果进行基准测试,我运行了 150 个独特的查询并为每个查询计时。我正在运行这样的东西:

c = connection.cursor()
starttime = time.time()
c.execute('SELECT /*+ NOCACHE */ COUNT (*) AS ROWCOUNT FROM (' + sql + ')')
endtime = time.time()
runtime = endtime - starttime

每个查询都通过变量 sql 传递,它们在长度、运行时间和它们访问的表方面有很大差异。也就是说,所有查询都表现出以下行为:

第一次运行非常慢(相对)

第 2 次运行:明显更快(花费 1/2 - 1/5 的时间)

第 3 次运行:比第 2 次运行略快

所有后续运行 >= 4: 大约等于第 3 次运行

我需要禁用缓存才能获得准确的结果,但前几次运行确实丢失了我的数据;就好像 NOCACHE 根本不起作用...这是怎么回事?

编辑:Allan 回答了我的问题,但对于任何可能感兴趣的人,我做了更多的研究,发现了这两页,它们也很有帮助:

How to clear all cached items in Oracle

http://www.dba-oracle.com/t_flush_buffer_cache.htm

最佳答案

来自documentation :

The NOCACHE hint specifies that the blocks retrieved for the table are placed at the least recently used end of the LRU list in the buffer cache when a full table scan is performed. This is the normal behavior of blocks in the buffer cache.

从这里看来,nocache 提示根本没有按照您的预期进行。

您可以通过运行 ALTER SYSTEM FLUSH SHARED_POOL 清除共享缓存,通过运行 ALTER SYSTEM FLUSH BUFFER_CACHE 清除缓冲区缓存。您需要在每个查询之间执行此操作以防止使用缓存。

关于python - Oracle 11g - 即使有 NOCACHE 提示,查询似乎也会缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33446459/

相关文章:

python - 如何将参数从鼠标事件传递到函数?

python - 迭代 Python 列表,保留嵌入列表的结构

python - 良好的性能

Oracle BINARY_FLOAT : 2 integers give same value?

python - scipy 将一个稀疏矩阵的所有行附加到另一个

oracle - 如何在 Haskell 中连接到 Oracle DB

sql - 将条件合并到一个地方

database - Oracle 数据库表行数

sql - 如何在另一个 SQL 语句中使用输出

sql - 在oracle查询中对价格范围进行排序