虽然有索引,但没有用上
生活随笔
收集整理的這篇文章主要介紹了
虽然有索引,但没有用上
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
select??count(id)?from?cin_offlinemessage?where?`datetime`?>=?'2012-03-15'?and?state=0;? 結(jié)果是全表掃描。 mysql>?explain??select??count(id)?from?cin_offlinemessage?where?`datetime`?>=?'2012-03-15'?and?state=0;??? +----+-------------+--------------------+------+------------------------------+------+---------+------+----------+-------------+ |?id?|?select_type?|?table??????????????|?type?|?possible_keys????????????????|?key??|?key_len?|?ref??|?rows?????|?Extra???????| +----+-------------+--------------------+------+------------------------------+------+---------+------+----------+-------------+ |??1?|?SIMPLE??????|?cin_offlinemessage?|?ALL??|?cin_offflinemessage_datetime?|?NULL?|?NULL????|?NULL?|?45046637?|?Using?where?| +----+-------------+--------------------+------+------------------------------+------+---------+------+----------+-------------+ 1?row?in?set?(0.00?sec) 雖然有索引,但沒(méi)有用上,掃描的行數(shù)太多了(8位數(shù)),優(yōu)化器認(rèn)為全表掃描比索引來(lái)的快。 后我修改了下 mysql>?explain??select??count(id)?from?cin_offlinemessage?where?`datetime`?between?'2012-03-15?00:00:00'?and?'2012-03-16?23:59:59'??and?state=0; +----+-------------+--------------------+-------+------------------------------+------------------------------+---------+------+---------+-------------+ |?id?|?select_type?|?table??????????????|?type??|?possible_keys????????????????|?key??????????????????????????|?key_len?|?ref??|?rows????|?Extra???????| +----+-------------+--------------------+-------+------------------------------+------------------------------+---------+------+---------+-------------+ |??1?|?SIMPLE??????|?cin_offlinemessage?|?range?|?cin_offflinemessage_datetime?|?cin_offflinemessage_datetime?|?9???????|?NULL?|?3534912?|?Using?where?| +----+-------------+--------------------+-------+------------------------------+------------------------------+---------+------+---------+-------------+ 1?row?in?set?(0.00?sec) 這樣就用上了索引。(掃描了7位數(shù))
本文轉(zhuǎn)自 liang3391 51CTO博客,原文鏈接:http://blog.51cto.com/liang3391/816803
與50位技術(shù)專家面對(duì)面20年技術(shù)見(jiàn)證,附贈(zèng)技術(shù)全景圖
本文轉(zhuǎn)自 liang3391 51CTO博客,原文鏈接:http://blog.51cto.com/liang3391/816803
與50位技術(shù)專家面對(duì)面20年技術(shù)見(jiàn)證,附贈(zèng)技術(shù)全景圖
總結(jié)
以上是生活随笔為你收集整理的虽然有索引,但没有用上的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: vCenter Server管理:[2]
- 下一篇: 安装mariadb以及允许远程访问