mysql IN FIND_IN_SET對比

FILE_IN_SET IN 使用方法對比

user表數據code

id name
1 小明
2 小紅
3 小李子

如今查詢id爲 1,3的數據索引

select * from `user` where id in(1,3)
select * from `user` where FIND_IN_SET(id, '1,3')

使用explain命令查詢運行狀況table

  • IN命令結果
explain
select * from `user` where id in(1,3)
id select_type table type possible_keys key key_len ref rows Extra
id SIMPLE user range PRIMARY PRIMARY 4 2 Using where

type = range 索引範圍掃描效率

  • FIND_IN_SET 結果
explain
select * from `user` where FIND_IN_SET(id, '1,3')
id select_type table type possible_keys key key_len ref rows Extra
id SIMPLE user ALL 3 Using where

type=ALL 全盤掃描 效率低下不推薦使用select

結論

IN 效率高與 FIND_IN_SET方法

相關文章
相關標籤/搜索