mysql重點--正確使用

來源:互聯網
上載者:User

標籤:div   keyword   mysq   order by   處理   時間   整數   com   主鍵   

1.一些錯誤情況

資料庫表中添加索引後確實會讓查詢速度起飛,但前提必須是正確的使用索引來查詢,如果以錯誤的方式使用,則即使建立索引也會不奏效。
即使建立索引,索引也不會生效:

 1 - like ‘%xx‘ 2     select * from tb1 where name like ‘%cn‘; 3 - 使用函數 4     select * from tb1 where reverse(name) = ‘wupeiqi‘; 5 - or 6     select * from tb1 where nid = 1 or email = ‘[email protected]‘; 7     特別的:當or條件中有未建立索引的列才失效,以下會走索引 8             select * from tb1 where nid = 1 or name = ‘seven‘; 9             select * from tb1 where nid = 1 or email = ‘[email protected]‘ and name = ‘alex‘10 - 類型不一致11     如果列是字串類型,傳入條件是必須用引號引起來,不然...12     select * from tb1 where name = 999;13 - !=14     select * from tb1 where name != ‘alex‘15     特別的:如果是主鍵,則還是會走索引16         select * from tb1 where nid != 12317 - >18     select * from tb1 where name > ‘alex‘19     特別的:如果是主鍵或索引是整數類型,則還是會走索引20         select * from tb1 where nid > 12321         select * from tb1 where num > 12322 - order by23     select email from tb1 order by name desc;24     當根據索引排序時候,選擇的映射如果不是索引,則不走索引25     特別的:如果對主鍵排序,則還是走索引:26         select * from tb1 order by nid desc;27  28 - 複合式索引最左首碼29     如果複合式索引為:(name,email)30     name and email       -- 使用索引31     name                 -- 使用索引32     email                -- 不使用索引
2.其他注意事項

- 避免使用select *

count (1)或 count (列) 代替  count (*) - 建立表時盡量時  char  代替  varchar - 表的欄位順序固定長度的欄位優先 - 複合式索引代替多個單列索引(經常使用多個條件查詢時) - 盡量使用短索引 - 使用串連( JOIN )來代替子查詢(Sub-Queries) - 連表時注意條件類型需一致 - 索引散列值(重複少)不適合建索引,例:性別不適合3.limit分頁這裡只做簡單表述。在使用 select sth from table_name limit 0,10; 過程中發現當資料量大的時候。比如limit 24322,10需要ALL遍曆到兩萬條後才會拿到所需要的資料。需要的時間非常長,最佳化為: select * from bigdata where nid > 3000 limit 3000,10; 這樣會進行range查詢。速度非常快,所做的僅僅是記錄下上次查詢過的nid就行。同樣在直接輸入頁數比如客戶輸入4989,怎麼樣處理?利用B-tree數組來先粗略定位頁數也許可行。 

 

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.