LOCK Comparizon : NOLOCK vs. READPAST vs. ROWLOCK vs. UPLOCK vs. HOLDLOCK

1. TABLOCK VS. HOLDLOCK

 

SELECT
 
*
 
FROM
 
table
 
WITH
 (
HOLDLOCK
) 其他事务可以读取表,但不能更新删除 


SELECT * FROM table WITH (TABLOCKX) 其他事务不能读取表,更新和删除

2. NOLOCK VS. UPLOCK

http://www.mssqltips.com/tip.asp?tip=1257

The UPDLOCK hint tells the SQL Server query engine “Don’t allow any other reader of this row to acquire an UPDLOCK (“U” lock) because I will be promoting this lock to an exclusive “X” lock later in my processing”.

from envykok:

- the row i selected no allowed to be modify by others guys because i will promote modify later .

 

 

3. NOLOCK VS. ROWLOCK

 

Diff:

  • NOLOCK only use in SELECT Statement
  • ROWLOCK can be used in SELECT, UPDATE, DELETE

 

http://www.sql-server-performance.com/articles/per/lock_contention_nolock_rowlock_p2.aspx

Using NOLOCK

Using NOLOCK politely asks SQL Server to ignore locks and read directly from the tables. This means you completely circumvent the lock system, which is a major performance and scalability improvement. However, you also completely circumvent the lock system, which means your code is living dangerously. You might read the not-necessarily-valid uncommitted modifications of a running transaction. This is a calculated risk. 

For financial code and denormalized aggregates (those little counters of related data that you stash away and try desperately to keep accurate), you should play it safe and not use this technique. But I think you'll find that for better than 90% of your application, it would not be that big of a deal if a user (or even intermediate code) saw an uncommitted modification. In fact, you'll probably find that most of your data never or only very rarely changes, in which case the overhead of locking the data is almost always completely wasted. 

For example, if I want to count all users that joined Streamload.com between June 1 and August 31 of Y2K, there's no reason for me to lock anything: that number was cast in stone the moment September 1, 2000 rolled around. Another example is the file listings you see on Streamload.com: it doesn't much matter if you don't see the exact perfect data, since either you don't own the data and it doesn't much matter what you see, or you do own the data and you know perfectly well whether you just modified the data or not and whether new files have finished uploading. 

Just don't use this type of data as the basis for modifications to the database, and don't use it when it's really important that the user not see the wrong thing (an account statement or balance, for instance). 

 

Using ROWLOCK

Using ROWLOCK politely asks SQL Server to only use row-level locks. You can use this in SELECT, UPDATE, and DELETE statements, but I only use it in UPDATE and DELETE statements. You'd think that an UPDATE in which you specify the primary key would always cause a row lock, but when SQL Server gets a batch with a bunch of these, and some of them happen to be in the same page (depending on this situation, this can be quite likely, e.g. updating all files in a folder, files which were created at pretty much the same time), you'll see page locks, and bad things will happen. And if you don't specify a primary key for an UPDATE or DELETE, there's no reason the database wouldn't assume that a lot won't be affected, so it probably goes right to page locks, and bad things happen. 

By specifically requesting row-level locks, these problems are avoided. However, be aware that if you are wrong and lots of rows are affected, either the database will take the initiative and escalate to page locks, or you'll have a whole army of row locks filling your server's memory and bogging down processing. One thing to be particularly aware of is the "Management/Current Activity" folder with Enterprise Manager. It takes a long time to load information about a lot of locks. The information is valuable, and this technique is very helpful, but don't be surprised if you see hundreds of locks in the "Locks/Processes" folder after employing this technique. Just be glad you don't have lock timeouts or deadlocks. 

 

Additional Note

I get the sense that SQL Server honors NOLOCK requests religiously, but is more discretional with ROWLOCK requests. You can only use NOLOCK in SELECT statements. This includes inner queries, and the SELECT clause of the INSERT statement. You can and should use NOLOCK in joins, for example:

