V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
MySQL 5.5 Community Server
MySQL 5.6 Community Server
Percona Configuration Wizard
XtraBackup 搭建主从复制
Great Sites on MySQL
Percona
MySQL Performance Blog
Severalnines
推荐管理工具
Sequel Pro
phpMyAdmin
推荐书目
MySQL Cookbook
MySQL 相关项目
MariaDB
Drizzle
参考文档
http://mysql-python.sourceforge.net/MySQLdb.html
hellohacker
V2EX  ›  MySQL

1 个困扰我 3 个多月没解决的 mysql 性能问题

  •  
  •   hellohacker · 2014-06-18 18:56:02 +08:00 · 5199 次点击
    这是一个创建于 3605 天前的主题,其中的信息可能已经有所发展或是发生改变。
    这个问题研究了n次,每次都以失败告终,今天又突然想研究下,但是还是没有进展。大家帮忙看下。先谢过了!
    我描述下问题,我手头有个网站,用PHP+mysql+iis+windows 2003 配置,网站间歇性变慢,大概持续15分钟,每天1-2次。网站变慢后,网站打开速度从5s提高的20s,慢的速度难以让人接受。
    在网站变慢的时候,我查看了下服务器cpu使用,不到10%。然后使用mysql命令
    show processlist;

    打印出结果如下:
    mysql> show processlist;
    +----------+------+-----------------+------+---------+------+--------------------+------------------+
    | Id | User | Host | db | Command | Time | State | Info |
    +----------+------+-----------------+------+---------+------+--------------------+------------------+
    | 19021447 | root | 127.0.0.1:28253 | o2o | Query | 17 | removing tmp table | DESC fanwe_user |
    | 19025616 | root | 127.0.0.1:32578 | NULL | Query | 0 | NULL | show processlist |
    +----------+------+-----------------+------+---------+------+--------------------+------------------+

    持续打印 show processlist;命令 state还会变成close table等状态 Time时间有时候会增加到20s。
    sql语句 DESC fanwe_user 我在phpmyadmin里执行 执行时间几乎统计不到,为什么在这里要花费 20s的查询时间呢
    我去查询了下 mysql 慢查询日志:
    输出结果 如下:
    # Time: 140611 10:14:16
    # User@Host: root[root] @ [127.0.0.1]
    # Query_time: 6.218750 Lock_time: 0.000000 Rows_sent: 67 Rows_examined: 67
    SET timestamp=1402452856;
    DESC fanwe_user;
    # Time: 140611 10:14:30
    # User@Host: root[root] @ [127.0.0.1]
    # Query_time: 7.718750 Lock_time: 0.015625 Rows_sent: 67 Rows_examined: 67
    SET timestamp=1402452870;
    DESC fanwe_user;
    # Time: 140611 10:14:40
    # User@Host: root[root] @ [127.0.0.1]
    # Query_time: 7.531250 Lock_time: 0.000000 Rows_sent: 67 Rows_examined: 67
    SET timestamp=1402452880;
    DESC fanwe_user;
    # Time: 140611 10:15:12
    # User@Host: root[root] @ [127.0.0.1]
    # Query_time: 23.765625 Lock_time: 0.000000 Rows_sent: 67 Rows_examined: 67
    SET timestamp=1402452912;
    DESC fanwe_user;
    # Time: 140611 10:15:28
    # User@Host: root[root] @ [127.0.0.1]
    # Query_time: 10.296875 Lock_time: 0.000000 Rows_sent: 67 Rows_examined: 67
    SET timestamp=1402452928;
    DESC fanwe_user;
    # Time: 140611 10:15:53
    # User@Host: root[root] @ [127.0.0.1]
    # Query_time: 10.812500 Lock_time: 0.000000 Rows_sent: 67 Rows_examined: 67
    SET timestamp=1402452953;
    DESC fanwe_user;
    。。。。。后面还有很多 都是在DESC table。有时候 table的名字会变化。但只是2个表


    现在对于这个一点头绪没有,希望各位能帮忙解决下。

    ===================================

    mysql> show global variables like '%table%';
    +----------------------------------------+----------+
    | Variable_name | Value |
    +----------------------------------------+----------+
    | big_tables | OFF |
    | innodb_file_per_table | OFF |
    | innodb_table_locks | ON |
    | lower_case_table_names | 1 |
    | max_heap_table_size | 16777216 |
    | max_tmp_tables | 32 |
    | old_alter_table | OFF |
    | performance_schema_max_table_handles | 100000 |
    | performance_schema_max_table_instances | 50000 |
    | sql_big_tables | OFF |
    | table_definition_cache | 400 |
    | table_open_cache | 1520 |
    | tmp_table_size | 70254592 |
    | updatable_views_with_limit | YES |
    +----------------------------------------+----------+
    14 rows in set (0.00 sec)
    =========================show create table fanwe_user 数据==================================
    mysql> show create table fanwe_user\G;
    *************************** 1. row ***************************
    Table: fanwe_user
    Create Table: CREATE TABLE `fanwe_user` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `user_name` varchar(255) NOT NULL,
    `user_pwd` varchar(255) NOT NULL,
    `create_time` int(11) NOT NULL,
    `update_time` int(11) NOT NULL,
    `login_ip` varchar(255) NOT NULL,
    `group_id` int(11) NOT NULL,
    `is_effect` tinyint(1) NOT NULL,
    `is_delete` tinyint(1) NOT NULL,
    `email` varchar(255) NOT NULL,
    `mobile` varchar(255) NOT NULL,
    `score` int(11) NOT NULL,
    `money` double(20,4) NOT NULL,
    `verify` varchar(255) NOT NULL,
    `code` varchar(255) NOT NULL COMMENT '鐧诲綍鐢ㄧ殑鏍囪瘑鐮?,
    `pid` int(11) NOT NULL,
    `login_time` int(11) NOT NULL,
    `referral_count` int(11) NOT NULL,
    `password_verify` varchar(255) NOT NULL,
    `integrate_id` int(11) NOT NULL,
    `sina_id` int(11) NOT NULL,
    `renren_id` int(11) NOT NULL,
    `kaixin_id` int(11) NOT NULL,
    `sohu_id` int(11) NOT NULL,
    `lottery_mobile` varchar(255) NOT NULL,
    `lottery_verify` varchar(255) NOT NULL,
    `verify_create_time` int(11) NOT NULL,
    `tencent_id` varchar(255) NOT NULL,
    `referer` varchar(255) NOT NULL,
    `login_pay_time` int(11) NOT NULL,
    `focus_count` int(11) NOT NULL COMMENT '鍏虫敞鍒汉鐨勬暟閲?,
    `focused_count` int(11) NOT NULL COMMENT '绮変笣鏁?,
    `province_id` int(11) NOT NULL,
    `city_id` int(11) NOT NULL,
    `sex` tinyint(1) NOT NULL DEFAULT '-1',
    `my_intro` varchar(255) NOT NULL,
    `is_merchant` tinyint(1) NOT NULL,
    `merchant_name` varchar(255) NOT NULL,
    `is_daren` tinyint(1) NOT NULL,
    `daren_title` varchar(255) NOT NULL,
    `step` tinyint(1) NOT NULL,
    `byear` int(4) NOT NULL,
    `bmonth` int(2) NOT NULL,
    `bday` int(2) NOT NULL,
    `locate_time` int(11) DEFAULT '0' COMMENT '鐢ㄦ埛鏈

    另外,我还有个问题没有说,我的这个数据库选择的是innodb类型的,但是我的表都是MyISAM类型的,不知道这会不会影响性能
    6 条回复    2014-06-19 10:19:27 +08:00
    cevincheung
        1
    cevincheung  
       2014-06-18 19:02:04 +08:00
    方维使用的是thinkphp的框架,这个SQL是框架在获取表结构,有一个开关可以打开缓存,只获取一次。在lib/core/convxxxxx.php(依稀记得是这个路径)你看看哪里有配置项吧。然后打开all_in_one选项
    mahone3297
        2
    mahone3297  
       2014-06-18 19:15:17 +08:00
    @cevincheung 看来是熟悉使用 “方维” 的高人啊。。。
    不过说到底,为什么会慢呢?这个原因好像还是没说。。。
    cevincheung
        3
    cevincheung  
       2014-06-18 19:20:34 +08:00
    @mahone3297
    -.- MYSQL不甚了解。pgsql可以帮忙
    billlee
        4
    billlee  
       2014-06-18 19:41:02 +08:00
    关掉 innodb_stats_on_metadata 试试?
    minbaby
        5
    minbaby  
       2014-06-19 09:54:00 +08:00
    如果觉得是 mysql 问题,可以把慢查询打开看下
    yueyoum
        6
    yueyoum  
       2014-06-19 10:19:27 +08:00
    虽同样不明白为何变慢
    但 建议LZ 以后可以把这个 user 表拆分一下。

    - 不太变动的 和经常变动的。 这一条不做也可以,毕竟现在memcache, redis缓存也很好用
    - 把第三方帐号信息做成外键,这样好扩展。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   769 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 20:54 · PVG 04:54 · LAX 13:54 · JFK 16:54
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.