阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

MySQL的SQL语句优化一例

128次阅读
没有评论

共计 2588 个字符,预计需要花费 7 分钟才能阅读完成。

今天在系统上看到一条 SQL 运行时间达到 9 秒,不符合规范要求,优化之,sql 如下:sql 强制用 into_time 索引

  1. # Time: 20170214T11:35:01.594499+08:00
  2. # User@Host: oms_readonly[oms_readonly] @ [10.44.xxx.xxx] Id: 41636892
  3. # Query_time: 9.299612 Lock_time: 0.000124 Rows_sent: 20 Rows_examined: 2439330
  4. SET timestamp=1487043301;
  5. select * from customers force index(`into_time`) where `type` = 1 AND `status` < 7 AND `isarea` = 8 AND `into_time`<=‘2017-01-31 23:59:59’ order by score desc limit 0,20;

查看表的相关状态:

  1. MySQL> show table status like ‘customers’ \G;
  2. *************************** 1. row ***************************
  3.            Name: customers
  4.          Engine: InnoDB
  5.         Version: 10
  6.      Row_format: Dynamic
  7.            Rows: 2504609
  8.  Avg_row_length: 710
  9.     Data_length: 1780383744
  10. Max_data_length: 0
  11.    Index_length: 1253048320
  12.       Data_free: 6291456
  13.  Auto_increment: 2546101
  14.     Create_time: 2017-01-07 01:59:34
  15.     Update_time: 2017-02-14 13:58:17
  16.      Check_time: NULL
  17.       Collation: utf8_general_ci
  18.        Checksum: NULL
  19.  Create_options:
  20.         Comment:
  21. 1 row in set (0.00 sec)

表一共大约有 250 万行记录,查看 下满足into_time<=‘2017-01-31 23:59:59’ 这个条件的有多少行

    mysql> select count(*) from customers where `into_time`<=‘2017-01-31 23:59:59’;

    1. ++
    2. | count(*) |
    3. ++
    4. | 2439147 |
    5. ++
    6. 1 row in set (0.95 sec)


    显然into_time 这个列的索引已经不合适了,查看下表上都有那些索引

    1. Create Table: CREATE TABLE `customers` (
    2.   `id` int(11) NOT NULL AUTO_INCREMENT,
    3.   PRIMARY KEY (`id`),
    4.   KEY `newdata` (`newdata`),
    5.   KEY `cusname` (`cusname`),
    6.   KEY `type` (`type`,`ownerid`),
    7.   KEY `operator` (`operator`),
    8.   KEY `into_time` (`into_time`),
    9.   KEY `isarea` (`isarea`),
    10.   KEY `linkcase` (`linkcase`),
    11.   KEY `score` (`score`),
    12.   FULLTEXT KEY `fdx_cusname` (`cusname_idx`)
    13. ) ENGINE=InnoDB AUTO_INCREMENT=2546101 DEFAULT CHARSET=utf8
    14. 1 row in set (0.00 sec)

    可以看到 score 列有索引,如果能采用这个列的索引是个比较好的选择,去掉强制索引看下执行计划

      1. mysql> explain select * from customers where `type` = 1 AND `status` < 7 AND `isarea` = 8 AND `into_time`<=‘2017-01-31 23:59:59’ order by score desc limit 0,20;
      2. +++++++++++++
      3. | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
      4. +++++++++++++
      5. | 1 | SIMPLE | customers | NULL | index | type,into_time,isarea,status,idx_isarea_renew_owner,type_status | score | 2 | NULL | 270 | 0.92 | Using where |
      6. +++++++++++++
      7. 1 row in set, 1 warning (0.00 sec)

    可以看到用了 score 索引,执行时间从最 9 秒多,到优化后的 0.0 几秒。

    本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-02/140652.htm

    正文完
    星哥说事-微信公众号
    post-qrcode
     0
    星锅
    版权声明:本站原创文章,由 星锅 于2022-01-22发表,共计2588字。
    转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
    【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
    阿里云-最新活动爆款每日限量供应
    评论(没有评论)
    验证码
    【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中