SELECT COUNT(Users.UserID)
FROM Users WITH (NOLOCK)
JOIN UsersInUserGroups WITH (NOLOCK) ON 
Users.UserID = UsersInUserGroups.UserID 

 

4. NOLOCK Vs. READPAST

Diff:

  • NOLOCK : Read Uncommitted record
  • READPAST : Ignore Uncommitted record

Same: only use for SELECT Statment

http://articles.techrepublic.com.com/5100-10878_11-6185492.html

 

NOLOCK

This table hint, also known as READUNCOMMITTED, is applicable to SELECT statements only. NOLOCK indicates that no shared locks are issued against the table that would prohibit other transactions from modifying the data in the table.

The benefit of the statement is that it allows you to keep the database engine from issuing locks against the tables in your queries; this increases concurrency and performance because the database engine does not have to maintain the shared locks involved. The downside is that, because the statement does not issue any locks against the tables being read, some "dirty," uncommitted data could potentially be read. A "dirty" read is one in which the data being read is involved in a transaction from another connection. If that transaction rolls back its work, the data read from the connection using NOLOCK will have read uncommitted data. This type of read makes processing inconsistent and can lead to problems. The trick is being able to know when you should use NOLOCK.

As a side note, NOLOCK queries also run the risk of reading "phantom" data, or data rows that are available in one database transaction read but can be rolled back in another. (I will take a closer look at this side effect in part two of this article series.)

The following example shows how NOLOCK works and how dirty reads can occur. In the script below, I begin a transaction and insert a record in the SalesHistory table.

BEGIN TRANSACTION
      INSERT INTO SalesHistory
      (Product, SaleDate, SalePrice)         
      VALUES           
      ('PoolTable', GETDATE(), 500)                  

The transaction is still open, which means that the record that was inserted into the table still has locks issued against it. In a new query window, run the following script, which uses the NOLOCK table hint in returning the number of records in the SalesHistory table.

SELECT COUNT(*) FROM SalesHistory WITH(NOLOCK)

The number of records returned is 301. Since the transaction that entered the record into the SalesHistory table has not been committed, I can undo it. I'll roll back the transaction by issuing the following statement:

ROLLBACK TRANSACTION

This statement removes the record from the SalesHistory table that I previously inserted. Now I run the same SELECT statement that I ran earlier:

SELECT COUNT(*) FROM SalesHistory WITH(NOLOCK)

This time the record count returned is 300. My first query read a record that was not yet committed -- this is a dirty read.
READPAST

This is a much less commonly used table hint than NOLOCK. This hint specifies that the database engine not consider any locked rows or data pages when returning results.

The advantage of this table hint is that, like NOLOCK, blocking does not occur when issuing queries. In addition, dirty reads are not present in READPAST because the hint will not return locked records. The downside of the statement is that, because records are not returned that are locked, it is very difficult to determine if your result set, or modification statement, includes all of the necessary rows. You may need to include some logic in your application to ensure that all of the necessary rows are eventually included.

The READPAST table hint example is very similar to the NOLOCK table hint example. I'll begin a transaction and update one record in the SalesHistory table.

BEGIN TRANSACTION
      UPDATE TOP(1) SalesHistory
      SET SalePrice = SalePrice + 1

Because I do not commit or roll back the transaction, the locks that were placed on the record that I updated are still in effect. In a new query editor window, run the following script, which uses READPAST on the SalesHistory table to count the number of records in the table.

SELECT COUNT(*)

FROM SalesHistory WITH(READPAST)

My SalesHistory table originally had 300 records in it. The UPDATE statement is currently locking one record in the table. The script above that uses READPAST returns 299 records, which means that because the record I am updating is locked, it is ignored by the READPAST hint.

 

 

Others:

 

HOLDLOCK 持有共享锁,直到整个事务完成,应该在被锁对象不需要时立即释放,等于SERIALIZABLE事务隔离级别

NOLOCK 语句执行时不发出共享锁,允许脏读 ,等于 READ UNCOMMITTED事务隔离级别

