SELECT incc.customer_id from contacts inc leftjoin r_contacts_customer incc on incc.contacts_id = inc.contacts_id where inc.telephone like'%18958102776%'
EXPLAIN select incc.customer_id from contacts inc leftjoin r_contacts_customer incc on incc.contacts_id = inc.contacts_id where inc.telephone like'%6911%'
结果如下:
1 2 3 4
1 SIMPLE inc index idx_telephone_fixedphone 60812822520Usingwhere; Using index 1 SIMPLE incc ref idx_all,idx_contacts_id idx_all 4 inc.contacts_id 1Using index
CREATE INDEX idx_telephone_fulltext ON your_table (telephone, other_column);
使用第三方工具: 使用 lucene或者elasticsearch
以上的解决方案中,最贴近业余需求的,就是使用内置函数和 全文索引了.先试试以下2种方法
使用内置函数
1 2
SELECT incc.customer_id from contacts inc leftjoin r_contacts_customer incc on incc.contacts_id = inc.contacts_id where LOCATE('6911',inc.telephone)>1
上面的语句虽然比 like 快了点 但是 EXPLAIN 的结果还是 不走索引 并且全表扫描 contacts
1 2 3 4
1 SIMPLE inc index idx_telephone_fixedphone 60812822520Usingwhere; Using index 1 SIMPLE incc ref idx_all,idx_contacts_id idx_all 4 inc.contacts_id 1Using index
EXPLAIN select incc.customer_id from contacts inc leftjoin r_contacts_customer incc on incc.contacts_id = inc.contacts_id where inc.telephone like'%6911%'
结果是:
1 2 3 4
1 SIMPLE inc index idx_telephone_fixedphone 60812822520Usingwhere; Using index 1 SIMPLE incc ref idx_all,idx_contacts_id idx_all 4 smm.inc.contacts_id 1Using index
使用 MATCH() 匹配 全文索引 如下:
1 2 3
SELECT incc.customer_id from contacts inc leftjoin r_contacts_customer incc on incc.contacts_id = inc.contacts_id whereMATCH(inc.telephone) AGAINST('"6911"'INBOOLEAN MODE)