82 Matching Annotations
  1. Last 7 days
    1. 事务语句顺序优化

      对于一个事务,应该将更容易被多个事务访问的锁放在后面加,比如说店铺,而比如用户的锁则先加,这样能够减少店铺的锁占用的时间

    2. 死锁检测以及相应开销

      由于事务之间可能产生死锁,因此要么设置最大等待超时时间,要么设置死锁检测。

      死锁检测

      1. 维护等待图(Wait-for Graph)

      InnoDB 内部维护一个有向图,图中的节点表示事务(Transaction),边表示锁的等待关系。

      例如:事务A持有行X的锁,事务B请求行X的锁并被阻塞,则图中有一条边从B指向A(B→A,表示B在等待A释放锁)。

      当某个事务请求锁时,如果锁已被其他事务持有,InnoDB 会更新等待图,添加一条新的边。

      2. 检测环(Cycle)

      每次有事务请求锁失败(进入等待状态)时,InnoDB 会触发死锁检测。

      深度优先搜索(DFS):InnoDB 通过DFS遍历等待图,检查是否存在环。如果发现环,则判定为死锁。

      优化:为了减少性能开销,InnoDB 不会每次都全图遍历,而是从新加入的边出发,仅检查可能形成环的路径。

      3. 选择牺牲者(Victim)

      如果检测到死锁,InnoDB 会选择一个事务作为牺牲者(通常选择回滚成本更低的事务,例如修改数据量较小的事务),强制回滚该事务,并释放其持有的锁。

      回滚后,等待图中对应的边被移除,其他被阻塞的事务可以继续执行。

      死锁的开销

      问题

      因此由于事务的不断加入,图会变得越来越大,进行环检测是O(n)的操作,若n很大则cpu大量消耗。

      解决

      可以通过中间件对操作相同key的事务限流,这样事务虽然也在等待,但是没有增加死锁检测的负担。

    1. 这个跟索引c的数据是一模一样的。

      当索引值相同时,按照主键排序,因此索引最后的字段是主键的话,可以去掉该索引

    2. MySQL有哪些锁?

      1. 全局锁

      元数据锁

      2. 表级锁

      • 元数据锁
      • 插入意向锁
      • 自增锁

      3. 行级锁

      • 间隙锁
      • 行锁
    3. 全库只读,最好使用flush table with read lock,其比设置readonly的好处是: 1. 能够自动释放 2. 可能其他系统会使用readonly做主备逻辑

    1. 如果有不合适的,为什么,更好的方法是什么?

      重建主键

      删除主键相当于重建整个表,mysql内部会使用一个隐式的自增字段存储记录。而创建主键则再次进行更改,将其从隐式转成显示的字段。因此如果需要重建主键索引则可以直接使用alter table T engine=InnoDB。

      删除自增主键

      如果要删除自增主键,那么主键列必须将自增删除掉,因为隐式的主键也是自增的,一个表只能有一个自增字段

    2. 显然,主键长度越小,普通索引的叶子节点就越小,普通索引占用的空间也就越小。

      尽量使用自增主键,能够避免页分裂,同时减少普通索引叶子节点的占用空间

    1. 不论是删除主键还是创建主键,都会将整个表重建

      删除或创建主键会重建表

    2. 覆盖索引、前缀索引、索引下推

      索引主要优化手段。尽量避免回表从而提高查询性能

    1. 在一天一备的模式里,最坏情况下需要应用一天的binlog。比如,你每天0点做一次全量备份,而要恢复出一个到昨天晚上23点的备份。一周一备最坏情况就要应用一周的binlog了。

      物理备份(热备)

      Percona XtraBackup 8.0.30-23引入了--register-redo-log-consumer参数。--register-redo-log-consumer参数默认为禁用状态。当启用时,此参数允许Percona XtraBackup在备份开始时注册为一个重做日志消费者。服务器不会移除Percona XtraBackup(消费者)尚未复制的重做日志。消费者读取重做日志并手动提升日志序列号(LSN)。在此过程中,服务器阻止写入操作。基于重做日志的消耗,服务器决定何时可以清理日志。

      为什么需要定期全量备份?

      因为备库会很快同步主库的数据,如果发生误删数据,备库无法保证数据的恢复。而全量备份则可以留存定期的快照,当出现问题时能够回滚

    2. 我给你留一个问题吧。你现在知道了系统里面应该避免长事务,如果你是业务开发负责人同时也是数据库负责人,你会有什么方案来避免出现或者处理这种情况呢?

      预防和发现

      1. 通过innodb_schema追踪innodb的事务状态,发现长事务,予以告警
      2. 连接初始时需要显示的设置autocommit或者显式启动连接,关闭或归还连接池的时候应显式重置连接(目前spring、mybatis会自动做)
    3. select * from information_schema.innodb_trx where TIME_TO_SEC(timediff(now(),trx_started))>60

      查找长事务

    4. 就是当系统里没有比这个回滚日志更早的read-view的时候。

      由于当前系统中没有比undolog更早的readview才能删除undolog。因此应当避免长事务来防止undolog大量占用磁盘和buffer pool。

    5. 在MySQL中,实际上每条记录在更新的时候都会同时记录一条回滚操作。记录上的最新值,通过回滚操作,都可以得到前一个状态的值。

      undolog使得记录能够往回回滚,使得mvcc读取到历史版本的数据

    1. 目前有团队在做基于redolog来做归档,从而去除binlog的依赖

    2. redolog是innodb引入保证crash-safe的。而binlog则是mysql的归档日志,可以提供备份、从节点复制等功能。为了保证redolog与binlog的一致性,通常需要两阶段提交,从而防止mysql的数据与binlog数据不一致。主要在于需要保证事务的redolog状态至少为prepare状态,且binlog完整(如果当前redolog处于提交状态,则事务直接是有效的)

    3. redolog是循环写的,只需要读取需要修改的数据(页)然后根据页的状态生成物理修改日志。使用redolog则无需直接去随机写修改磁盘页,而转变为了“顺序写redolog”

    4. 正常情况下的WAL或者说日志基本都是逻辑日志,而在mysql中redolog是物理日志,mysql如何知道要将数据写到物理磁盘上的那一个位置呢? Buffer Pool中为物理页的映射,由此知道如何在物理上进行数据的修改。

    1. 对于有索引的表,执行的逻辑也差不多。第一次调用的是“取满足条件的第一行”这个接口,之后循环取“满足条件的下一行”这个接口,这些接口都是引擎中已经定义好的。你会在数据库的慢查询日志中看到一个rows_examined的字段,表示这个语句执行过程中扫描了多少行。这个值就是在执行器每次调用引擎获取数据行的时候累加的

      row_examined表示在执行器扫描的数据行数,不代表执行引擎扫描的数据行数

    2. 有些时候MySQL占用内存涨得特别快,这是因为MySQL在执行过程中临时使用的内存是管理在连接对象里面的

      连接使用的一些内存资源只有在连接断开时释放。因此对于大查询或者长期使用的连接可以定期释放或者在5.7+执行mysql_reset_connection

  2. Jun 2024
  3. Feb 2023
    1. Most applications do not need to process massive amounts of data. This has led to a resurgence in data management systems with traditional architectures; SQLite, Postgres, MySQL are all growing strongly, while “NoSQL” and even “NewSQL” systems are stagnating.

      SQL still shines over NoSQL

  4. Jan 2023
    1. The DDGLC data are not accessible online as of yet. A migration of the database and the data into aMySQL target system is underway and will allow us to offer an online user interface by the end of 2017 Whatwe can already offer now is a by-product of our work, the Gertrud Bauer Zettelkasten Online.6'

      61 Available online at http://research.uni-leipzig.de/ddglc/bauerindex.html. The Work on this parergon to the lexicographical labors of the DDGLC project was funded by the Gertrud-und Alexander Böhlig-Stiftung. The digitization of the original card index was conducted by temporary collaborators and volunteers in the DDGLC project: Jenny Böttinger, Claudia Gamma, Tami Gottschalk, Josephine Hensel, Katrin John, Mariana Jung, Christina Katsikadeli, and Elen Saif. The IT concept and programming were carried out by Katrin John and Maximilian Moller.

      Digitization of Gertrud Bauer's zettelkasten was underway in 2017 to put the data into a MySQL database with the intention of offering it as an online user interface sometime in 2017.

  5. Dec 2022
  6. Aug 2022
    1. 使用@DynamicUpdate性能会好一些。因为不使用@DynamicUpdate时,即使没有改变的字段也会被更新
    1. 那么查询索引本身已经“覆盖”了需要的数据,不再需要回表查询。因此,这种情况也叫作索引覆盖

      extra中显示:use index,不是user index condition,也不是null

    1. version被其他事务抢先更新

      version拿到最新的当前值

    2. 事务B是当前读,必须要读最新版本,而且必须加锁,因此就被锁住了,必须等到事务C’释放这个锁,才能继续它的当前读。
    1. 事务中的MDL锁,在语句执行开始时申请,但是语句结束后并不会马上释放,而会等到整个事务提交后再释放。

      mdl只有在事务之后才会释放

    2. 比较理想的机制是,在alter table语句里面设定等待时间,如果在这个指定的等待时间里面能够拿到MDL写锁最好,拿不到也不要阻塞后面的业务语句,先放弃

      等待

    3. 使用参数–single-transaction的时候,导数据之前就会启动一个事务,来确保拿到一致性视图。而由于MVCC的支持,这个过程中数据是可以正常更新的。
    1. 第一原则是,如果通过调整顺序,可以少维护一个索引,那么这个顺序往往就是需要优先考虑采用的
    1. 恢复目标时间
    2. select * from information_schema.innodb_trx where TIME_TO_SEC(timediff(now(),trx_started))>60
    3. 就是当系统里没有比这个回滚日志更早的read-view的时候

      疑惑点

    4. MySQL的隔离级别设置为“读提交

      mysql默认是可重复读

    5. 创建一个视图
    6. 一个事务执行过程中看到的数据,总是跟这个事务在启动时看到的数据是一致的

      非所有数据,而是所读取行

    1. 感觉oracle的DG就诞生了,物理的速度也将远超逻辑的,毕竟只记录了改动向量
    2. redo log和binlog都可以用于表示事务的提交状态,而两阶段提交就是让这两个状态保持逻辑上的一致

      如果中间出现事故,mysql会做相应处理两阶段提交

  7. Mar 2022
    1. 真实环境,数据库服务器进程和客户端进程可能运行在不同的主机中,它们之间必须通过网络进行通讯。

      MySQL 采用 TCP 作为服务器和客户端之间的网络通信协议。

      在网络环境下,每台计算机都有一个唯一的 IP 地址,如果某个进程需要采用 TCP 协议进行网络通信方面的需求,可以向操作系统申请一个端口号,这是一个整数值,它的取值范围是 0~65535。

      这样在网络中的其他进程就可以通过 IP 地址 + 端口号的方式来与这个进程链接,这样进程之间就可以通过网络进行通信了。

    Tags

    Annotators

    1. 由图中可看到,每种设备都有两个指标: - 延时(响应时间):表示硬件的突发处理能力; - 带宽(吞吐量):代表硬件持续处理的能力。

    2. 大多数情况,性能最慢的设备会是瓶颈点。

      如,下载时网络速度可能会是瓶颈点,本地复制文件时硬盘可能是瓶颈点。

      为什么这些一般的工作能快速确认瓶颈点呢?

      因为我们队这些慢速设备的性能数据有一些基本的认识,如网络带宽是 2 Mbps,硬盘是每分钟 7200 转等等。

      (结论)因此,为了快速找到 SQL 的性能瓶颈点,需要了解计算机系统的硬件基本性能指标,如当前主流计算机性能指标数据。

    3. 数据库访问优化法则

      (目的):要正确的优化 SQL;

      (条件):需要快速定位性能的瓶颈点;

      (进一步阐释说明):即是快速找到 SQL 主要的开销在哪里?

  8. Oct 2021
  9. Jun 2021
    1. The alternative for curl is a credential file: A .netrc file can be used to store credentials for servers you need to connect to.And for mysql, you can create option files: a .my.cnf or an obfuscated .mylogin.cnf will be read on startup and can contain your passwords.
      • .netrc <--- alternative for curl to store secrets
      • .my.cnf or .mylogin.cnf <--- option files for mysql to store secrets
  10. May 2020
    1. I think you should normalize if you feel that introducing update or insert anomalies can severely impact the accuracy or performance of your database application.  If not, then determine whether you can rely on the user to recognize and update the fields together. There are times when you’ll intentionally denormalize data.  If you need to present summarized or complied data to a user, and that data is very time consuming or resource intensive to create, it may make sense to maintain this data separately.

      When to normalize and when to denormalize. The key is to think about UX, in this case the factors are db integrity (don't create errors that annoy users) and speed (don't make users wait for what they want)

    2. Can database normalization be taken too far?  You bet!  There are times when it isn’t worth the time and effort to fully normalize a database.  In our example you could argue to keep the database in second normal form, that the CustomerCity to CustomerPostalCode dependency isn’t a deal breaker.

      Normalization has diminishing returns

    3. Now each column in the customer table is dependent on the primary key.  Also, the columns don’t rely on one another for values.  Their only dependency is on the primary key.

      Columns dependency on the primary key and no dependency on other columns is how you get 2NF and 3NF

    4. A table is in third normal form if: A table is in 2nd normal form. It contains only columns that are non-transitively dependent on the primary key

      3NF Definition

  11. Dec 2019
  12. unix4lyfe.org unix4lyfe.org
    1. if you care at all about storing timestamps in MySQL, store them as integers and use the UNIX_TIMESTAMP() and FROM_UNIXTIME() functions.

      MySQL does not store offset

  13. Jul 2019
    1. WordPress Database Switcheroo First, do a MySQL database export of the old database on the old server, create a new blank database on the new server, import the old data either in PHPMyAdmin or mysql directly in the command line. Make sure you have the new database selected, then run some SQL updates and replacement commands on the tables notably, wp_options, wp_posts, wp_postmeta. Use the code as below and swap in your old and new URLs, no trailing slashes. Also if necessary change the table prefix values where applicable (ie wp_ ) UPDATE wp_options SET option_value = replace(option_value, 'http://www.oldurl', 'http://www.newurl') WHERE option_name = 'home' OR option_name = 'siteurl'; UPDATE wp_posts SET guid = replace(guid, 'http://www.oldurl','http://www.newurl'); UPDATE wp_posts SET post_content = replace(post_content, 'http://www.oldurl', 'http://www.newurl'); UPDATE wp_postmeta SET meta_value = replace(meta_value,'http://www.oldurl','http://www.newurl');

      This is really helpful when you're manually cloning a WP site. If you have a Domain of One's Own and you want to move a site from one user's account to another or fork a site into another user's account, this is really helpful.

  14. Apr 2019
  15. Mar 2019
    1. DBA Por Acaso: RDS, MySQL e Tuning para Iniciantes

      Outro assunto que não é explicitamente coberto por nossos tópicos, mas que é base importante para o administrador de sistemas na nuvem - e aqui coberto em um nível introdutório, para não assustar ninguém. E procura por Cloud em https://wiki.lpi.org/wiki/DevOps_Tools_Engineer_Objectives_V1 para ver como esse assunto é importante!

  16. Jan 2018
  17. Oct 2017
    1. MySQL’s replication architecture means that if bugs do cause table corruption, the problem is unlikely to cause a catastrophic failure.

      I can't follow the reasoning here. I guess it's not guaranteed to replicate the corruption like Postgres would, but it seems totally possible to trigger similar or identical corruption because the implementation of the logical statement would be similar on the replica.

  18. Mar 2015
    1. My current process for debugging stored procedures is very simple. I create a table called "debug" where I insert variable values from the stored procedure as it runs. This allows me to see the value of any variable at a given point in the script, but is there a better way to debug MySQL stored procedures?

      facing a similar issue