1. 程式人生 > >mysql 5.6 order by Limit執行效率問題

mysql 5.6 order by Limit執行效率問題

今天研發的同事反饋一個sql執行140+s但是sql很簡單,也有索引,那麼問題出在哪裡呢?

經過排查發現,mysql中,order by limit 一起用的時候是有問題的不是我們常用的思路,下面舉例說明:

select tid, productname, pic, minorder, minorderunit from `f_product` where cid = 6234052 and `status`=1   order by repubtime desc limit 8

mysql> explain select tid, productname, pic, minorder, minorderunit from `f_product` where cid = 6234052 and `status`=1   order by repubtime desc limit 8\G;
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: f_product
         type: index
possible_keys: cid_status_endtime_repubtime,status_repubtime,status_endtime,status_tid,status_pubtime_tid
          key: repubtime

      key_len: 5
          ref: NULL
         rows: 9486
        Extra: Using where
1 row in set (0.00 sec)


ERROR: 
No query specified

不加Limit8 時:

mysql> explain select tid, productname, pic, minorder, minorderunit from `f_product` where cid = 6234052 and `status`=1   order by repubtime desc \G;
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: f_product
         type: ref
possible_keys: cid_status_endtime_repubtime,status_repubtime,status_endtime,status_tid,status_pubtime_tid
          key: cid_status_endtime_repubtime


      key_len: 6
          ref: const,const
         rows: 12950
        Extra: Using where; Using filesort
1 row in set (0.00 sec)


ERROR: 
No query specified

發現加limit,和不加走的索引不一樣。測試當不加Limit時執行時間:

mysql>  select tid, productname, pic, minorder, minorderunit from `f_product` where cid = 6234052 and `status`=1   order by repubtime desc;

7183 rows in set (0.03 sec)

mysql>  select tid, productname, pic, minorder, minorderunit from `f_product` where cid = 6234052 and `status`=1   order by repubtime desc limit 8;

8 rows in set (0.68 sec)

發現效率慢了20倍。

而且發現:limit 後面的值越大,影響的行數越多,例如limit 1 ,limit 5, limit8影響的行數都是不一樣的。

經線下測試發現:limit 偏移量越小,用Limit效率越高,limit 越大效率越差。

所以在這裡我調整了sql:

mysql> select tid, productname, pic, minorder, minorderunit from (select tid, productname, pic, minorder, minorderunit from `f_product` where cid = 6234052 and `status`=1 order by repubtime desc ) c limit 8; 

 8 rows in set (0.04 sec)    //執行時間縮小了17被。

同時也發現 :我們在order by  limit 一起時 執行順序不是按照:where ------order by ------ limit

                    而是:order by ----- limit -------where的順序去執行,這樣就會有一個問題,按照我們管用的思路,上面的查詢肯定是會丟失資料的。具體後面遇到再測試。

目前我們的資料庫版本是mysql5.6.10不知道其他版本的如何,這可能是mysql5.6版本的一個優化設計。

相關推薦

mysql 5.6 order by Limit執行效率問題

今天研發的同事反饋一個sql執行140+s但是sql很簡單,也有索引,那麼問題出在哪裡呢? 經過排查發現,mysql中,order by limit 一起用的時候是有問題的不是我們常用的思路,下面舉例說明: select tid, productname, pic, m

mysql order by limit 問題

name hsb 出現 .cn cnblogs test mit width 1-1 問題 不使用limit時 使用limit出現問題了: 原因是出現相同值 gaoxiong 解決辦法:使用多個字段排序,上述例子可以改成 select * from hsb_test or

mysql order by limit 的一個坑

需求:一次對錶中單行的值進行計數排序 發現的問題: 對單個無索引的欄位進行排序後limit .發現當被排序欄位有相同值時並且在limit範圍內,取的值並不是正常排序後的值,也就是說,當排在第N行的資料可取key1、 key2 時 , 排序結果可能是key1,也

mysql group by order by limit 1

SELECT course_id, MAX(id) mid, COUNT(id) FROM org_class WHERE course_id IN(1, 102) GROUP BY course_id

MySQL5.6limit的工作機制和order by limit優化原理

MySQL5.6中Limit的工作機制 如果你僅需要在一個結果集中返回特定的幾行,通常是使用limit,而不是取回整個結果集再捨去不需要的資料,MySQL通常按照如下的方式優化一個包含limit row_count或HAVING的語句: ◎只有limit 如果你只通過li

MySQL實驗 內連線優化order by+limit 以及新增索引再次改進

