我知道每個人都討厭指定問題,但我需要你的幫助。 所以 - 那是代碼。它必須獲取信息的用戶如何優化這個mysql查詢(max,replace,left join)?
select
c.id,
c.firstName,
c.lastName,
...
dt.code as docCode,
dt.name as docName,
replace(replace(replace(replace(replace(cc.contact, ' ',''), '(',''), ')', ''), '-', ''), '+', '') as homePhone,
replace(replace(replace(replace(replace(cc1.contact, ' ',''), '(',''), ')', ''), '-', ''), '+', '') as cellPhone
from Client c
left join ClientPolicy p on p.id=(select max(pp.id)
from ClientPolicy pp
where
pp.client_id = c.id
and pp.deleted = 0)
left join rbPolicyType pt on pt.id = p.policyType_id
left join ClientDocument d on d.id =(SELECT MAX(dd.id)
FROM ClientDocument dd
WHERE
dd.client_id = c.id
and dd.deleted = 0)
left join rbDocumentType dt on dt.id = d.documentType_id and dt.code IN ('1')
left join ClientContact cc ON cc.id = (select MAX(ccc.id)
FROM ClientContact ccc
where
ccc.client_id = c.id
and ccc.deleted = 0
and ccc.contactType_id = 1)
left join ClientContact cc1 ON cc1.id = (SELECT MAX(ccc1.id)
FROM ClientContact ccc1
WHERE
ccc1.client_id = c.id
and ccc1.deleted = 0
and ccc1.contactType_id = 3)
where
c.deleted = 0
and c.firstName like '%'
and c.patrName like '%'
and c.lastName like '%'
and replace(replace(replace(replace(replace(cc.contact, ' ',''), '(',''), ')', ''), '-', ''), '+', '') like '%521%'
and replace(replace(replace(replace(replace(cc1.contact, ' ',''), '(',''), ')', ''), '-', ''), '+', '') like '%8905%'
每隔%意味着用戶可以插入一些數據存在,例如like '%8905%
關於INDEX
的
我添加索引類似下面,所以這樣一來,我肯定不會幫助
INDEX client_insurer (client_id, insurer_id),
INDEX policyType_id (policyType_id),
INDEX Serial_Num (serial, number),
關於replace(replace...
我相信,正則表達式會給我只需1秒以下,ADDI NG這個
How do you extract a numerical value from a string in a MySQL query?
溶合不reduse時間(實際添加5秒以上)
我不知道(MB粘貼條件從where
至join
?)如何使其更快。請幫幫我。
嗯,謝謝你,我昨天讀你的評論,沒有時間回答。我會添加你所有的信息,你需要。另外,我的teamleader強烈贊同你的索引,例如(id,deleted)。 PS非常感謝你 –