在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

最新文章

  1. 转载 wpf使用经验
  2. HTML5 地理位置定位(HTML5 Geolocation)原理及应用
  3. thinkphp关闭调试模式(APP_DEBUG => false),导致程序出错
  4. C# MVC ( 添加路由规则以及路由的反射机制 )
  5. 001.linux下clock()检测程序运行时间
  6. jwplayer直播
  7. Lintcode: Maximum Subarray II
  8. Android OkHttp详解
  9. phpcms V9利用num++实现多样形式列表标签调用
  10. (转载)PHP删除数组中的特定元素的代码
  11. Linux UGO和ACL权限管理
  12. JS设置Cookie过期时间
  13. 要求必须全部重复的数据sql--想了半天才写出来的
  14. 关于form-checkbox 必填项无效的错误
  15. 获取spring security用户相关信息
  16. EF CodeFirst系列(2)---CodeFirst的数据库初始化
  17. IT行业中文资源网址集绵
  18. ArrayList迭代器源码分析
  19. opencv学习之路(22)、轮廓查找与绘制(一)
  20. maven项目发布到tomcat的错误

热门文章

  1. Android的init过程详解(一)(转)
  2. jsp中全局变量和局部变量的设置
  3. DirectX 初始化DirectX(第一方式)
  4. C# yield return 流程理解
  5. Html中value和name属性的作用
  6. [RxJS] Using Observable.create for fine-grained control
  7. 利用Python进行数据分析——数据规整化:清理、转换、合并、重塑(七)(1)
  8. 使用Xshell连接Ubuntu
  9. 前端--关于客户端javascript
  10. SQL server sysobjects表说明