有什么方法可以在不导致 MySQL 锁定的情况下进行选择?

Any way to select without causing locking in MySQL?(有什么方法可以在不导致 MySQL 锁定的情况下进行选择?)
本文介绍了有什么方法可以在不导致 MySQL 锁定的情况下进行选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

查询:

SELECT COUNT(online.account_id) cnt from online;

但是在线表也被一个事件修改,所以我经常可以通过运行show processlist看到锁.

But online table is also modified by an event, so frequently I can see lock by running show processlist.

MySQL 中有没有什么语法可以让 select 语句不引起锁?

Is there any grammar in MySQL that can make select statement not causing locks?

我忘记在上面提到它在 MySQL 从数据库上.

And I've forgotten to mention above that it's on a MySQL slave database.

在我添加到 my.cnf:transaction-isolation = READ-UNCOMMITTED 之后从站会遇到错误:

After I added into my.cnf:transaction-isolation = READ-UNCOMMITTED the slave will meet with error:

错误无法进行二进制日志记录.消息:InnoDB 中的事务级别READ-UNCOMMITTED"对于查询时的二进制日志模式STATEMENT"不安全

Error 'Binary logging not possible. Message: Transaction level 'READ-UNCOMMITTED' in InnoDB is not safe for binlog mode 'STATEMENT'' on query

那么,有没有兼容的方法来做到这一点?

So, is there a compatible way to do this?

推荐答案

找到一篇名为MYSQL WITH NOLOCK"的文章

Found an article titled "MYSQL WITH NOLOCK"

https://web.archive.org/web/20100814144042/http://sqldba.org/articles/22-mysql-with-nolock.aspx

在 MS SQL Server 中,您将执行以下操作:

in MS SQL Server you would do the following:

SELECT * FROM TABLE_NAME WITH (nolock)

和 MYSQL 等价的是

and the MYSQL equivalent is

SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED ;
SELECT * FROM TABLE_NAME ;
SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ ;

编辑

Michael Mior 提出以下建议(来自评论)

Michael Mior suggested the following (from the comments)

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED ;
SELECT * FROM TABLE_NAME ;
COMMIT ;

这篇关于有什么方法可以在不导致 MySQL 锁定的情况下进行选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Simulating MySQL#39;s ORDER BY FIELD() in Postgresql(在 Postgresql 中模拟 MySQL 的 ORDER BY FIELD())
Using MySQL query to traverse rows to make a recursive tree(使用MySQL查询遍历行制作递归树)
MySQL LOAD DATA INFILE with ON DUPLICATE KEY UPDATE(MySQL LOAD DATA INFILE 和 ON DUPLICATE KEY UPDATE)
Search for quot;whole word matchquot; in MySQL(搜索“全字匹配在 MySQL 中)
add column to mysql table if it does not exist(如果不存在,则将列添加到 mysql 表)
MIN/MAX vs ORDER BY and LIMIT(MIN/MAX 与 ORDER BY 和 LIMIT)