所以,我算了一下,真正的問題是,因爲我使用tagids IN子句的。更改文本搜索的部分查詢並沒有多大幫助。任何想法如何改善查詢?
查詢在服務器上運行時間過長。這裏Partha S是用戶輸入的搜索項目。表聯繫人包含個人信息,標籤包含類別名稱和ID; contacts2tags表中包含contactid和tagid,其值分別與聯繫人和標籤中的id類似。
SELECT *
FROM
(
SELECT *,
IF
(
first_name LIKE 'Partha S'
OR last_name LIKE 'Partha S'
OR phone_number LIKE 'Partha S'
OR mobile_number LIKE 'Partha S'
OR email_address LIKE 'Partha S'
OR address LIKE 'Partha S'
OR organization LIKE 'Partha S'
OR other LIKE 'Partha S'
OR sector LIKE 'Partha S'
OR designation LIKE 'Partha S'
OR concat (first_name, ' ', last_name) LIKE 'Partha S'
OR concat (last_name, ' ', first_name) LIKE 'Partha S',
1,
0)
as exact,
IF
(
(
first_name LIKE '%Partha%'
OR last_name LIKE '%Partha%'
OR phone_number LIKE '%Partha%'
OR mobile_number LIKE '%Partha%'
OR email_address LIKE '%Partha%'
OR address LIKE '%Partha%'
OR organization LIKE '%Partha%'
OR other LIKE '%Partha%'
OR sector LIKE '%Partha%'
OR designation LIKE '%Partha%')
AND
(
first_name LIKE '%S%'
OR last_name LIKE '%S%'
OR phone_number LIKE '%S%'
OR mobile_number LIKE '%S%'
OR email_address LIKE '%S%'
OR address LIKE '%S%'
OR organization LIKE '%S%'
OR other LIKE '%S%'
OR sector LIKE '%S%'
OR designation LIKE '%S%')
,
1,
0)
as normal
FROM contacts
WHERE id in
(
SELECT DISTINCT contacts.id
from contacts INNER
JOIN contacts2tags ON contacts.id = contacts2tags.contactid
WHERE (tagid in (178)))
)
d
WHERE exact = 1
OR normal = 1
ORDER BY exact desc,
last_name asc LIMIT 0,
20
UPDATE: 具體根據建議,我除去精確搜索AGAINST LIKE運算符,以及用於MATCH(..)(..)代替LIKE在後者的情況下。儘管第一次更改確實提高了性能,但使用MATCH()AGAINST()並沒有令人驚訝地改變執行時間。這是更新後的查詢。 PS我嘗試使用匹配(所有列)反對(搜索項目)和匹配(單列)反對(搜索項目)結合OR。請建議。感謝
SELECT *
FROM
(
SELECT *,
IF
(
first_name ='Partha S'
OR last_name ='Partha S'
OR phone_number ='Partha S'
OR mobile_number ='Partha S'
OR email_address = 'Partha S'
OR address ='Partha S'
OR organization ='Partha S'
OR other ='Partha S'
OR sector ='Partha S'
OR designation ='Partha S'
OR concat (first_name, ' ', last_name) ='Partha S'
OR concat (last_name, ' ', first_name) ='Partha S',
1,
0)
as exact,
IF
(match(first_name,last_name,phone_number,mobile_number,email_address, address,organization,other,sector,designation) against('Partha')
OR match(first_name,last_name,phone_number,mobile_number,email_address,address,organization,other,sector,designation) against('S')
,
1,
0)
as normal
FROM contacts
WHERE id in
(
SELECT DISTINCT contacts.id
from contacts INNER
JOIN contacts2tags ON contacts.id = contacts2tags.contactid
WHERE (tagid in (178)))
)
d
WHERE exact = 1
OR normal = 1
ORDER BY exact desc,
last_name asc LIMIT 0,
20
謝謝..我刪除LIKE建議。它有幫助 – user415