知方可补不足~Sqlserver发布订阅与sql事务的关系
回到目录 前几讲说了一下通过sqlserver的发布与订阅来实现数据的同步,再通过EF这个ORM架构最终实现架构系统的读写分离,而在使用发布与订阅来实现数据同步时,需要我们注意几点, 那就是当操作被使用在事务上下文时 ,你的同步操作有可能会被延时,嘟嘟!
回到目录
前几讲说了一下通过sqlserver的发布与订阅来实现数据的同步,再通过EF这个ORM架构最终实现架构系统的读写分离,而在使用发布与订阅来实现数据同步时,需要我们注意几点,那就是当操作被使用在“事务上下文”时,你的同步操作有可能会被延时,嘟嘟!
这个不难理解,我们都知道事务有一些级别,而最高级别serializable 又是.net TransactionScope默认的级别,所以,在程序开发中,只要用了事务,基本都是serializable,而这个级别是最安全的,当然对于SQL来说,也是最容易发生死锁及阻塞的,呵呵。
如果对要SQL锁不清楚的同学,可以看我的这篇文章《知方可补不足~Sqlserver中的几把锁和.net中的事务级别》
下面是我总结的,在事务为serializable级别,对于发布订订阅同步的关系
<span>set</span> <span>transaction</span> <span>isolation</span> <span>level</span> <span>serializable</span> <span>begin</span> <span>tran</span> <span>select</span> <span>*</span> <span>from</span> User_Info <span>--</span><span>读取所有数据,等待事务结束后才能同步 TAB(S) ,TAB(IX) </span> <span>update</span> User_Info <span>set</span> Status<span>=</span><span>1</span> <span>where</span> UserInfoID<span>=</span><span>1</span> <span>--</span><span>更新其他数据,可以立即同步 TAB(IX),Page(IX),Key(X)</span> <span>select</span> <span>*</span> <span>from</span> User_Info <span>where</span> UserInfoID<span>=</span><span>1</span> <span>--</span><span>事务中读取其他数据,可以立即同步 TAB(IS),KEY(S),Page(IS)</span> <span>select</span> <span>*</span> <span>from</span> User_Info <span>where</span> UserInfoID<span>=</span><span>28</span> <span>--</span><span>事务中读当前数据,等待事务结束后才能同步 TAB(IS),KEY(S),Page(IS)</span> <span>select</span> <span>*</span> <span>from</span> User_Info <span>where</span> UserInfoID<span><span>30</span> <span>--</span><span>事务中读取范围数据,包括要同步的数据,等待事务结束后才能同步,tab(is), KEY(ranges-s),page(is)</span> <span>select</span> <span>*</span> <span>from</span> User_Info <span>where</span> UserInfoID<span><span>10</span> <span>--</span><span>事务中读取范围数据,不包括要同步的数据,可以立即同步,tab(is), KEY(ranges-s),page(is)</span> <span>waitfor</span> delay <span>'</span><span>00:02:00</span><span>'</span> <span>--</span><span>等待2分钟 </span> <span>commit</span> <span>tran</span></span></span>
通过上面的结果,我们可以知道,只要当前需要同步(正在发生变化的数据,就是要同步的数据)的数据不在被锁的范围里,就不会对同步有所影响,当然,你要是在事务里来个select * from table,那你就玩完了,需要等待你的事务结束后,你这个张表发生变
化的数据才能被同步,所以,经验告诉我们,在事务里,能不写查询就不要写,呵呵。
下面图中显示的是在一个事务里添加了范围锁的例子,看上支挺恐怖的,它对应的语句是select * from User_Info where UserInfoID10,直接查询出10条数据,这时,SQL会把这10条数据分别加上范围共享锁,以对这10条数据进行保护,你此时,要想对这10条数据的任何一条进行修改,那只能等待事务结束后了......
回到目录

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

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version
Visual web development tools

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