[MySQL 45 Lecture Notes]

Lecture 1: How is a SQL query statement executed?

  • Connector: After completing the classic TCP handshake, start authenticating identity and verifying permissions
  • Analyzers: lexical analysis and syntax analysis
  • Optimizer: which index to use, the connection order of each table
  • Executor: Call the engine interface to get data, determine whether it meets the conditions, and put those that meet the conditions in the result set
  • Storage engine: interacts with the disk, obtains data, and returns it to the executor

Lecture 2: How is a SQL update statement executed?

两个重要日志

  • bin log: server layer, append write, record update SQL statement
  • redo log: storage engine, overwriting, recording update data

更新过程

  • redo log - prepare: update data records to memory
  • bin log: update statements are recorded to disk
  • redo log - commit: update data recorded to disk

Lecture 3: Transaction isolation, why I still can’t see it after you changed it

事务执行语句

  • Transaction starting statements: begin, start transaction
  • Transaction commit statement: commit
  • Transaction rollback statement: rollback
  • Autocommit is turned off: set autocommit = 0

Lecture 4: Understanding indexing in simple terms (Part 1)

创建索引语句

create table T(
	id int primary key,
	k int not null,
	name varchar(16),
	index(k)
)engine = InnoDB;

主键索引和普通索引的区别

  • The primary key index does not need to be returned to the table. If the ordinary index does not cover the index, the table needs to be returned.

Lecture 5: Explain the index in simple terms (Part 2)

几个重要的概念

  • Covering index: If the ordinary index is a covering index, there is no need to return the table
  • Leftmost prefix principle: multiple fields in the index are from left to right, and the content of the fields is also from right to right.
  • Index pushdown: can reduce the number of table returns

Lecture 6: Global locks and table locks. Why are there so many obstacles in adding a field to a table?

全局锁

  • Global read lock command: Flush tables with read lock
  • Typical usage scenario: full database logical backup

表级锁

  • Table lock (data lock): lock tables t1 read, t2 write; (lock), unlock table; (unlock)
  • Metadata lock MDL (table structure lock): no need to use it explicitly, it will be added automatically when accessing a table

行级锁

  • MySQL's row-level locks are implemented by each engine at the engine layer, but not all engines support row locks.

Lecture 7: What are the merits and demerits of row locks? How to reduce the impact of row locks on performance?

重要概念

  • Two-stage lock: locking stage and unlocking stage
  • Deadlock: Two-phase locking can lead to deadlock

死锁解决

  • Set deadlock timeout: innodb_lock_wait_timeout = 50
  • Set deadlock detection: innodb_deadlock_detect = on
  • Control concurrency: control the number of threads operating the same row of data
  • Change one line into logical multiple lines to reduce lock conflicts

Lecture 8: Are transactions isolated or not?

两种读

  • Current reading: Read the latest submitted data
  • Consistent read: read data following the isolation mechanism

不同操作的隔离机制

  • Update operation: read committed (current read)
  • Query operation: repeatable read (consistent read)

思考一下为什么?
Transaction B finds that the value of K is 3, and transaction A finds that the value of K is 1
Insert image description here

Lecture 9: How to choose between ordinary index and unique index?

Because the update operation of a unique index may not use the change buffer, it is generally recommended to choose a normal index.

Lecture 10: Why does MySQL sometimes choose the wrong index?

查询索引信息

  • Instruction: show index from table_name;

影响索引选择的因素

  • Number of scanned rows: Prioritize indexes with smaller number of scanned rows
  • Discrimination: The greater the discrimination, the smaller the number of scanning lines.
  • Table return: If the performance consumed by ordinary index + table return is greater than that of full table query, it will cause index failure.
  • Field sorting:

索引选择错误的解决办法

  • Execute correction command: analyze table t;
  • Force the use of indexes: select * from t force(a) where …
  • Guide to using index: order by b becomes order by b,a
  • Remove misused indexes

Lecture 11: How to index a string field?

  • 命令:alter table user add index index_email(email6));
  • Principle: Trade-off between Distinction and Index Length

Lecture 12: Why does my MySQL "tremble"?

主要分为两种场景,导致MySQL突然抖一下

  • The redo log is full, and the redo log memory needs to be refreshed (the log is written back to disk)
  • The buffer pool is not enough. The dirty pages must be written to the disk first (the data pages are written back to the disk).

InnoDB刷脏页的控制策略

  • Factors affecting InnoDB's ability to flush dirty pages: dirty page ratio, redo log writing speed
  • You can get the IOPS of the disk through the command, and then set InnoDB's innodb_io_capacity to IOPS
  • The actual disk flushing speed of InnoDB is calculated internally by an R%, and then multiplied by innode_io_capacity, which is the disk flushing speed.

Lecture 13: Why is the table file size unchanged when half of the table data is deleted?

  • The delete command actually just marks the recorded location or data page as "reusable", but the size of the disk file will not change. In other words, the table space cannot be reclaimed through the delete command. These can be reused, but the unused space looks like "empty".

Lecture 14: count(*) is so slow, what should I do?

  • As the number of records in the system increases, select count(*) from table_name will execute slower and slower.
  • You can use cache and data tables to store the value of count(*)
  • Performance of different counts: count (field) < count (primary key id) < count (1) ≈ count (*)

Lecture 16: How does order by work?

select city, name, age from T where city = '杭州' order by name limit 1000;

  • Full field sorting: take out the entire row of data, then take city, name, age and put them into sort_buffer for sorting, no need to return to the table
  • rowid sorting: take out the entire row of data, put some fields into sort_buffer for sorting, and need to return to the table
  • If (city, name) is an index, then the order by above does not require sorting.

Lecture 17: Why does it execute so slowly when I only check one line of statements?

select * from table where id = 1

  • Waiting for lock release (MDL lock, row lock)
  • Wait for flush to refresh disk

select * from table where c = 50000 limit 1

  • Slow query: full table scan,
  • Transaction: Undo caused by consistent reading many times, the current read directly reads the latest value

Guess you like

Origin blog.csdn.net/m0_46638350/article/details/132148688