通俗的例子解释SQL的各种运行状态

本文解析了 SQL Server 的执行模型,重点介绍了 SQL Server 如何使用 SQLOS 调度程序来管理用户请求的执行。文章详细阐述了运行、可运行和挂起状态之间的轮转过程,以及等待列表和可运行队列的工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The SQL Server SQLOS uses schedulersto manage the execution of user requests. SQLOS Schedulers map to CPUs. Assuminga 4-CPU Server, there would be 4 SQLOS schedulers by default. The following diagrams depict a simplifiedversion of execution model using a single SQLOS scheduler. The execution model in Figure 1 depicts how SQL Serveruser requests or sessions (denoted by SPIDs) are scheduled for execution.

Figure 1: Execution Model – Running, runnable and suspendedstatus, Runnable Queue and Wait List

Figure 2 shows how SQL Server sessionsrotate between the following statuses:Running(only one session can be running or executing, per scheduler),Runnable (sessions waiting for CPU), orSuspended. SPIDs with suspended statuses are placedin Waiter List until the requested resources are available. If a running sessionneeds a data page that is not in cache, or needs a page that is blocked byanother user’s lock, the session is moved to the wait list. The next SPID or session_idin the runnable queue is scheduled to start running.

Figure 2: Execution Model – How status changes affect SPIDs

The status change sequence of events is as follows:

  1. SPID60 needs a page not in cache. Thus its status changes from Running to Suspended with wait type IO_Completion
  2. SPID60 moved to Waiter List
  3. SPID51 moves from Runnable queue with a runnable status to Running status, SPID64 then moves to the top of the Runnable queue
  4. SPID56 is waiting for a parallel process to complete. When the parallel process is completed, the status for SPID56 changes from Suspended with wait type CXPACKET to Runnable
  5. SPID56 moved to the bottom of the Runnable queue

Figure 3 depicts execution “after”session_ids (or SPIDs) have rotated clockwise due to status changes.

Figure 3: Execution Model – After status change

The wait list means a thread has to wait for aresource. Example of resource waits includes IOs to complete, a lock to bereleased, a memory grant, and so on. When the sessionis moved to the wait list, a wait type is assigned and time is accumulated.When the resource becomes available, the thread is moved to the runnable queueand it executes as soon as the CPU is available. The clockwise rotation between running,runnable and suspended states continues until the user request is completed.

具体参考微软白皮书:Performance_Tuning_Waits_Queues.doc

### 如何正确运行SQL语句 在Python中执行SQL语句通常涉及使用特定的库来连接数据库并发送查询。以下是关于如何通过Python程序执行SQL语句的关键知识点: #### 数据库连接与SQL执行流程 为了在Python中执行SQL语句,需要先建立到目标数据库的连接。常用的库有`sqlite3`(用于SQLite)、`pymysql`(用于MySQL)以及`psycopg2`(用于PostgreSQL)。一旦建立了连接,可以通过游标对象传递SQL命令[^1]。 对于SQLite而言,在其交互式窗口中输入SQL语句需注意每条语句结尾应加上分号`;`再按回车键确认执行。而在编程环境中,则无需手动添加该符号,因为大多数驱动器会自动处理它。 当涉及到Entity Framework Core (EF Core),可以利用 `DbContext.Database.ExecuteSqlRaw()` 方法或者异步版本的方法来直接调用原始SQL字符串,并返回影响行的数量。此操作前记得引入必要的命名空间如 `Microsoft.EntityFrameworkCore` [^2]。 下面展示了一个简单的例子说明怎样借助Python中的`sqlite3`模块完成基本的数据检索任务: ```python import sqlite3 # 创建/打开一个名为 'example.db' 的 SQLite 数据库文件 conn = sqlite3.connect('example.db') try: cursor = conn.cursor() # 执行创建表的操作 create_table_sql = """ CREATE TABLE IF NOT EXISTS users ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, age INT); """ cursor.execute(create_table_sql) # 插入一些测试数据 insert_data_sql = "INSERT INTO users(name,age) VALUES ('Alice', 30), ('Bob', 25);" cursor.execute(insert_data_sql) conn.commit() finally: conn.close() ``` 上述脚本首先定义了一张叫做users的新表格结构;接着向其中加入两条记录最后保存更改关闭链接资源。 #### 注意事项 - **安全性**: 避免拼接字符串形成最终要被执行的SQL指令以防SQL注入攻击风险。推荐采用参数化查询方式替代。 - **事务管理**: 明确何提交(commit)/撤销(rollback)修改动作以维护数据一致性状态
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值