Simultaneous Access to SQL Server

When the sql server receives two queries (SELECT * From the_Same_Table), exactly at the same time, and if you have a server with multiple processors, can the sql server receive data at the same time?

I'm trying to figure out what happens if a cheap select statement ends within 0.01 seconds and 1000 users run the same query at the same time. I think what happens if the server has four processors, then the SQL server will serve the first four in 0.01 seconds and serve the next four users in 0.02 seconds.

Is it even close to what will actually happen?

I think that I will try to use some kind of code and registrars to test this, or maybe there are reliable testing tools for this.

thanks

+4
source share
3 answers

Each batch (query) included in SQL Server creates a task. The task is scheduled for execution and picked up by the worker. The worker is very similar to a thread. The task remains with the worker until it ends, and then frees the worker to pick up another task. The system runs a limited number of workers configured on sp_configure 'max worker threads' . At a minimum, there are 256 workers, of which about 35 are systems. An employee needs a scheduler to run, and there is one scheduler for each processor core. Employees collaborate in an exchange planner.

In some tasks, sub-tasks arise, such as parallel queries. These tasks are also queued for execution and need to be completed by the employee. Tasks that arise sub-tasks cannot be completed until all the tasks that it runs are completed.

There are also some system tasks, depending on the user's actions, such as a handshake to log into the system. When a client opens a new connector, authentication / authorization authorization and login authorization are performed by a task that requires a worker.

When 1000 requests arrive on the server, 1000 tasks are created and queued for execution. Free workers raise tasks and begin to carry them out. Upon completion of one task, they select the next task until all tasks created by 1000 requests are completed.

DMVs that show what is happening:

These details are described in SQL Server Batch or Task Scheduling and the Glory Blog .

Further, after completing the task, the request will be compiled. Compilation first looks for the query text in memory and looks for an existing compiled plan for the query with an identical plan. You can read my answer Dynamically created SQL vs Parameters in SQL Server for a more detailed study of how this happens. Also see Caching and reusing an execution plan . After creating the plan, it is launched. A query of type SELECT ... FROM table will create a trivial plan in which there are only a few statements that basically extract each row and put it in the TDS stream back to the client. A query plan is a statement tree, and a query is always executed by querying the root of the tree for the next line in the loop until the root returns EOF. Tree query operators become more specific until the bottom operator has physical access to the selected access path (index or heap selected by the optimizer to satisfy the request). See Processing SQL Queries . Access to the index will always request data from the buffer pool, never from disk. When the buffer pool does not have a cached page, PAGEIOLATCH is placed on the page, and the request to read the page is sent to the I / O subsystem. Subsequent requests for the same page will wait for this I / O to complete, and as soon as the page is in the buffer pool, all other requests that need this page will be received from the buffer pool. unused pages are sent when the buffer pool needs free pages, but if the system has enough RAM, the page will never be unloaded after loading. Index and heap scan operations will request a preliminary check, expecting that pages prior to the current page in the page link chain will be requested. Moving forward is limited to fragments of index fragments, and it is when index fragmentation is included in the picture because it reduces the size of read requests forward, see Understanding Pages and Extents .

Another aspect of query execution is logical row locking. For stability, reading can place row locks or range locks depending on the isolation model in the rows it reads, to prevent matching updates when the request passes validation. At the SNAPSHOT isolation level, the request will not request locks at all, but instead a version label will be used to possibly serve the data requested from the version store (see SQL Transaction Isolation Based on Server 2005 Row Version ). In the READ UNCOMMITED section (or when using the nolock hint), the query does not request locks on the lines it reads, but the reads are incompatible if consistent updates occur (unsolicited lines are read, one line can be read twice or an existing line can not be read at all).

+14
source

No, your assumption of sequential processing is incorrect, this question becomes very complex and deep - an attempt to do what I know about it is simple:

Each request has a designated thread, and the scheduler uses collaborative scheduling rather than proactive, so each thread can refuse to allocate (quantize) time before it is assigned by the scheduler. Each request will have to make requests for data from the buffer pool, which may mean waiting in the IO / network, etc. Therefore, they will press the wait states and issue their processing time slices.

When a process no longer waits for a resource, it can re-enter the list of threads available for processing, and then will receive another time slice for processing the request.

In reality, your queries run in parallel, but you cannot deterministically predict the order in which the queries complete.

To get real depth on this, I think that the book, SQL Inner Books 2008, by Adam Mechanic, Kimberley Tripp, Paul Randal, is a good starting point or older book of SQL Server 2000 architecture from Ken Henderson, which was also very low.

I would have to go back to the comments on the course in order to remember the exact process - it comes to the question - "since you cannot directly influence this, why are you asking?"

+2
source

The Sql server is optimized for parallel concurrent reads. The only time you can get stuck is if you have a lot of update operations happening in the same table that you are trying to access. However, if so, you can use nolock or even set the transaction isolation level to READ UNCOMMITTED .

Now regarding your questionnaire. SQL Server uses something called fibers, which look like an auxiliary thread. Thus, you do not necessarily see the same scaling of the processor stream that you expect to see in a multiprocessor environment. Although the SQL server can access a certain number of threads, there is a maximum number of fibers that can also be used. This is why you can have 1000 clients accessing the same server at the same time, even on small boxes.

If you really want to get into the thread / fiber schedule, you will need to find a good SQL server and a tie that really really gets it, as it gets complicated.

Just understand that the sql server is optimized for this, and you do not need to unit test it in any way, as it has already been proven with tools that you probably cannot recreate.

0
source

All Articles