dimas
dimas

Reputation: 2597

Does Oracle cached SQL scripts results or output

I am using Oracle 10G and Oracle SQL developer ver 3. I wanted to know if Oracle stores/caches the result of a query somewhere so that next you execute the same query with the same parameters the time it would take to retrieve the data is lesser than the first time you execute the script.

If it does for how long does it cache those results?

I was trying to execute a select SQL script and after repeating it 3x, the time it takes for it to retrieve the data and display is reduced by almost half. But some other times if I execute the same script on another database it doesn't appear to make any change even if I repeat it several times.

Upvotes: 1

Views: 1784

Answers (2)

Justin Cave
Justin Cave

Reputation: 231661

Oracle caches SQL statements in the library cache. When you execute a statement multiple times (whether or not the same bind variable values are used), Oracle uses the data in the library cache to avoid having to re-parse the SQL statement and generate a new query plan. When you are looking at tuning an individual query, however, it is very unlikely that this caching is meaningfully affecting performance. Parsing a SQL statement and generating the query plan is generally a very quick process.

Oracle also caches data blocks in the buffer cache. So when you execute the query and read a particular block of data in order to get the data for a particular row in the table, that block will be cached. That frequently allows subsequent executions of the same SQL statement with the same set of bind variables to do many fewer physical reads since many more of the blocks that the query needs to read are cached. In general, these blocks are stored in a LRU (least recently used) cache so they will be cached until it ages out because other blocks are more popular (there are exceptions that make the buffer cache something other than a pure LRU cache-- blocks that are read via full table scans are generally put a the end of the cache rather than at the beginning, for example-- but it's generally reasonable to think of it as a LRU cache).

Then you have all the caches outside of Oracle. For example, your operating system will often be configured to cache portions of files that are read so even if a block ages out of Oracle's buffer cache, it may be in the file system cache which would still be much faster than reading the block from disk. Most SANs will also cache recently used data which would allow that data to be retrieved without needing to read it from disk. Generally, it is more efficient if the data is cached in Oracle but it is still much more efficient to retrieve a block from the file system cache or from the SAN cache rather than reading it from a physical drive.

Running a query multiple times in succession will generally get faster because you are forcing more of the blocks you are interested in to be cached somewhere and substantially reducing the amount of physical I/O the system needs to do. Of course, if your queries are not I/O bound or if your queries are against data that is already cached, the difference will be much smaller. It is entirely possible, though, that the same query would benefit substantially from being run multiple times one day and not at all the next day because one day none of the blocks it is interested in were already cached and other sessions and another day all the blocks it is interested in were already cached.

In 11g, you have the option of configuring a result cache that would allow you to cache the results of particular queries not just the blocks that are read in order to produce those results.

Upvotes: 4

antlersoft
antlersoft

Reputation: 14786

Oracle normally caches every page it reads from the disk, and will try to hold that information for as long as it can (it's basically an MRU cache). This is a very important consideration when trying to measure the performance of database operations; often subsequent executions of a query will be much faster than the first.

If the query requires very little I/O, or more than will fit in the cache, or if the cache is being heavily used by other users, the caching might not be noticeable.

Note that query plans are also cached, and this can also affect the observed performance of queries.

Upvotes: 1

Related Questions