4
我有一個使用SUBSTRING()
作爲條件的查詢:在MySQL中更快的替代SUBSTRING()?
SELECT p.name p_name,
pa.line1 p_line1,
pa.zip p_zip,
c.name c_name,
ca.line1 c_line1,
ca.zip c_zip
FROM bank b
JOIN import_bundle ib ON ib.bank_id = b.id
JOIN generic_import gi ON gi.import_bundle_id = ib.id
JOIN account_import ai ON ai.generic_import_id = gi.id
JOIN account a ON a.account_import_id = ai.id
JOIN account_address aa ON aa.account_id = a.id
JOIN address ca ON aa.address_id = ca.id
JOIN address pa ON pa.zip = ca.zip OR (pa.zip = ca.zip AND pa.line1 = ca.line1)
JOIN prospect p ON p.address_id = pa.id
JOIN customer c ON a.customer_id = c.id
WHERE b.name = 'M'
AND ib.active = 1
AND gi.active = 1
AND SUBSTRING(p.name, 1, 12) = SUBSTRING(c.name, 1, 12)
LIMIT 100
正如你所看到的,它只是比較p.name
和c.name
前12個字符。不幸的是,將此查詢添加到WHERE
子句使得我的查詢非常緩慢。有沒有什麼技巧可以做相同的比較,或者我最好的辦法是在每個包含客戶名稱前12個字符的表中添加另一列?我希望這不是後者,因爲這將是很多工作,我最終會做這樣的幾個比較。
出於好奇,它是如何使用AND p.name LIKE SUBSTRING(c.name,1,12)||執行的。 '%''?無論如何,我認爲你使用'p.name'和'c.name'的唯一機會是使用Jim Garrison的答案。 – ninjalj 2011-04-27 19:57:59