mysql資料庫最佳化之 如何選擇合適的列建立索引

來源:互聯網
上載者:User

標籤:資料庫最佳化   payment   表名   sql   bsp   index   mysq   font   出現   

1. 在where 從句,group by 從句,order by 從句,on 從句中出現的列;

2. 索引欄位越小越好;

3. 離散度大的列放到聯合索引的前面;比如:

    select * from payment where staff_id = 2 and customer_id = 236;

    針對上面的查詢是  index(sftaff_id, customer_id) 好?還是index(customer_id, staff_id)好?

    因為customer_id的離散度更大,因此用後面的更合適!!

那麼問題來了。怎麼判斷離散度呢,可以使用 select count(distinct customer_id), count(distinct staff_id) from 表名

誰的值大,說明這一些列的離散度更高!

mysql資料庫最佳化之 如何選擇合適的列建立索引

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.