以前我们也有讲过sql事务相关文章,下面我们来了解一下关于sql server 2005事务持久性一些东西,有需要的朋友参考一下。
原子性:事务必须是原子工作单元;对于其数据修改,要么全都执行,要么全都不执行。
一致性:事务完成时,必须使所有的数据都保持一致状态。
隔离性:由并发事务所作的修改必须与任何其他并发事务所作的修改隔离。
持久性:事务完成之后,它对于系统的影响是永久性的。该修改即使出现系统故障也将一直保持。
下面用简单的示例(Northwind)来说明这一点。
原子性:
正常情况下是先删除订单详细表,再删除订单信息,下面倒过来为了演示事务的原子性,生产中这么写就是属于逻辑错误。
代码如下 | 复制代码 |
USE Northwind; GO BEGIN TRAN; DELETE FROM dbo.Orders WHERE OrderID = 10249; DELETE FROM dbo."Order Details" WHERE OrderID = 10249; COMMIT TRAN; GO |
执行结果:
表Orders因为违反引用完整性,删除操作失败, 表Order Details成功删除2行.从这个例子来看第一条T-SQL语句执行失败而第二条T-SQL却执行成功,这不明显证明事务不具有原子性。别急接
着往下看,因为BEGIN TRAN与END TRAN之间的语句为一个事务,要么操作全部成功,要么操作全部回滚,其实导致这个事务没有回滚的原因是会话的XACT_ABORT选项默认为OFF,产生运行时错误
时只回滚产生错误的T-SQL语句,事务会继续进行处理,如果错误严重也可能回滚整个事务。所以对于大多数时候需要显示将XACT_ABORT选项设置为ON,以便产生运行时错误时回滚事务。
运行以下T-SQL
代码如下 | 复制代码 |
SET XACT_ABORT ON; BEGIN TRAN; DELETE FROM dbo.Orders WHERE OrderID = 10250; DELETE FROM dbo."Order Details" WHERE OrderID = 10250; COMMIT TRAN; GO |
执行结果:Orders,Order Details表删除失败,因为删除Orders表数据出现错误,所以没有继续执行第二条T-SQL就进行事务回滚了,由此可以证明事务具备原子性。
一致性:
指相关的所有规则都必须应用于事务的修改,以保证事务的完整性,如用户自定义完整性(业务逻辑)以及数据库引擎提供的数据完整性(如实体完整性,域完整性,引用完整性,NULL,DEFAULT)。也可以理解为事务的操作必须遵守数据库现有的规则。
隔离性:隔离性主要说明事务之间不能相互影响,(主要是依靠锁,行版本控制实现)。
新打开2个查询窗口:
在第1个窗口输入下列T-SQL并且执行
代码如下 | 复制代码 |
BEGIN TRAN; UPDATE dbo.Products SET ProductName = N'IPHONE4' WHERE ProductID = 1; GO |
在第2个窗口输入下列T-SQL并且执行
代码如下 | 复制代码 |
SELECT * FROM dbo.Products; GO |
第二个窗口执行的状态是"正在执行查询...",这是因为第一个事务并没有完成(COMMIT OR ROLLBACK),而第二个事务需要读取第一个事务所依赖的资源,事务不会识别中间状态(我们也可以理
解为"未知状态",因为在未完成之前,我们无法知道知道第一个事务的最终状态是提交,还是回滚),正式因为这个原因,而已第二个事务才举棋不定,无法读取。这个示例很直接的说明了事务的
隔离性。
持久性:持久性主要是依靠记录设备,SQL Server 2005中指的是事务日志。即使服务器硬件、操作系统或数据库引擎 实例自身出现故障,该实例也可以在重新启动时使用事务日志,将所有未完成的事务自动地回滚到系统出现故障的点,具体的实现在这就不详细说明了,大家可以参考相关资料

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

Key metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.

Usingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB

MySQL/InnoDB supports four transaction isolation levels: ReadUncommitted, ReadCommitted, RepeatableRead and Serializable. 1.ReadUncommitted allows reading of uncommitted data, which may cause dirty reading. 2. ReadCommitted avoids dirty reading, but non-repeatable reading may occur. 3.RepeatableRead is the default level, avoiding dirty reading and non-repeatable reading, but phantom reading may occur. 4. Serializable avoids all concurrency problems but reduces concurrency. Choosing the appropriate isolation level requires balancing data consistency and performance requirements.

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment