delay_key_write

 

This option applies only to MyISAM tables. It can have one of the following values to affect handling of the DELAY_KEY_WRITE table option that can be used in CREATE TABLE statements.

 

If DELAY_KEY_WRITE is enabled for a table, the key buffer is not flushed for the table on every index update, but only when the table is closed. This speeds up writes on keys a lot, but if you use this feature, you should add automatic checking of all MyISAM tables by starting the server with the --myisam-recover-options option (for example, --myisam-recover-options=BACKUP,FORCE). See Section 5.1.3, “Server Command Options”, and Section 14.4.1, “MyISAM Startup Options”.


Another performance option in MySQL is the DELAY_KEY_WRITE option. According to the MySQL documentation the option makes index updates faster because they are not flushed to disk until the table is closed.

Note that this option applies only to MyISAM tables,

You can enable it on a table by table basis using the following SQL statement:

ALTER TABLE sometable DELAY_KEY_WRITE = 1;

This can also be set in the advanced table options in the MySQL Query Browser.

This performance option could be handy if you have to do a lot of update, because you can delay writing the indexes until tables are closed. So frequent updates to large tables, may want to check out this option.

Ok, so when does MySQL close tables?

That should have been your next question. It looks as though tables are opened when they are needed, but then added to the table cache. This cache can be flushed manually with FLUSH TABLES; but here's how they are closed automatically according to the docs:

  • When the cache is full and a thread tries to open a table that is not in the cache.
  • When the cache contains more than table_cache entries and a thread is no longer using a table.
  • FLUSH TABLES; is called.

If DELAY_KEY_WRITE is enabled, this means that the key buffer for tables with this option are not flushed on every index update, but only when a table is closed. This speeds up writes on keys a lot, but if you use this feature, you should add automatic checking of all MyISAM tables by starting the server with the --myisam-recover option (for example, --myisam-recover=BACKUP,FORCE).

So if you do use this option you may want to flush your table cache periodically, and make sure you startup using the myisam-recover option.


 
插入大数据时,有索引会很慢,可以DISABLE KEYS,或者直接在table中加入DELAY_KEY_WRITE

注:

delay_key_write这个参数只对myisam类型表有效
如果你某个表需要经常update操作,这个参数就很管用!
但等delay_key_write使用时,出现断电或重启时,会导致在cache的索引update没来得及更新,所以必须在启动参数加上
–myisam-recover,或者在conf设置myisam-recover=BACKUP,FORCE。这样在你启动mysql的时候会检查你的
表并同步表和索引.

另外如果修复myisam类表可以在my.cnf中mysqld段设置myisam-recover恢复功能,参数有:default,force,backup,QUICK

1.
LOCK TABLES `test` WRITE;
ALTER TABLE `test` DISABLE KEYS ;

INSERT
INTO `test` VALUES
(1,'???',80,1),(2,'???',90,2),(1,'李四',80,3),(2,'王五',90,4),(1,'aa',12,5),
(3,'aa',123,6),(4,'aadwa',123,7);

ALTER TABLE `test` ENABLE KEYS;
UNLOCK TABLES;

2.

最近天天MySQL负载经常一会高,一会低的不稳定。
整天还被perl 折腾着,晕死了。
 
首先, iostat -x 1看看是不是 io 瓶颈较大。
iowait 才 0.45 见鬼,多又是程序问题。
top 一下看看了 mysqld 消耗CPU非常厉害
估计又是程序问题。
用我的per程序取了下 MySQL的数据
发现 key_writes / key_write_request 几乎接近 1了。晕
说明,update,delete, insert  语句非常平凡。
最后看了 binlog 仅1小时 有个表 有1w多次 UPDATE操作,
 
看来需要 打开 delay_key_write 了。
这个参数只对 MyISAM有效,可以再create table 时指定 delay_key_write ,如果表已经存在可以使用 alter table sometable delay_key_write =1;
 
如果你的某个表有很多update操作,这个参数的优势会很好的体现出来。因为这个参数能延迟更新索引到表关闭。
当我们需要经常跟新一个大表的时候,可以考虑使用这个参数。
 
那么,表关闭会在什么时候发生?你可以理解成当flash table的时候,表将关闭。那么有2种情况将会发生 flush table:
当cache 满了一个新的thread试图打开一个表的时候,那个表没有在cache;
当cache里的表数比table_cache多时thread不在使用表;
这个2种情况将会flush table。
 

当delay_key_write 使用的时候,如果出现重启或者掉电等情况,会导致在cache的索引update没来得及更新,所以必须在启动参数加上--myisam-recover,或者在conf设置myisam-recover=BACKUP,FORCE。这样在你启动mysql的时候会检查你的表并同步表和索引.

常用MySQL的童鞋都知道这个myisam类型的表极容易损坏,多数人可能都是用myisamchk命令来人工修复,下面介绍一种自动修复myisam的方法,也是我上午刚学的,共同进步,呵呵~
在MySQL的配置文件my.cnf中,启动项部分加入myisam-recover设置数据恢复功能,具体参数如下:
DEFAULT
与没有使用--myisam-recover选项相同。
BACKUP
如果在恢复过程中,数据文件被更改了,将tbl_name.MYD文件备份为tbl_name-datetime.BAK。
FORCE
即使.MYD文件将丢掉多个行也进行恢复。
QUICK
如果没有删除块,不要检查表中的行。
我设置了BACKUP和FORCE参数,如下:
[mysqld]
myisam-recover=BACKUP,FORCE

