mysql select不使用任何锁(select with nolock)
在ms sql中可以通过with(nolock)选项指定查询不锁表,在mysql中没有这个选项,需要通过set语句来设置不锁表:
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED ;
SELECT * FROM TABLE_NAME ;
COMMIT ;
也可以通过下面的sql语句:
SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED ;
SELECT * FROM TABLE_NAME ;
SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ ;
WITH (NOLOCK) table hint equivalent for MySQL
I don’t remember how many times I was asked about an equivalent term of the infamous “NOLOCK” hint for mysql database server, hence I thought it was worth to write about it here. “WITH (NOLOCK)” is a transaction isolation levels that defines how data is available during an update, or with other words it is a property that defines at what point changes made by an update operation will become available in a row, table or database to other processes.
The official SQL standard defines four isolation levels:
READ COMMITTED
READ UNCOMMITTED
REPEATABLE READ
SERIALIZABLE
Oracle, SQL Server and MySQL support isolation levels. During an operation, the database engine places certain locks to maintain data integrity. Different types of locking apply to different databases (Oracle vs. MySQL), or table types (eg. MyISAM vs. InnoDB).
When WITH (NOLOCK) is used with SQL Server, the statement does not place a lock nor honor exclusive locks on table. The MySQL equivalent is READ UNCOMMITTED, also known as “dirty read” because it is the lowest level of isolation. If we specify a table hint then it will override the current default isolation level. MySQL default isolation level is REPEATABLE READ which means locks will be placed for each operation, but multiple connections can read data concurrently.
SQL Server WITH (NOLOCK) looks like this:
SELECT * FROM TABLE WITH (nolock)
To achieve the same with MySQL, we change the session isolation mode using the SET SESSION command.
SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED ;
SELECT * FROM TABLE_NAME ;
SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ ;
This statement will work similar to WITH (NOLOCK) i.e READ UNCOMMITTED data. We can also set the isolation level for all connections globally:
SET GLOBAL TRANSACTION ISOLATION LEVEL READ UNCOMMITTED ;
In addition, two system variables related to isolation also level exist in MySQL server:
SELECT @@global.tx_isolation; (global isolation level)
SELECT @@tx_isolation; (session isolation level)
Or set the isolation level inside a transaction:
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ
GO
mysql select不使用任何锁(select with nolock)的更多相关文章
- MySQL事务隔离级别,锁(转)
add by zhj: 本文针对的是MySQL的InnoDB存储引擎,不适用于MySQL的其它存储引擎和其它数据库 原文:MySQL数据库事务隔离级别(Transaction Isolation Le ...
- mysql锁SELECT FOR UPDATE【转】
MySQL 使用SELECT ... FOR UPDATE 做事务写入前的确认 以MySQL 的InnoDB 为例,预设的Tansaction isolation level 为REPEATABLE ...
- 数据库:Mysql中“select ... for update”排他锁分析
Mysql InnoDB 排他锁 用法: select … for update; 例如:select * from goods where id = 1 for update; 排他锁的申请前提:没 ...
- [MySQL] 行级锁SELECT ... LOCK IN SHARE MODE 和 SELECT ... FOR UPDATE
一.译文 翻译来自官方文档:Locking Reads If you query data and then insert or update related data within the same ...
- Mysql中“select ... for update”排他锁(转)
原帖地址 https://blog.csdn.net/claram/article/details/54023216 Mysql InnoDB 排他锁 用法: select … for update; ...
- mysql行级锁 select for update
mysql行级锁 select for update 1.属于行级锁 2.where条件后需要写出明确的索引条件(如果有多个条件,可以建立联合索引) 3.如果其所在的事务提交或者回滚后,或者更新该条数 ...
- MySQL实战 | 01-当执行一条 select 语句时,MySQL 到底做了啥?
原文链接:当执行一条 select 语句时,MySQL 到底做了啥? 也许,你也跟我一样,在遇到数据库问题时,总时茫然失措,想重启解决问题,又怕导致数据丢失,更怕重启失败,影响业务. 就算重启成功了, ...
- MySQL SELECT语法(一)SELECT语法详解
源自MySQL 5.7 官方手册:13.2.9 SELECT Syntax SELECT的语法如下: SELECT [ALL | DISTINCT | DISTINCTROW ] [HIGH_PRIO ...
- select for update行锁
select for update行锁 2008-05-26 15:15:37 分类: Oracle Select-For Update语句的语法与select语句相同,只是在select语句的后面 ...
- oracle行锁select for update
oracle行锁select for update 学习了:https://blog.csdn.net/zdwzzu2006/article/details/50490157 学习了:https:// ...
随机推荐
- XPath <第四篇>
.Net框架下的System.Xml.XPath命名空间提供了一系列的类,允许你应用XPath数据模式查询和展示XML文档数据. 一.XPath介绍 XPath有七种类型的节点:元素.属性.文本.命名 ...
- VS2012 快捷键 VS Resharper 设置
原文 http://www.cnblogs.com/skyangell/archive/2013/03/24/2979835.html 一直用Resharper插件,最近发现Ctrl+E,C快捷见被R ...
- PLSQL 循环示例
<pre name="code" class="sql"><pre name="code" class="sql ...
- libeXosip2(1-1) -- How-To initialize libeXosip2.
How-To initialize libeXosip2. The eXtented eXosip stack Initialize eXosip and prepare transport laye ...
- UVa10340.All in All
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- 【转】基于DM8168的视频智能分析系统的设计方案
[导读] 为了实现高清视频的智能分析功能,本文介绍了一种以TI公司的DM8168为核心的高清视频智能分析系统的设计方案,该方案从硬件设计和软件设计两个方面介绍了硬件组成.工作流程.软件架构,并 ...
- android CMWAP, CMNET有何差别
什么是CMNET,什么是CMWAP? 答:CMWAP和CMNET仅仅是中国移动为其划分的两个GPRS接入方式.中国移动对CMWAP作了一定的限制,主要表如今CMWAP接入时仅仅能訪问GPRS网络内的I ...
- What is NetApp's Cluster File System?
Data ONTAP GX: A Scalable Storage Cluster www.usenix.org/event/fast07/tech/full_papers/eisler/eisler ...
- js将对象转成字符串-支持微信
最近写一个微信项目时用到了 把对象转成字符串,因为我需要把它存在cookie中,碰到了一些问题,在这里分享一下. 要转换的就是这货~ var FBinf = { "workPlacesCod ...
- 提示框的优化之自定义Toast组件之(三)Toast组件优化
开发步骤: 在toast_customer.xml文件中添加一个图片组件对象显示提示图片 <?xml version="1.0" encoding="utf-8&q ...