这是第3次编辑。根据您的所有反馈,我能够使用多个搜索条件生成以下查询。
请注意,这是一个现有系统,预算是一个问题,所以我尽我所能来改善现有的查询。您看到的搜索是基于数组手动完成的,没有连接。同样的搜索需要花费2-3分钟来处理,而感谢所有人摇滚大师现在需要7-8秒来处理:)
SELECT SQL_CALC_FOUND_ROWS fname, lname, desig, company, region, state, country, add_uid, contacts.`id` as id
FROM contacts
INNER JOIN contact_to_categories ON contact_to_categories.contactid = contacts.id
AND (
contact_to_categories.catid = '2'
)
INNER JOIN contact_professional_details ON contact_professional_details.contact_id = contacts.id
AND (
FIND_IN_SET('1', contact_professional_details.pd_insid)
OR FIND_IN_SET(' 8', contact_professional_details.pd_insid)
OR FIND_IN_SET(' 33', contact_professional_details.pd_insid)
)
AND (
FIND_IN_SET('4', contact_professional_details.pd_secid)
OR FIND_IN_SET('3', contact_professional_details.pd_secid)
OR FIND_IN_SET('5', contact_professional_details.pd_secid)
OR FIND_IN_SET('7', contact_professional_details.pd_secid)
OR FIND_IN_SET('12', contact_professional_details.pd_secid)
OR FIND_IN_SET('11', contact_professional_details.pd_secid)
OR FIND_IN_SET('9', contact_professional_details.pd_secid)
OR FIND_IN_SET('38', contact_professional_details.pd_secid)
OR FIND_IN_SET('35', contact_professional_details.pd_secid)
OR FIND_IN_SET('115', contact_professional_details.pd_secid)
)
INNER JOIN contact_address ON contact_address.contact_id = contacts.id
AND (
contact_address.hmregion IN ('AF', 'EU', 'OC', 'SA')
OR contact_address.hmcountry IN ('Algeria', 'Angola', 'Benin', 'Comoros', 'Andorra', 'Austria', 'Belarus', 'Belgium', 'American Samoa', 'Australia', 'French Polynesia', 'Guam', 'Kiribati', 'Marshall Islands', 'Colombia', 'Ecuador', 'Falkland Islands', 'Guyana', 'Paraguay', 'Peru', 'Laos', 'Malaysia', 'Myanmar', 'Singapore', 'Vietnam')
OR contact_address.hmcity = 'singapore'
)
INNER JOIN contact_offices ON contact_offices.contact_id = contacts.id
AND (
contact_offices.off_region IN ('AF', 'EU', 'OC', 'SA')
OR contact_offices.off_country IN ('Algeria', 'Angola', 'Benin', 'Comoros', 'Andorra', 'Austria', 'Belarus', 'Belgium', 'American Samoa', 'Australia', 'French Polynesia', 'Guam', 'Kiribati', 'Marshall Islands', 'Colombia', 'Ecuador', 'Falkland Islands', 'Guyana', 'Paraguay', 'Peru', 'Laos', 'Malaysia', 'Myanmar', 'Singapore', 'Vietnam')
OR contact_offices.off_city = 'singapore'
)
WHERE 1 AND (
FIND_IN_SET('1', contacts.ins_id)
OR FIND_IN_SET(' 8', contacts.ins_id)
OR FIND_IN_SET(' 33', contacts.ins_id)
)
AND (
FIND_IN_SET('4', contacts.sec_id)
OR FIND_IN_SET('3', contacts.sec_id)
OR FIND_IN_SET('5', contacts.sec_id)
OR FIND_IN_SET('7', contacts.sec_id)
OR FIND_IN_SET('12', contacts.sec_id)
OR FIND_IN_SET('11', contacts.sec_id)
OR FIND_IN_SET('9', contacts.sec_id)
OR FIND_IN_SET('38', contacts.sec_id)
OR FIND_IN_SET('35', contacts.sec_id)
OR FIND_IN_SET('115', contacts.sec_id)
)
AND ( FIND_IN_SET('Tier 1', `vip_tier`) OR FIND_IN_SET('Tier 3', `vip_tier`) )
AND ( FIND_IN_SET('Tier A', `vip_coll_tier`) )
AND ( FIND_IN_SET('Yes', `vip_influencer`) )
AND ( FIND_IN_SET('Contemporary', `vip_class_art_coll`) OR FIND_IN_SET('Modern', `vip_class_art_coll`) OR FIND_IN_SET('Geographic', `vip_class_art_coll`) )
AND ( FIND_IN_SET('Sculpture', `vip_med_art_coll`) OR FIND_IN_SET('Photography', `vip_med_art_coll`) OR FIND_IN_SET('Video', `vip_med_art_coll`) OR FIND_IN_SET('Installation', `vip_med_art_coll`) )
AND ( FIND_IN_SET('Japan', `vip_geo_int`) OR FIND_IN_SET('Korea', `vip_geo_int`) OR FIND_IN_SET('Southeast Asia', `vip_geo_int`) OR FIND_IN_SET('Oceania', `vip_geo_int`) )
AND ( FIND_IN_SET('HNWI', `vip_seniority`) OR FIND_IN_SET('Top Social Leaders', `vip_seniority`) OR FIND_IN_SET('Other Executives', `vip_seniority`) )
AND ( `status` = 'a' )
ORDER BY fname
asc
LIMIT 0, 50
我知道可以通过将find值设置为单独的表并在contacts master table和values master table之间创建关系表来进一步改进。但正如我所说,预算对于这些人来说是一个大问题所以我想这对他们来说效率更高。
然而,欢迎任何进一步改进的想法。
答案 0 :(得分:4)
这是在查询中出错的部分
INNER JOIN contact_professional_details
ON contact_professional_details.contact_id = contacts.id
AND ( <-- Here
INNER JOIN contact_to_categories
ON contact_to_categories.contactid = contacts.id
AND (
contact_to_categories.catid = '2'
)
将此更改为
INNER JOIN contact_professional_details
ON contact_professional_details.contact_id = contacts.id
INNER JOIN contact_to_categories
ON contact_to_categories.contactid = contacts.id
AND contact_to_categories.catid = '2'
修改强>
您发布的查询总是很乱,您确实多次加入相同的表,并使用链式OR
条件而不是IN
子句。因此,下面是您修改后的查询。
SELECT SQL_CALC_FOUND_ROWS fname,
lname,
desig,
company,
region,
state,
country,
add_uid,
contacts.`id` as id
FROM contacts
INNER JOIN contact_to_categories
ON contact_to_categories.contactid = contacts.id
AND contact_to_categories.catid = '2'
INNER JOIN contact_professional_details
ON contact_professional_details.contact_id = contacts.id
AND (
FIND_IN_SET('4', contact_professional_details.pd_secid)
OR FIND_IN_SET('3', contact_professional_details.pd_secid)
OR FIND_IN_SET('5', contact_professional_details.pd_secid)
OR FIND_IN_SET('7', contact_professional_details.pd_secid)
OR FIND_IN_SET('12', contact_professional_details.pd_secid)
OR FIND_IN_SET('11', contact_professional_details.pd_secid)
OR FIND_IN_SET('9', contact_professional_details.pd_secid)
OR FIND_IN_SET('38', contact_professional_details.pd_secid)
OR FIND_IN_SET('35', contact_professional_details.pd_secid)
OR FIND_IN_SET('115', contact_professional_details.pd_secid)
)
INNER JOIN contact_address ON contact_address.contact_id = contacts.id
AND
contact_address.hmregion IN ('AF','EU','OC','SA')
AND
contact_address.hmcountry IN ('Algeria',
'Angola',
'Benin',
'Comoros',
'Andorra',
'Austria',
'Belarus',
'Belgium',
'American Samoa',
'Australia',
'French Polynesia',
'Guam',
'Kiribati',
'Marshall Islands',
'Colombia',
'Ecuador',
'Falkland Islands',
'Guyana',
'Paraguay',
'Peru',
'Laos',
'Malaysia',
'Myanmar',
'Singapore',
'Vietnam'
)
AND contact_address.hmcity='singapore'
INNER JOIN contact_offices ON contact_offices.contact_id = contacts.id
AND
contact_offices.off_region IN ('AF','EU','OC','SA')
AND
contact_offices.off_country IN ('Algeria',
'Angola',
'Benin',
'Comoros',
'Andorra',
'Austria',
'Belarus',
'Belgium',
'American Samoa',
'Australia',
'French Polynesia',
'Guam',
'Kiribati',
'Marshall Islands',
'Colombia',
'Ecuador',
'Falkland Islands',
'Guyana',
'Paraguay',
'Peru',
'Laos',
'Malaysia',
'Myanmar',
'Singapore',
'Vietnam'
)
AND contact_offices.off_city='singapore'
WHERE 1 AND (
FIND_IN_SET('1', contacts.ins_id)
OR FIND_IN_SET(' 8', contacts.ins_id)
OR FIND_IN_SET(' 33', contacts.ins_id)
)
AND (
FIND_IN_SET('4', contacts.sec_id)
OR FIND_IN_SET('3', contacts.sec_id)
OR FIND_IN_SET('5', contacts.sec_id)
OR FIND_IN_SET('7', contacts.sec_id)
OR FIND_IN_SET('12', contacts.sec_id)
OR FIND_IN_SET('11', contacts.sec_id)
OR FIND_IN_SET('9', contacts.sec_id)
OR FIND_IN_SET('38', contacts.sec_id)
OR FIND_IN_SET('35', contacts.sec_id)
OR FIND_IN_SET('115', contacts.sec_id)
)
AND (FIND_IN_SET('Tier 1', `vip_tier`) OR FIND_IN_SET('Tier 3', `vip_tier`))
AND (FIND_IN_SET('Tier A', `vip_coll_tier`))
AND (FIND_IN_SET('Yes', `vip_influencer`))
AND (FIND_IN_SET('Contemporary', `vip_class_art_coll`) OR FIND_IN_SET('Modern', `vip_class_art_coll`) OR FIND_IN_SET('Geographic', `vip_class_art_coll`))
AND (FIND_IN_SET('Sculpture', `vip_med_art_coll`) OR FIND_IN_SET('Photography', `vip_med_art_coll`) OR FIND_IN_SET('Video', `vip_med_art_coll`) OR FIND_IN_SET('Installation', `vip_med_art_coll`))
AND (FIND_IN_SET('Japan', `vip_geo_int`) OR FIND_IN_SET('Korea', `vip_geo_int`) OR FIND_IN_SET('Southeast Asia', `vip_geo_int`) OR FIND_IN_SET('Oceania', `vip_geo_int`))
AND (FIND_IN_SET('HNWI', `vip_seniority`) OR FIND_IN_SET('Top Social Leaders', `vip_seniority`) OR FIND_IN_SET('Other Executives', `vip_seniority`))
AND (`status`='a')
ORDER BY fname asc
LIMIT 0,50
答案 1 :(得分:3)
如果有多个连接,最好为表使用别名而不是整个名称。使阅读和理解更容易。无论如何,尝试在第9行附近,你得到错误。
INNER JOIN contact_professional_details
ON contact_professional_details.contact_id = contacts.id
INNER JOIN contact_to_categories ON contact_to_categories.contactid = contacts.id
AND contact_to_categories.catid = '2'
答案 2 :(得分:2)
您的内部联接条件不正确。
例如
INNER JOIN contact_to_categories ON contact_to_categories.contactid = contacts.id
AND
(
contact_to_categories.catid = '2'
)
需要
INNER JOIN contact_to_categories ON (
contact_to_categories.contactid = contacts.id
AND contact_to_categories.catid = '2'
)
您需要修复所有符合上述模式的内部联接
答案 3 :(得分:1)
假设您对指定区域或指定国家/地区或指定城市中的联系人感兴趣,并稍微清理您的代码: -
SELECT SQL_CALC_FOUND_ROWS fname, lname, desig, company, region, state, country, add_uid, contacts.`id` as id
FROM contacts
INNER JOIN contact_to_categories ON contact_to_categories.contactid = contacts.id AND contact_to_categories.catid = '2'
INNER JOIN contact_professional_details ON contact_professional_details.contact_id = contacts.id
AND (
FIND_IN_SET('4', contact_professional_details.pd_secid)
OR FIND_IN_SET('3', contact_professional_details.pd_secid)
OR FIND_IN_SET('5', contact_professional_details.pd_secid)
OR FIND_IN_SET('7', contact_professional_details.pd_secid)
OR FIND_IN_SET('12', contact_professional_details.pd_secid)
OR FIND_IN_SET('11', contact_professional_details.pd_secid)
OR FIND_IN_SET('9', contact_professional_details.pd_secid)
OR FIND_IN_SET('38', contact_professional_details.pd_secid)
OR FIND_IN_SET('35', contact_professional_details.pd_secid)
OR FIND_IN_SET('115', contact_professional_details.pd_secid)
)
INNER JOIN contact_address ON contact_address.contact_id = contacts.id
INNER JOIN contact_offices ON contact_offices.contact_id = contacts.id
WHERE 1
AND ((
contact_address.hmregion IN ('AF', 'EU', 'OC', 'SA')
OR contact_address.hmcountry IN ('Algeria', 'Angola', 'Benin', 'Comoros', 'Andorra', 'Austria', 'Belarus', 'Belgium', 'American Samoa', 'Australia', 'French Polynesia', 'Guam', 'Kiribati', 'Marshall Islands', 'Colombia', 'Ecuador', 'Falkland Islands', 'Guyana', 'Paraguay', 'Peru', 'Laos', 'Malaysia', 'Myanmar', 'Singapore', 'Vietnam')
OR contact_address.hmcity='singapore'
)
OR (
contact_offices.off_region IN ('AF', 'EU', 'OC', 'SA')
OR contact_offices.off_country IN ('Algeria', 'Angola', 'Benin', 'Comoros', 'Andorra', 'Austria', 'Belarus', 'Belgium', 'American Samoa', 'Australia', 'French Polynesia', 'Guam', 'Kiribati', 'Marshall Islands', 'Colombia', 'Ecuador', 'Falkland Islands', 'Guyana', 'Paraguay', 'Peru', 'Laos', 'Malaysia', 'Myanmar', 'Singapore', 'Vietnam')
OR contact_offices.off_city='singapore'
)
)
AND (
FIND_IN_SET('1', contacts.ins_id)
OR FIND_IN_SET(' 8', contacts.ins_id)
OR FIND_IN_SET(' 33', contacts.ins_id)
)
AND (
FIND_IN_SET('4', contacts.sec_id)
OR FIND_IN_SET('3', contacts.sec_id)
OR FIND_IN_SET('5', contacts.sec_id)
OR FIND_IN_SET('7', contacts.sec_id)
OR FIND_IN_SET('12', contacts.sec_id)
OR FIND_IN_SET('11', contacts.sec_id)
OR FIND_IN_SET('9', contacts.sec_id)
OR FIND_IN_SET('38', contacts.sec_id)
OR FIND_IN_SET('35', contacts.sec_id)
OR FIND_IN_SET('115', contacts.sec_id)
)
AND (
FIND_IN_SET('Tier 1', `vip_tier`)
OR FIND_IN_SET('Tier 3', `vip_tier`)
)
AND (FIND_IN_SET('Tier A', `vip_coll_tier`))
AND (FIND_IN_SET('Yes', `vip_influencer`))
AND (FIND_IN_SET('Contemporary', `vip_class_art_coll`) OR FIND_IN_SET('Modern', `vip_class_art_coll`) OR FIND_IN_SET('Geographic', `vip_class_art_coll`))
AND (FIND_IN_SET('Sculpture', `vip_med_art_coll`) OR FIND_IN_SET('Photography', `vip_med_art_coll`) OR FIND_IN_SET('Video', `vip_med_art_coll`) OR FIND_IN_SET('Installation', `vip_med_art_coll`))
AND (FIND_IN_SET('Japan', `vip_geo_int`) OR FIND_IN_SET('Korea', `vip_geo_int`) OR FIND_IN_SET('Southeast Asia', `vip_geo_int`) OR FIND_IN_SET('Oceania', `vip_geo_int`))
AND (FIND_IN_SET('HNWI', `vip_seniority`) OR FIND_IN_SET('Top Social Leaders', `vip_seniority`) OR FIND_IN_SET('Other Executives', `vip_seniority`))
AND (`status`='a')
ORDER BY fname asc
LIMIT 0, 50
请注意,FIND_IN_SET的使用表明数据库标准化不正确,其中的字段包含以逗号分隔的值列表。