当前位置: 首页 > news >正文

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

转载于:https://www.cnblogs.com/micro-chen/p/5629188.html

相关文章:

  • Day5 双层装饰器、字符串格式化、生成器、迭代器、递归
  • Linux内核里的DebugFS
  • Linux 必掌握的 SQL 命令
  • curl的使用(from 阮一峰)
  • 对接微信红包时:CA证书出错,请登录微信支付商户平台下载证书
  • 深入理解JAVA I/O系列一:File
  • Java中的一些方法
  • Ubuntu server 16.04 安装mysql并设置远程访问
  • mybatis源码学习: 编译的方法
  • iOS 主动抛出异常
  • A Game
  • 选择最适合你的Linux学习方法
  • 什么是GPS的冷启动、温启动和热启动?
  • Clone
  • 正则表达式学习
  • JavaScript 如何正确处理 Unicode 编码问题!
  • CSS 提示工具(Tooltip)
  • hadoop入门学习教程--DKHadoop完整安装步骤
  • Javascript Math对象和Date对象常用方法详解
  • Java到底能干嘛?
  • mysql外键的使用
  • open-falcon 开发笔记(一):从零开始搭建虚拟服务器和监测环境
  • Vim Clutch | 面向脚踏板编程……
  • 番外篇1:在Windows环境下安装JDK
  • 工作中总结前端开发流程--vue项目
  • 设计模式走一遍---观察者模式
  • 使用Swoole加速Laravel(正式环境中)
  • 微信小程序开发问题汇总
  • 智能合约开发环境搭建及Hello World合约
  • 2017年360最后一道编程题
  • ​​​​​​​ubuntu16.04 fastreid训练过程
  • #设计模式#4.6 Flyweight(享元) 对象结构型模式
  • #在 README.md 中生成项目目录结构
  • (floyd+补集) poj 3275
  • (Java)【深基9.例1】选举学生会
  • (第9篇)大数据的的超级应用——数据挖掘-推荐系统
  • (附源码)计算机毕业设计高校学生选课系统
  • (更新)A股上市公司华证ESG评级得分稳健性校验ESG得分年均值中位数(2009-2023年.12)
  • (一)搭建springboot+vue前后端分离项目--前端vue搭建
  • (转载)hibernate缓存
  • .NET 4.0网络开发入门之旅-- 我在“网” 中央(下)
  • .Net 知识杂记
  • .NET/C# 编译期能确定的字符串会在字符串暂存池中不会被 GC 垃圾回收掉
  • .net中生成excel后调整宽度
  • @EventListener注解使用说明
  • @Mapper作用
  • [ACTF2020 新生赛]Include
  • [AIGC] Nacos:一个简单 yet powerful 的配置中心和服务注册中心
  • [ajaxupload] - 上传文件同时附件参数值
  • [Angular 基础] - 表单:响应式表单
  • [Angular 基础] - 自定义指令,深入学习 directive
  • [BZOJ] 3262: 陌上花开
  • [C#C++]类CLASS
  • [Docker]六.Docker自动部署nodejs以及golang项目
  • [Docker]四.Docker部署nodejs项目,部署Mysql,部署Redis,部署Mongodb