PAGLOCK 在使用一个表锁的地方用多个页锁

READPAST 让sql server跳过任何锁定行,执行事务,适用于READ UNCOMMITTED事务隔离级别只跳过RID锁,不跳过页,区域和表锁

ROWLOCK 强制使用行锁

TABLOCKX 强制使用独占表级锁,这个锁在事务期间阻止任何其他事务使用这个表

UPLOCK 强制在读表时使用更新而不用共享锁
内容概要:本文详细介绍了“电动汽车聚合可行域的内-外结合近似方法”的Matlab代码实现,旨在通过数值仿真手段对大规模电动汽车集群作为灵活资源参与电网调度的能力边界进行建模与逼近。该方法融合内部凸包构造与外部约束逼近策略,精确刻画电动汽车在充电状态、功率调节、时间维度等方面的聚合可调能力范围,形成紧致的可行域表示,为高比例电动车接入下的电力系统优化调度、需求响应、车网互动(V2G)等关键应用提供可靠的建模基础。文中强调科研应兼顾严谨逻辑与创新思维,并倡导借助YALMIP等成熟优化工具提升研究效率。完整的代码、工具包及相关案例可通过指定网盘链接和微信公众号“荔枝科研社”获取。; 适合人群:具备电力系统分析、优化理论基础及Matlab编程能力的科研人员,尤其适用于从事新能源并网、电动汽车调度、智能电网、综合能源系统等方向的研究生与工程技术人员。; 使用场景及目标:①研究大规模电动汽车集群的聚合建模与灵活性量化方法;②实现电动汽车聚合可行域的内-外近似算法;③支撑含高渗透率电动汽车的电力系统日前-实时协同优化调度;④学习并应用YALMIP工具进行数学规划建模与求解;⑤为需求响应、市场机制设计及V2G运营提供技术支撑。; 阅读建议:建议读者按照文档指引循序渐进学习,结合网盘提供的完整资源(包括YALMIP工具包与示例代码)进行实践调试,重点掌握内-外近似方法的数学建模逻辑、约束处理技巧及优化求解流程,并参考团队发布的其他相关案例,深化对优化算法与仿真技术在能源系统中应用的理解。
下载代码方式:https://pan.quark.cn/s/93e47adaae45 医学研究人员近期识别了若干新型病毒,经由对这些病毒的解析,揭示出它们的遗传物质序列均呈现环状形态。当前,研究人员已汇集了丰沛的病毒遗传物质与人类遗传物质样本数据,期望能迅速辨识个体是否感染了相关病毒。为便于探究,研究人员将人类遗传物质与病毒遗传物质均表述为由若干字母构成的字符串序列。继而,检测某种病毒遗传物质序列是否在患者遗传物质序列中有所显现,倘若出现,则表明该个体感染了对应病毒,反之则未感染。《病毒感染检测实验报告》是在医学研究背景下,依托软件技术基础中的编程知识完成的一个课程设计任务,其目的在于评估人体是否感染特定病毒。在此实验中,病毒遗传物质与人类遗传物质被表现为字母序列,通过比对病毒遗传物质序列是否存在于人的遗传物质序列中,从而判定感染状态。在程序构建方面,该实验所涉及的核心知识要点包括: 1. **字符串操作**:程序需处理病毒遗传物质与人类遗传物质的序列,这些序列以字符串形式呈现。字符串操作涵盖输入、输出、比较及匹配等操作,是C/C++编程的基础。 2. **线性表**:遗传物质序列可视为线性数据结构,可通过数组来表示。程序运用全局变量数组`V`和`D`存储病毒与人的遗传物质序列。 3. **栈**:在循环展开病毒遗传物质时,可能需借助栈数据结构来保存某个阶段遗传物质序列的状态,以便进行所有可能的匹配尝试。 4. **BF算法**(Brute Force,暴力搜索算法):BF算法是一种基础性的字符串匹配方法,用于在文本中搜寻模式。在此,BF算法用于在人的遗传物质序列中探寻病毒遗传物质序列的所有展开形态。 5. **文件处理**:程序需从文件《病毒感染检测输入数据...
内容概要:本文系统研究了基于粒子群PSO、灰狼GWO、鲸鱼WOA、哈里斯鹰HHO、蜣螂DBO、麻雀SSA六种智能优化算法的无人机三维路径规划方法,并通过Matlab代码实现了在复杂三维环境下的路径搜索与优化对比。研究构建了包含障碍物、威胁区域等实际地形特征的空间模型,综合考虑路径长度、飞行时间、能耗及安全性等多维度成本函数,对各算法的寻优能力、收敛速度、稳定性和全局搜索性能进行了量化评估与深入分析。通过大量仿真实验,全面比较了不同算法在多样化环境条件下的路径规划表现差异,揭示了各类算法的优势与局限性,为无人机在实际任务中的自主导航与决策提供了可靠的算法选型依据和技术支撑。; 适合人群:具备一定Matlab编程基础,从事无人机路径规划、智能优化算法研究或相关方向的研究生、科研人员及工程技术人员。; 使用场景及目标:① 掌握主流群智能优化算法在三维路径规划中的建模与实现方法;② 对比分析不同算法在复杂环境下的性能优劣,服务于算法选型与改进;③ 为无人机自主导航、无人系统协同控制等应用场景提供算法验证平台。; 阅读建议:此资源以Matlab代码为核心,建议读者结合代码与算法原理进行同步学习,通过调整环境参数与成本函数权重开展仿真实验,深入理解算法行为特征,并可根据自身研究需求进行二次开发与拓展。
本项目是一套基于知识图谱与 LLM 大模型的电影智能问答与推荐系统完整 Python 源码,为计算机相关专业高分毕业设计项目(评审得分 98 分),经导师全程指导与专业审定,全量源码均通过本地编译与多场景调试,确保可稳定运行,兼具学习参考与二次开发价值。 项目以电影领域知识为核心,通过构建结构化电影知识图谱,结合大语言模型(LLM)作为智能 AI 助手,实现了自然语言驱动的电影知识问答与个性化推荐能力,解决了传统系统语义理解能力弱、交互形式单一的痛点。 核心技术栈:基于 Python 开发,采用 Neo4j 图数据库存储与管理电影知识图谱,融合 LLM 大模型实现自然语言意图解析、答案生成与推荐理由输出,配套完整的前后端交互界面。 核心功能: 智能问答功能:支持用户以自然语言提问电影详情、演员作品、导演履历、影片类型、上映信息等各类问题,LLM 负责精准识别用户意图,调用知识图谱数据推理并生成通顺自然的回答; 个性化推荐:基于知识图谱的实体关联关系,结合用户偏好输出电影推荐结果,同时由 LLM 生成贴合用户需求的个性化推荐理由; 知识图谱可视化:支持电影、演员、导演等实体及其关联关系的图形化展示,直观呈现知识网络结构; 多轮对话交互:依托 LLM 的对话能力,支持上下文关联的多轮追问,交互体验更贴近真人对话。 项目代码结构清晰、注释完整,覆盖数据预处理、知识图谱构建、LLM 接口封装、后端服务、前端页面全流程,难度适中,非常适合计算机、软件工程、人工智能等专业学生用于毕业设计、课程设计、期末大作业,也可作为 Python 项目实战、知识图谱与 LLM 落地应用学习的练手项目,可直接基于现有框架进行功能扩展与场景创新。
内容概要:本文围绕基于去噪概率扩散模型(DDPM)的光伏场景生成展开研究,提出了一种利用扩散模型对光伏发电功率时间序列进行高保真建模与生成的方法。该方法充分挖掘DDPM在处理不确定性与复杂分布方面的优势,通过前向扩散逐步引入噪声、反向去噪逐步恢复数据的机制,实现了对光伏出力波动性和随机性的精准刻画。研究详细阐述了模型架构设计、训练流程、噪声调度策略及神经网络结构选择,并结合实际光伏数据进行了实验验证,生成的场景不仅具有良好的统计一致性,还能为新能源系统提供多样化、可靠的输入条件,有效支撑后续的规划、调度与风险评估任务。; 适合人群:具备一定机器学习、深度学习及时间序列分析基础的研究人员和技术工程师,特别适用于从事新能源发电预测、电力系统优化、能源场景模拟等相关领域的硕博研究生和科研工作者;熟悉Python编程并对生成模型感兴趣的技术人员亦可从中获益。; 使用场景及目标:①解决传统方法难以捕捉光伏出力复杂时空特性的难题,生成高质量、多样化的光伏功率场景;②服务于微电网、综合能源系统等在不确定环境下的优化调度、可靠性评估与韧性提升;③为电力市场仿真、储能配置、需求响应等应用提供稳健的输入数据支撑;④深入理解扩散模型在能源数据生成任务中的具体实现路径与关键技术细节。; 阅读建议:建议读者结合所提供的Python代码逐模块分析模型实现过程,重点掌握数据标准化、扩散过程建模、U-Net网络设计以及采样推理逻辑,并尝试在不同地区、不同时间分辨率的光伏数据集上进行迁移与调优,以深化对模型泛化能力和超参数敏感性的认识。
内容概要:本文围绕基于灰狼优化算法(GWO)优化改进的自适应完备集合经验模态分解(CEEMDAN)方法,提出了一种用于混合储能系统中风电功率分解与平抑风电波动的技术方案。研究通过Matlab代码实现了GWO-CEEMDAN联合算法,旨在有效应对风电输出功率强波动性和随机性带来的挑战。该方法首先利用CEEMDAN对原始风电功率信号进行多尺度自适应分解,获得一系列不同频率特性的本征模态函数(IMF),随后引入灰狼优化算法(GWO)对CEEMDAN的关键参数(如噪声标准差和集成次数)进行智能寻优,以提升分解精度并抑制模态混叠现象。在此基础上,依据各IMF分量的频率特征,制定合理的功率分配策略,将高频分量分配给响应速度快的储能单元(如超级电容),低频分量分配给能量密度高的储能单元(如蓄电池),从而实现混合储能系统内部的协同工作,显著降低风电功率波动对电网的冲击,延长储能设备使用寿命,提高系统运行的稳定性、经济性与可靠性。文中提供了完整的仿真流程、详细的算法设计、参数设置及实验验证结果,体现了信号处理与智能优化算法在新能源电力系统中的深度融合,属于电力系统自动化与可再生能源集成领域的高水平科研实践。; 适合人群:具备电力系统分析、可再生能源技术或信号处理等相关专业知识背景,熟悉Matlab/Simulink编程环境,正在从事风电并网、储能控制、智能优化算法应用等方向研究的研发工程师、高校教师及在读硕士、博士研究生(尤其适合工作1-3年或处于课题攻关阶段的研究人员)。; 使用场景及目标:①应用于风电场配套混合储能系统的实时功率平抑与能量管理,实现对风电波动的有效缓冲;②服务于高校、科研院所开展关于“风电功率预测与平滑”、“混合储能协调控制”、“智能优化算法在电力系统中的应用”等课题的理论研究、仿真验证与学术论文复现;③为工程技术人员提供一套成熟的GWO与CEEMDAN融合算法的Matlab实现代码框架与系统设计思路,作为开发更高级控制策略的技术参考。; 阅读建议:建议读者结合所提供的Matlab代码与技术文档进行逐行调试与运行,重点理解CEEMDAN的信号分解机制、GWO算法的寻优过程以及两者耦合的实现逻辑,深入掌握功率分频分配策略的设计原理。在学习过程中,应注重算法参数敏感性分析,并可尝试将GWO替换为其他智能算法(如PSO、WOA、HHO等)进行对比实验,以全面评估不同优化器的性能差异,从而深化对混合储能系统先进控制策略的理解与自主创新能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值