You can optimize database queries, upgrade the database server, but still not enough to guarantee good database performance for your application. This is because the problem may arise from concurrent (synchronous) query operations in the system. In fact, in a production database system, The problem that can be caused by the simultaneous execution of a query is that no amount of query optimization can be solved. We have the most optimized and fastest query, but if these queries do not get the required data for concurrency problems, the optimization is not done adequately.
Fundamentally, we must make sure that the database query and the connection that executes the query must be able to have no errors (such as deadlocks or update conflicts) in concurrent execution. And can be in the acceptable waiting time to get results. If not, locate the concurrency problem and find out why: It is correct to run the query independently, and the Concurrency Runtime is problematic .
The process of solving concurrency problems is a process based on experience. It is not easy to mimic a product environment, and it is difficult to predict the behavior of concurrent queries from many synchronous connections. This is why the system state must be kept constantly in order to diagnose the cause of the problem when the concurrency problem occurs.
The concurrency problem in most cases is that a SQL Server process (or task) waits for a resource, this resource has not been released by other processes. If two processes are serial, there is no conflict, and when they are in parallel, conflicts over a resource arise. These waits are mostly caused by lock requests for user resources.
In this chapter, we'll learn about techniques to solve concurrency problems. In general, it is to diagnose and resolve the following situations: locks, blocks, and deadlocks. The basic method is simple and straightforward:
Identify concurrency problems;
Analyze and identify the causes;
Modify and resolve the problem.
Using this method requires us to be familiar with a considerable amount of background information. This chapter assumes that the reader has already read or is already familiar with the contents of inside SQL Server 2005:the Storage engine 8th "Lock and Concurrency" Kalen Delaney writes ( See references in this book's Bibliography: Other resources and references. Specifically, that chapter contains the necessary information about what is required to resolve concurrency issues in SQL Server 2005, such as transactions, isolation levels, and locks.
1. New tools for solving concurrency problems
SQL Server 2005 adds some important new tools for resolving concurrency problems. In addition to the new tools, it still contains some of the original tools, such as system stored procedures such as SP_WHO2, perfmon counters, SQL Trace/profiler tools to solve concurrency problems, and so on.
To detect and analyze concurrency issues, SQL Server 2005 includes the following new tools:
DMV, especially Sys.dm_os_wait_stats and sys.dm_os_waiting_tasks.
Enhanced SQLDiag.exe function to collect information on running the system.
Quarantine options based on row versions (snapshot and committed read Snapshot isolation levels).
The new 1222 trace identity and SQL Trace detects deadlock chart event classes for deadlocks.
Blocked processes in SQL Trace/profiler report event classes and detect long blocked process thresholds server configuration options.
The new counter "Sqlserver:transactions Perfmon" object that detects update conflicts in the Snapshot isolation transaction and calculates the amount of storage used in the version store in tempdb.
Currently, there are two of the most basic tools in the new tool to note: The sys.dm_os_waiting_tasks DMV for detection and the row-version isolation level for resolution. Here we'll just summarize and talk about the specific techniques to solve the problem and introduce more details.
(1) New blocking detection tool: Sys.dm_os_waiting_tasks
Sys.dm_os_waiting_tasks is the most important new tool for SQL Server 2005 to detect concurrency problem blocking types. This DMV displays the wait information for all tasks. Not all waiting tasks are blocked because the task may wait for i/ o or memory authorization. When a task waits for another task for a relatively long time, it is blocked. When one task A is blocked by another task B, blocking task B is displayed in the view.
One of the advantages of this view is that it lists tasks and processes. Tasks exhibit more detailed SQL Server execution details than the SPID (server process ID). If the process is parallel, a given SPID may contain some synchronization tasks, and if the SPID is not parallel, the task has only one SPID.
There are many reasons for the task waiting, many of which have nothing to do with concurrency. So it's important to filter out irrelevant rows from the view so that you can better focus on blocking problems caused by resource lockouts.
(2) New Blocking resolution tool: Isolation level based on row version control
Another major new tool consists of a row version of the Snapshot isolation option, which reduces the number of locks and thus reduces blocking and deadlock. Specifically, row versions can reduce or remove shared locks within and outside of a transaction. Queries and transactions that may be blocked because they publish shared locks are no longer blocked. Unlike using the NOLOCK hint, these queries only read the submitted data.
The isolation level based on the row version is useful in some ways, including the changes in tempdb that are required by the row version to grow, the code churn needed by the trigger when using one or all of the snapshot isolation options, and the conflict between the DDL options and the potentially lost updates when using the snapshot isolation level.
2, the type of concurrency problem
When a SQL Server process or task is waiting, it is usually waiting for a resource to be locked by another SQL Server. The most likely concurrency problem is related to locks, including the following:
Lock
Obstructive
Dead lock
Tempdb uses
Update conflicts
The first 3 are related to the following: Locking is a basic concurrency mechanism for SQL Server, and blocking occurs when a task must wait for a resource that is locked by another task, and the deadlock occurs when the task blocks each other.
The following two kinds of the list are generated from the use of row versions. In general, we may see that the growth of the tempdb activity is related to the use of row versions, and there may be some performance issues associated with it. (We may also need to locate other tempdb-related concurrency issues, such as high tempdb activity related to the high frequency creation of temporary tables.) In addition, when using the "snapshot" isolation level for write database transactions, we may find cases of update conflicts.