虽然有索引,但没有用上
生活随笔
收集整理的這篇文章主要介紹了
虽然有索引,但没有用上
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
select??count(id)?from?cin_offlinemessage?where?`datetime`?>=?'2012-03-15'?and?state=0;? 結果是全表掃描。 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) 雖然有索引,但沒有用上,掃描的行數太多了(8位數),優化器認為全表掃描比索引來的快。 后我修改了下 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位數)
本文轉自 liang3391 51CTO博客,原文鏈接:http://blog.51cto.com/liang3391/816803
與50位技術專家面對面20年技術見證,附贈技術全景圖
本文轉自 liang3391 51CTO博客,原文鏈接:http://blog.51cto.com/liang3391/816803
與50位技術專家面對面20年技術見證,附贈技術全景圖
總結
以上是生活随笔為你收集整理的虽然有索引,但没有用上的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vCenter Server管理:[2]
- 下一篇: linux-Centos 7下bond与