InnoDB Read-Only Transactions_只读事务

InnoDB读取优化:减少不必要的事务ID,提升报表生成应用性能
文章介绍如何利用InnoDB的读取优化特性,通过减少不必要的事务ID来降低内部数据结构的查询开销,从而提高报表生成应用的性能。重点在于将一系列InnoDB查询组织在STARTTRANSACTIONREADONLY和COMMIT中,或在开启自动提交模式下运行,以避免锁定操作,实现高效的读取操作。

InnoDB Read-Only Transactions_只读事务

官方文档:http://dev.mysql.com/doc/refman/5.6/en/innodb-performance-ro-txn.html


As of(自从) MySQL 5.6.4, InnoDB can avoid the overhead associated with setting up the transaction ID (TRX_ID field) for transactions that are known to be read-only. A transaction ID is only needed for a transaction that might perform write operations or locking reads such as SELECT ... FOR UPDATE. Eliminating(排除) unnecessary transaction IDs(不必要的transaction id) reduces(降低) the size of internal data structures that are consulted(商量,顾及) each time a query or DML(数据操纵语言) statement constructs a read view. 


Currently, InnoDB detects(发现,发觉) read-only transactions when:

  • The transaction is started with the START TRANSACTION READ ONLY statement.  In this case, attempting to make changes to the database (for InnoDB, MyISAM, or other types of tables) causes an error, and the transaction continues in read-only state: 

    ERROR 1792 (25006): Cannot execute statement in a READ ONLY transaction.

    You can still make changes to session-specific temporary(临时的) tables in a read-only transaction, or issue locking queries for them, because those changes and locks are not visible to any other transaction.

  • The autocommit setting is turned on(打开), so that the transaction is guaranteed to be a single statement, and the single statement making up the transaction is a “non-locking” SELECT statement. That is, a SELECT that does not use a FOR UPDATE or LOCK IN SHARED MODE clause. 


InnoDB默认的read only有两种方式:

  • The transaction is started with the START TRANSACTION READ ONLY statement.

  • The autocommit setting is turned on(打开). 


Thus, for a read-intensive(加强的,强烈的) application such as a report generator, you can tune (调整使协调)a sequence of InnoDB queries by grouping them inside START TRANSACTION READ ONLY and COMMIT, or by turning on the autocommit setting before running the SELECT statements, or simply by avoiding any DML statements interspersed(散开的) with the queries. 


==================END==================


转载于:https://my.oschina.net/xinxingegeya/blog/342767

### 解决 MySQL 错误 1130 并优化配置参数的方法 #### 1. 解决错误 1130 错误代码 `1130` 是由于 MySQL 用户账户未被授权从指定主机(如 `'gateway'`)连接到服务器引起的。以下是解决方案: ##### (1) 创建或修改用户权限 通过登录 MySQL 控制台,执行以下 SQL 命令来创建新用户或将现有用户的权限扩展至目标主机: ```sql CREATE USER 'username'@'gateway' IDENTIFIED BY 'password'; GRANT ALL PRIVILEGES ON *.* TO 'username'@'gateway'; FLUSH PRIVILEGES; ``` 如果需要允许来自任意主机的连接,可以使用通配符 `%` 替代具体的主机名[^2]: ```sql CREATE USER 'username'@'%' IDENTIFIED BY 'password'; GRANT ALL PRIVILEGES ON *.* TO 'username'@'%'; FLUSH PRIVILEGES; ``` ##### (2) 验证绑定地址设置 检查 MySQL 配置文件(通常位于 `/etc/my.cnf` 或 `/etc/mysql/my.cnf`),确保 `bind-address` 参数已正确设置为服务器的实际 IP 地址或 `0.0.0.0`,以便接受外部请求: ```ini [mysqld] bind-address = 0.0.0.0 ``` 保存更改后重启 MySQL 服务以应用更新: ```bash sudo systemctl restart mysqld ``` --- #### 2. 优化 MySQL 配置参数 为了提高性能和稳定性,可以根据实际工作负载调整以下几个关键参数: ##### (1) 调整最大连接数 (`max_connections`) 默认的最大连接数可能不足以应对高并发场景。建议根据硬件资源适当增加该值: ```ini max_connections = 500 ``` 需要注意的是,过高的连接数可能会导致内存不足或其他系统瓶颈问题。 ##### (2) 设置 InnoDB 缓冲池大小 (`innodb_buffer_pool_size`) InnoDB 存储引擎依赖缓冲池缓存数据页和索引页。推荐将其设为物理内存总量的大约 70%-80%,但不超过总内存容量: ```ini innodb_buffer_pool_size = 4G ``` 对于较小规模的应用程序,可以选择较低的比例。 ##### (3) 启用查询缓存 (`query_cache_type`, `query_cache_size`) 尽管现代版本中逐渐弱化了查询缓存的作用,但在某些读密集型应用场景下仍然有效果。启用它可以帮助减少重复计算开销: ```ini query_cache_type = 1 query_cache_size = 64M ``` 然而,频繁写入操作可能导致缓存失效频率过高而降低效率,因此需权衡利弊后再决定是否开启此项功能。 ##### (4) 调优事务日志刷新策略 (`sync_binlog`, `innodb_flush_log_at_trx_commit`) 这两个选项控制着二进制日志同步磁盘的方式及时机。它们直接影响持久性和吞吐量之间的平衡关系: - 将 `sync_binlog` 和 `innodb_flush_log_at_trx_commit` 都设定为 1 提供最高级别的可靠性保障; - 若追求更高的速度则可考虑分别调低这些数值直到找到合适的折衷方案为止。 ```ini sync_binlog = 1 innodb_flush_log_at_trx_commit = 1 ``` 最后记得每次修改完配置之后都要重新启动服务使改动生效,并持续监控各项指标变化情况以评估改进成果。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值