like 『%xx『
select * from tb1 where name like 『%cn『; --未使用索引
select * from tb1 where name like 『cn%『; --使用索引
- 使用函式
--未使用索引
select * from tb1 where reverse(name) =『hwan『;
--使用索引
select * from tb1 where name = reverse(『hwan『);
- or
select * from tb1 where nid = 1 or email =『[email protected]『;
特別的:當or條件中有未建立索引的列才失效,以下會走索引
select * from tb1 where nid = 1 or name =『seven『;
select * from tb1 where nid = 1 or email =『[email protected]『and name =『alex『- 型別不一致
如果列是字串型別,傳入條件是必須用引號引起來,不然...
select * from tb1 where name = 999;
select * from tb1 where name !=『alex『--all
特別的:如果是主鍵,則還是會走索引
select * from tb1 where nid != 123 --range
select * from tb1 where name >『alex『特別的:如果是主鍵或索引是整數型別,則還是會走索引
select * from tb1 where nid > 123
select * from tb1 where num > 123
- order by
select email from tb1 order by name desc;
當根據索引排序時候,選擇的對映如果不是索引,則不走索引
特別的:如果對主鍵排序,則還是走索引:
select * from tb1 order by nid desc;
- 組合索引最左字首
如果組合索引為:(name,email)
name and email -- 使用索引
name -- 使用索引
email -- 不使用索引
有關mysql索引的一點補充
標籤:rom 排序 .com 失效 對映 range sel lex 主鍵
MySql有關索引的優化
一 寫sql需要注意的?1.在where及order by之後建立索引。2.避免使用 和 會導致索引無效。3.有關null值問題 例如 select from user where is null,避免使用 應給 設定乙個預設的值0。4.避免使用or查詢,否則會放棄索引。可以使用union all替...
有關索引的語句
建立索引 create index indexname on tablename columnname tablespace tablespacename 重建索引 alert index indexname rebuild nologging 若rebuild後跟online可減少鎖表的時間。刪除...
有關mysql事務
今天使用jdbctemplate和spring事務控制,發現無論怎麼配置,事務都不回滾。後來發現是mysql資料庫的問題。要讓mysql支援事務,需要滿足以下條件 1.autocommit引數為0 命令 set global autocommit 0 檢視 select autocommit 注意 ...