# MySQL實驗 內連線優化order by+limit 以及新增索引再次改進 在進行[子查詢優化雙引數limit](https://www.cnblogs.com/G-Aurora/p/13254473.html)時我萌生了測試更加符合實際生產需要的`ORDER BY + LIMIT`的想法,或許我們也

CentOS 7 安裝MySQL 5.6遇到問題及解決方案

linux localhost not ces name 解決 info target case centos下安裝mysql真的沒有想象中那麽容易,在這裏我總結一下遇到的問題 1. ERROR 2002 (HY000): Can’t connect to loca

linux CentOS6.5 yum安裝mysql 5.6

linux centos6.5 yum安裝mysql 5.6直接按步驟安裝# wget # rpm -ivh mysql-community-release-el6-5.noarch.rpm [[email protected]/* */ ~]# yum repolist all | grep

centos6.8 yum安裝mysql 5.6

centos6.8 yum安裝mysql 5.6一、檢查系統是否安裝其他版本的MYSQL數據 yum list installed | grep mysql yum -y remove mysql-libs.x86_64 二、安裝及配置 wget http://repo.mysql.com/mysql-co

mysqlmysql 5.6不停機主從搭建(一主一從基於GTID復制)

從庫 creat 不停機 event rep ply copy from end 環境說明:版本 version 5.6.25-log 主庫ip: 10.219.24.25從庫ip:10.219.24.22os 版本: centos 6.7已安裝熱備軟件:xtrabacku

mysqlmysql 5.6不停機雙主一從搭建(活躍雙主一從基於日誌點復制)

stat 5.6 create 文件夾 eat ima send spec tar 環境說明:版本 version 5.6.25-log 主1庫ip: 10.219.24.25主2庫ip: 10.219.24.22從1庫ip:10.219.24.26os 版本: cento

CentOS 6.5 二進制安裝MySQL 5.6

二進制安裝mysql 5.6CentOS 6.5 二進制安裝MySQL 5.6--------------------------------下載MySQL5.6二進制包mysql-5.6.20-linux-glibc2.5-x86_64.tar.gz安裝相關依賴包yum install libaio*use

MySQL 5.6升級至MySQL 5.7--------版本升級最佳實戰

mysql dba1. 背景 MySQL 5.7是當前MySQL最新版本,與MySQL 5.6版本相比,有如下特征  * 性能和可擴展性:改進 InnoDB 的可擴展性和臨時表的性能,從而實現更快的網絡和大數據加載等操作。 * JSON支持:使用 MySQL 的 JSON 功能,你可以結合 NoSQL

MySQL 5.6--------SSL連接最佳實戰

ssl mysql dba 1. 背景 * 在生產環境下,安全總是無法忽視的問題,數據庫安全則是重中之重,因為所有的數據都存放在數據庫中 * 當使用非加密方式連接MySQL數據庫時,在網絡中傳輸的所有信息都是明文的,可以被網絡中所有人截取,敏感信息可能被泄露。在傳送敏感信息(如密碼)時,可以采

mysql 5.6.25編譯安裝詳細步驟

dir 支持斷點續傳 啟動項 ++ roo blog eve files 文件 1 #安裝依賴 2 yum -y install make gcc-c++ cmake bison-devel ncurses-devel perl vim 3 4

mysql-5.6.17-win32免安裝版配置

user .exe rem rac mod 環境變量配置 登錄 setting bst mysql-5.6.17-win32免安裝版配置詳解 工具/原料 windows 2003 R2 mysql-5.6.17-win32.zip

windows10 安裝 mysql 5.6 教程

cef ef7 自動安裝 步驟 date 進度 電腦 eat 技術分享 首先是下載 mysql-installer-community-5.6.14.0.msi ,大家可以到 mysql 官方網去下載。 win10的安全機制比較嚴格,安裝前最好到<設置>

CentOS6.9上安裝mysql-5.6.36

tmp event 包名 conf cli group tor 進制 ins 本文參考自博客園,鳴謝原作者。 1、準備數據存放的文件系統 新建一個邏輯卷,並將其掛載至特定目錄即可。這裏不再給出過程。 這裏假設其邏輯卷的掛載目錄為/data,而後需要創建/data/mysql

centos 6.5安裝MySQL 5.6.35

linux mysql下載軟件[[email protected]/* */ src]# wget -q http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz解壓[[email 

CentOS 6.9上安裝mysql-5.6.37

初始 replica mil using logs ces def pan shutdown CentOS 6.9上安裝mysql-5.6.37 1、準備數據存放的文件系統 新建一個邏輯卷,並將其掛載至特定目錄即可。這裏不再給出過程。 這裏假設其邏輯卷的掛載目錄為/d