溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Mysql表關(guān)聯(lián)字段未建索引導(dǎo)致查詢慢,優(yōu)化后查詢效率顯著提升

發(fā)布時間:2020-08-05 10:30:23 來源:ITPUB博客 閱讀:226 作者:shawnloong 欄目:MySQL數(shù)據(jù)庫
今天收到用戶反饋前端頁面打開很慢。數(shù)據(jù)庫服務(wù)器負載也告警了。
登錄服務(wù)器查詢Mysql占用CPU過高,很直接打開show full process 跟慢查詢發(fā)現(xiàn)很多以下sql都是在10S以上
# User@Host: gyw[gwy] @  [x.x.x.x]  Id: 19513
# Query_time: 11.326904  Lock_time: 0.000327 Rows_sent: 69  Rows_examined: 1417696
SET timestamp=1504507662;
SELECT odet.seller AS sellerId,
           odet.agreementprice_id AS agreementpriceId,
           odet.customer_id AS customerId,
       (SELECT realname
          FROM sys_user suser
         WHERE suser.id = odet.seller)
          AS sellerName,
       odet.pkgticket_id AS pkgId,
       odet.pkgticket_price AS pkgPrice,
       DATE_FORMAT(odet.sell_time, '%Y-%m-%d') AS sellTime,
         sum(oct.tourist_number-IFNULL(ort.tourist_remain,0)) as totalPeople,
     sum((oct.tourist_number-IFNULL(ort.tourist_remain,0))*odet.pkgticket_price) as totalMoney,
       (SELECT name
          FROM scenic_pkgticket spkg
         WHERE spkg.id = odet.pkgticket_id)
          AS pkgticketName
  FROM  order_detail odet,order_checkticket oct
        LEFT JOIN order_refundticket ort
        on oct.id = ort.id
   WHERE odet.id=oct.order_detail_id
           and odet.scenic_id = 215
           and odet.sell_time >= '2017-09-04 00:00:00'
           and odet.sell_time <= '2017-09-04 23:59:59'
           GROUP BY sellerId, sellTime, pkgId, pkgPrice
           WITH ROLLUP;


手動查看一下執(zhí)行計劃發(fā)現(xiàn),使用Using temporary; Using filesort使用到了臨時表,這樣效率是最差的
explain SELECT odet.seller AS sellerId,
    ->        odet.agreementprice_id AS agreementpriceId,
    ->        odet.customer_id AS customerId,
    ->        (SELECT realname FROM sys_user suser WHERE suser.id = odet.seller) AS sellerName,
    ->        odet.pkgticket_id AS pkgId,
    ->        odet.pkgticket_price AS pkgPrice,
    ->        DATE_FORMAT(odet.sell_time, '%Y-%m-%d') AS sellTime,
    ->        sum(oct.tourist_number - IFNULL(ort.tourist_remain, 0)) as totalPeople,
    ->        sum((oct.tourist_number - IFNULL(ort.tourist_remain, 0)) *
    ->            odet.pkgticket_price) as totalMoney,
    ->        (SELECT name
    ->           FROM scenic_pkgticket spkg
    ->          WHERE spkg.id = odet.pkgticket_id) AS pkgticketName
    ->   FROM order_detail odet, order_checkticket oct
    ->   LEFT JOIN order_refundticket ort
    ->     on oct.id = ort.id
    ->  WHERE odet.id = oct.order_detail_id
    ->    and odet.scenic_id = 215
    ->    and odet.sell_time >= '2017-09-04 00:00:00'
    ->    and odet.sell_time <= '2017-09-04 23:59:59'
    ->  GROUP BY sellerId, sellTime, pkgId, pkgPrice WITH ROLLUP;
+----+--------------------+-------+--------+---------------+---------+---------+----------------------------+--------+---------------------------------+
| id | select_type        | table | type   | possible_keys | key     | key_len | ref                        | rows   | Extra                           |
+----+--------------------+-------+--------+---------------+---------+---------+----------------------------+--------+---------------------------------+
|  1 | PRIMARY            | oct   | ALL    | NULL          | NULL    | NULL    | NULL                       | 414589 | Using temporary; Using filesort |
|  1 | PRIMARY            | ort   | eq_ref | PRIMARY       | PRIMARY | 8       | sd_ets.oct.id              |      1 | NULL                            |
|  1 | PRIMARY            | odet  | eq_ref | PRIMARY       | PRIMARY | 8       | sd_ets.oct.order_detail_id |      1 | Using where                     |
|  3 | DEPENDENT SUBQUERY | spkg  | eq_ref | PRIMARY       | PRIMARY | 8       | func                       |      1 | NULL                            |
|  2 | DEPENDENT SUBQUERY | suser | eq_ref | PRIMARY       | PRIMARY | 8       | func                       |      1 | NULL                            |
+----+--------------------+-------+--------+---------------+---------+---------+----------------------------+--------+---------------------------------+


嘗試在在order_detail 上加一個復(fù)合索引(scenic_id,sell_time),但是依然如此未走索引,仔細檢查發(fā)現(xiàn)order_checkticket order_detail_id未建索引。加上索引后執(zhí)行計劃如下
+----+--------------------+-------+--------+-------------------------+-----------------+---------+----------------+------+--------------------------------------------------------+
| id | select_type        | table | type   | possible_keys           | key             | key_len | ref            | rows | Extra                                                  |
+----+--------------------+-------+--------+-------------------------+-----------------+---------+----------------+------+--------------------------------------------------------+
|  1 | PRIMARY            | odet  | range  | PRIMARY,idx_od_si_stime | idx_od_si_stime | 14      | NULL           |  183 | Using index condition; Using temporary; Using filesort |
|  1 | PRIMARY            | oct   | ref    | idx_oct_odi             | idx_oct_odi     | 8       | sd_ets.odet.id |    1 | NULL                                                   |
|  1 | PRIMARY            | ort   | eq_ref | PRIMARY                 | PRIMARY         | 8       | sd_ets.oct.id  |    1 | NULL                                                   |
|  3 | DEPENDENT SUBQUERY | spkg  | eq_ref | PRIMARY                 | PRIMARY         | 8       | func           |    1 | NULL                                                   |
|  2 | DEPENDENT SUBQUERY | suser | eq_ref | PRIMARY                 | PRIMARY         | 8       | func           |    1 | NULL                                                   |
+----+--------------------+-------+--------+-------------------------+-----------------+---------+----------------+------+--------------------------------------------------------+
5 rows in set (0.00 sec)
我們看key已經(jīng)走了索引使用idx_od_si_stime
查詢速度只要0.01毫秒。提升速度上千倍

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI