Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is 'physical reads' less than 'read-ahead reads' & 'logical reads' in SQL Server for first time execution of query?

I restarted SQL Server 2005 and ran a query with statistics IO on.

I got these results: Table 'xxx'. Scan count 1, logical reads 789, physical reads 3, read-ahead reads 794, ...

read-ahead reads is the number of pages read and placed in the cache. Shouldn't then the physical reads at least be the same number?

Also because it's a first run for the query, shouldn't logical reads be 0? Or is SQL Server reading the pages physically, putting them in the cache and then reading from the cache? Why is there a count for logical reads when the cache should be empty?

like image 545
Tony_Henrich Avatar asked Apr 19 '11 20:04

Tony_Henrich


People also ask

What is the difference between physical reads and logical reads in SQL Server?

A logical read occurs every time the Database Engine requests a page from the buffer cache. If the page is not currently in the buffer cache, a physical read first copies the page from disk into the cache.

What are physical reads?

The physical reads Oracle metric is the total number of data blocks read from disk. This equals the number of "physical reads direct" plus all reads into buffer cache. Other notes on physical reads includes: Oracle Physical Reads Segment Statistics.

What is physical reads and logical reads in Oracle?

When a block is requested by a query, Oracle looks for it in the Buffer Cache and if found, it results in a Logical read and if it does not find the Block in there it results in a physical read (disk I/O).

How avoid physical reads in SQL Server?

So indexes are great for reducing reads because they allow us to store only the data that is needed for a specific query (both as key columns and included columns). Fewer columns = greater density = fewer reads necessary.


2 Answers

All reads are logical reads.

Some also incur physical reads and read ahead reads in addition to bring pages into cache.

Presumably it read ahead a few pages that were never actually required by your query hence the fact that this number is slightly larger than the actual logical reads.

like image 183
Martin Smith Avatar answered Sep 23 '22 13:09

Martin Smith


The docs from MS say that "Read-Ahead Reads" are reading pages into the cache. That's not very helpful... one would assume that "Physical Reads" also place their resulting page into the cache.

My re-wording of what MS is saying is:

  • Physical Read - The query is blocked waiting for the page to be read from disk into the cache for immediate use.
  • Read-Ahead Read - The page is being read before it blocks the query and is read into the cache as are all reads. Read-Aheads are possible when you are scanning an index, in which case the next leaf pages in the index can assume to be needed and the read can be initiated for them before the query actually says it needs them. This allows the disk to be busy while the db engine is examining the contents of previously fetched pages.

I can't find them saying that anywhere, but read-ahead would be pointless if that's not what they were really trying to say.

like image 21
huntharo Avatar answered Sep 22 '22 13:09

huntharo