参考:

http://dev.mysql.com/doc/refman/5.5/en/server-system-variables.html#sysvar_delay_key_write

http://www.petefreitag.com/item/441.cfm

http://www.cnblogs.com/zhizhesky/archive/2011/08/22/2149357.html

MySQL DELAY_KEY_WRITE Option的更多相关文章

  1. mysql 有没有参数都报错“mysql: unknown option”

    报错: [root@XXXX tmp]# mysql -uroot -pmysql: unknown option '--You have new mail in /var/spool/mail/ro ...

  2. mysql 常用option

    [mysql 常用option] --host=host_name, -h host_name Connect to the MySQL server on the given host. --por ...

  3. Mysql –>EF edmx(model first)–> Sql server table

    一.mysql environment When we create an new database,first We need draw er diagram for somebody to sho ...

  4. Spark:将DataFrame写入Mysql

    Spark将DataFrame进行一些列处理后,需要将之写入mysql,下面是实现过程 1.mysql的信息 mysql的信息我保存在了外部的配置文件,这样方便后续的配置添加. //配置文件示例: [ ...

  5. Spark:读取mysql数据作为DataFrame

    在日常工作中,有时候需要读取mysql的数据作为DataFrame数据源进行后期的Spark处理,Spark自带了一些方法供我们使用,读取mysql我们可以直接使用表的结构信息,而不需要自己再去定义每 ...

  6. 在oracle配置mysql数据库的dblink

    本文介绍如何在oracle配置mysql数据库的dblink:虽然dblink使用很占资源:俗称“性能杀手”.但有些场景不得不使用它.例如公司使用数据库是oracle:可能其他部门或者CP合作公司使用 ...

  7. Mysql 5.6 MHA (gtid) on Kylin

    mha on Kylinip hostname repl role mha role192.168.19.69 mysql1 master node192.168.19.73 mysql2 slave ...

  8. MySQL/MariaDB数据库的主主复制

      MySQL/MariaDB数据库的主主复制 作者:尹正杰  版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.主主复制概述 1>.什么是主主复制 所谓的主主复制,说白了就是两台节点互为 ...

  9. MySQL SSL配置(mysql5.7和mysql5.6)

    专题一:mysql5.7上开启并配置ssl [root@mysqlmaster01 bin]# ./mysql_ssl_rsa_setup --datadir=/data/mysql_data1/ - ...

随机推荐

  1. 50-Identity MVC:DbContextSeed初始化

    1-创建一个可以启动时如果没有一个账号刚创建1个新的账号 namespace MvcCookieAuthSample.Data { public class ApplicationDbContextS ...

  2. 【財務会計】BS科目とは・PL科目とは

    「BS科目」「PL科目」という言葉がありますが.聞いたことあるけどよくわからん!っていう人は多いと思います.なので.簡単にご説明を. BS科目は「いくらあるか」 「BS科目」は.「B/S科目」と書くこ ...

  3. Bug是一种财富-------研发同学的错题集、测试同学的遗漏用例集

    此文已由作者王晓明授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 各位看官,可能看到标题的你一定认为这是一篇涉嫌"炒作"的文章,亦或是为了吸引眼球而起的标 ...

  4. 一次和别人争吵一个按钮,点击后显示导航;再点击不显示的效果,是否一定以及必须用js?

    事情经过是这样的,我们组一个说话很喜欢用一定,肯定的哥们,吃午饭的时候拿了自己做的一个UI库,头部有一个按钮 点击展开,再次点击收缩,他意思说一个按钮无法记录点击状态,必须使用js.然后我看了一眼,心 ...

  5. 【APUE】Chapter15 Interprocess Communication

    15.1 Introduction 这部分太多概念我不了解.只看懂了最后一段,进程间通信(IPC)内容被组织成了三个部分: (1)classical IPC : pipes, FIFOs, messa ...

  6. 每天一个Linux命令(12):su命令

    su命令用于切换当前用户身份到其他用户身份,变更时须输入所要变更的用户帐号与密码. 语法: su(选项)(参数) 选项: -c<指令>或--command=<指令>:执行完指定 ...

  7. 游戏测试中遇到的奇葩bug(不断整理中...)

    1:跨服组织战中,不同服务器相同组织ID的敌对玩家不能造成伤害. 2:节日活动24点开启,角色不下线自然过渡到活动开启,界面显示异常 3:前端请求数据之后,不管是否接收到后端返回的数据,只要玩家点击仙 ...

  8. LuffyCity-CMDB实战

    第1章 章节一 课时01-ITIL介绍 课时02-CMDB介绍 课时03-CMDB需求讨论 课时04-CMDB需求讨论2 课时05-CMDB表结构设计 课时06-CMDB表结构设计2 课时07-CMD ...

  9. python基础篇 08 文件操作

    本节主要内容:1. 初识⽂件操作2. 只读(r, rb)3. 只写(w, wb)4. 追加(a, ab)5. r+读写6. w+写读7. a+写读(追加写读)8. 其他操作⽅法9. ⽂件的修改以及另⼀ ...

  10. 使用HashOperations操作redis

    方法 c参数 s说明 Long delete(H key, Object... hashKeys); H key:集合key Object... hashKeys:key对应hashkey  删除ma ...