SQL Server Enterprise Edition Advanced Scanning

Forrards
March 30, 2023

It is widely known that SQL Server Enterprise Edition contains a range of improvements, which under certain conditions allow you to perform operations in a more optimal way in comparison with Standard Edition. One of these improvements is Advanced Scanning, which allows multiple scans to share one physical read from the disk. You can also come across another name of this feature -  Merry-go-Round Scan.

For example, we have a large Table1 consisting of 1 million pages. User A starts executing T-SQL, which requires the table scan. When the 200,000 pages have been scanned User B starts another query, which also requires scanning of Table1. To scan all pages after 200 000th the Database Engine will schedule only one physical read and return the resulting strings to both execution plans. After 500,000 pages have been scanned User C connects and also requests all data from Table1. Now physical scan is divided between 3 queries. Once the scan is over for User A, it will continue for User B and User C, but from the 1st to 200th page for User B and, respectively, to 500 000th for User C.

What’s the point of this optimization? In case you have very large tables that do not fit entirely into the memory and scans result in "washing out" of the buffer pool, and these scans are often performed in parallel, then this optimization will help to reduce load on the buffer pool and do scan multiple times.

To demonstrate this, let's create a sufficiently large table with a clustered index in database AdventureWorks2012.

use
[AdventureWorks2012];
go

select
[DatabaseLogID],
[PostTime],
[DatabaseUser],
[Event],
[Schema],
[Object],
[TSQL],
[XmlEvent]
into [dbo].[BigDatabaseLog]
from [dbo].[DatabaseLog];
go

insert into [dbo].[BigDatabaseLog]
(
[PostTime],
[DatabaseUser],
[Event],
[Schema],
[Object],
[TSQL],
[XmlEvent]
)
select
[PostTime],
[DatabaseUser],
[Event],
[Schema],
[Object],
[TSQL],
[XmlEvent]
from [dbo].[DatabaseLog];
go 200

create clustered index [IX_CL_DatabaseLogID]
on [dbo].[BigDatabaseLog]
(
[DatabaseLogID] asc
) on [PRIMARY];
go

It’s also worth noting that Advanced Scanning works only if there is no requirement to order data. For example, as in the following query execution plan:

QueryPlan1

This optimization demonstrates that the clustered index scan with no requirement to order data can lead to the output of not sorted data. And indeed, if I run the specified query in a few sessions with a pause between starts, it doesn’t guarantee that resulting data will be sorted.

res1
res2
res3

In addition, I should note that I performed this experiment for only scanning clustered index, which does not fit entirely into the buffer pool. I set 400 MB as the maximum size of memory in SQL Server. And in two situations described above this went wrong:

  1. When scanning a heap table (without a clustered index) data were always returned in the same order.
  2. When the whole table is cached in the buffer pool, the results also come back in the same order. But this does not conflict with anything, because there are not heavy read from the disk operations, but only logical read from the buffer pool.

Forrards
March 30, 2023
Share
Recent articles:
Database consultancy: SaaS database considerations
March 31, 2023

Utilizing given approach each client get his own database. In such case every client`s data stays isolated from others. It simplifies backing up the data, installing new clients but causes higher cost of maintenance.

More
Database consultancy: tree-like structure
March 31, 2023

Subject recursion is well covered in the literature, but, nevertheless, the problem of output “tree” does not mean the client and SQL Server many baffled. So, put the problem: there is a table with the name and record id field indicating the parent identifier. Immediately fill in this table, some sort of test data:

More
Database consultancy: Hierarchy in MS SQL Server
March 31, 2023

In our time of greatest prevalence of databases were relational databases, which are the main targets of the tables and the relationships between them. Tables can solve most problems for data storage and manipulation. But in the real world entity requiring storage is not always presented in a tabular form. One of these very common types of data structures other than the table is a tree structure, where each data element is the parent and the offspring. An example of such a structure may be the structure of state enterprises, which is headed by the director (the root of the tree), his deputies, heads of departments from which are subject to certain deputies, employees of departments, which are subject to the rulers.

More

Subscribe to our blog

    

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Database consultancy: SaaS database considerations

Utilizing given approach each client get his own database. In such case every client`s data stays isolated from others. It simplifies backing up the data, installing new clients but causes higher cost of maintenance.

March 31, 2023
Database consultancy: tree-like structure

Subject recursion is well covered in the literature, but, nevertheless, the problem of output “tree” does not mean the client and SQL Server many baffled. So, put the problem: there is a table with the name and record id field indicating the parent identifier. Immediately fill in this table, some sort of test data:

March 31, 2023

Discuss your project with us

    

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.