我有 3 个表(不写所有和实际的字段名称)
合同(约 30 000 行)
|id|client_id|contract_nr|....
容器(约 30 000 行
|id|contract_nr|phone_1(varchar)|....
客户端(约 35 000 行)
|id|phone_2(varchar)|phone_3(varchar)|phone_4(varchar)|....
我需要搜索电话号码,所以我试试这个 - 如果我搜索一个电话号码,例如在 container.phone_1
SELECT *
FROM contract JOIN client
ON contract.client_id = client.id
JOIN container
ON contract.contract_nr = container.contract_nr
WHERE container.phone_1 LIKE '264%'
id select_type table type possible_keys key key_len ref rows filtered Extra
-------------------------------------------------------------------------------------------------------------
1 SIMPLE container range contract_nr,phone_1 phone_1 63 NULL 912 100.00 Using where
1 SIMPLE contract ref contract_nr,client_id contract_nr 26 container.contract_nr 1 100.00
1 SIMPLE client eq_ref PRIMARY,id PRIMARY 3 contract.client_id 1 100.00
但是当我开始使用 OR 添加客户端的电话号码时
SELECT *
FROM contract JOIN client
ON contract.client_id = client.id
JOIN container ON contract.contract_nr = container.contract_nr
WHERE (
container.phone_1 LIKE '264%' OR
client.phone_2 LIKE '264%' OR
client_phone_3 LIKE '264%'
)
解释扩展:
id select_type table type possible_keys key key_len ref rows filtered Extra
-------------------------------------------------------------------------------------------------------------
1 SIMPLE container ALL k_ligums,k_telef NULL NULL NULL 32113 100.00
1 SIMPLE contract ref contract_nr,client_id contract_nr 26 za.zaao_konteineri.k_ligums 1 100.00
1 SIMPLE client eq_ref PRIMARY,id PRIMARY 3 contract.client_id 1 100.00 Using where
我已经索引了所有用于连接表和用于搜索的字段。我还尝试为包含所有搜索行的客户端和容器表创建多列索引 - 没有区别。
如何避免扫描容器表的 32113 行?