mysql中min和max查詢優化

2021-07-31 13:58:34 字數 1360 閱讀 2845

但是往往min()或者max()函式往往會造成全表掃瞄. 那麼如何快速查詢出自己想要的資料呢, 請看我下面做的測試:

首先來看一下表結構:

create table `biggoods` (

`goods_id` int(10) unsigned not null auto_increment,

`cat_id` int(10) unsigned not null,

`goods_name` varchar(32) not null,

`click_count` int(11) not null default '0',

`goods_number` int(11) not null default '0',

`is_delete` int(11) not null default '0',

primary key (`goods_id`),

key `goods_cat_id` (`goods_id`,`cat_id`),

key `cat_goods_id` (`cat_id`,`goods_id`)

) engine=innodb auto_increment=34143982 default charset=utf8 |

goods表中大概有三千萬行資料

用時4.45毫秒

用時0.00公釐

普通語句:

優化後的sql語句:

通過explain清楚的看到普通sql語句和優化後的sql語句都進行了index索引掃瞄, 但是優化後的sql語句在explain中的rows欄位中顯示只掃瞄了1行資料, 而普通語句確掃瞄了2012行資料. 這一切的緣由就是因為在查詢中,優化後的語句用order by進行排序(索引是自帶排序功能的)後limit 1, 使其僅僅進行了1行資料的掃瞄

SQL中MAX 和MIN 的用法

select max column name min column name from table name1 查詢teacher表中教師的最大年齡。例項 select max age as max age from teacher這樣只能查出來乙個最大值,不能查出教師的id,name等其他資訊。2...

C語言 min和max標頭檔案

min和max標頭檔案 雖然說求最大值最小值函式在哪個標頭檔案下並不是非常重要,但是遇到問題的時候我們很快的找到 msdn上說在algorithm下,但是出錯了,其實這兩個函式需要包含兩個標頭檔案和檔案,其他的還有 min和 max需要包含標頭檔案。include iostream include ...

Linux核心中的Min和Max函式

今天看 時看到乙個有趣的東東,就是linux核心也有min函式,但它的實現很是奇怪,先貼出來 min max macros that also do strict type checking.see the unnecessary pointer comparison.define min x,y ...