[英]MySQL to search using joins
I have 2 mysql tables - 我有2个mysql表-
candskill - (cis, sid) - where cid = candidate id, and sid = skill id candskill-(cis,sid)-其中cid =候选人ID,而sid =技能ID
Data in candskill (size - 257,000) - 坎兹基尔(大小-257,000)中的数据-
c1, s1
c1, s2
c2, s3
c1, s4
c2, s5
...
skills - (sid, name) - where sid = skill id, and name = skill name 技能-(sid,名称)-其中sid =技能ID,名称=技能名称
Data in skills (size 257,000)- 技能数据(25.7万)
s1 - oracle
s2 - project management
s3 - oracle
s4 - testing
s5 - testing
...
Now, I want to fetch all the candidates who have skills 'oracle' and 'testing' both. 现在,我想获取所有具有“ oracle”和“ testing”技能的候选人。 Or I want candidate who have skills either 'oracle' or 'testing'. 或者我想要具有“ oracle”或“ testing”技能的候选人。 I want to have any AND/OR combination of skills present, and want to fetch candidates for those skills. 我想展示任何AND / OR技能,并希望获取这些技能的候选人。
How would I achieve that? 我将如何实现?
This is what I have so far, which is not working in all scenarios. 到目前为止,这是我要解决的问题,并非在所有情况下都有效。
select distinct(cs.cid), s.name from candskill cs
inner join skills s on (cs.sid = s.sid and (s.name = 'oracle' or s.name = 'testing'))
Also, the query execution is taking too much time. 另外,查询执行花费太多时间。 approx 120 sec. 约120秒 How do we go about doing that. 我们如何去做。
I am thinking of writing a query, and passing the skill part of the query via php code, concate the strings, and generate new query each time a user searches for candidates for a particular skill. 我正在考虑编写查询,并通过php代码传递查询的技能部分,连接字符串,并在每次用户搜索特定技能的候选人时生成新查询。
You could use an having clause on count of s.name 您可以使用s.name计数的having子句
select cs.cid
from candskill cs
inner join skills s on (cs.sid = s.sid and s.name in ( 'oracle' , 'testing'))
group by cs.cid
having count(distinct(s.name)) = 2
for 1 or 2 1或2
select cs.cid
from candskill cs
inner join skills s on (cs.sid = s.sid and s.name in ( 'oracle' , 'testing'))
group by cs.cid
having count(distinct(s.name)) >= 1
Maybe reducing the set of skills helps with the performance, eg 也许减少技能集对表演有帮助,例如
select cs.cid
from (select sid from skills where name in ('oracle', 'testing')) s
join candskills cs on cs.sid = s.sid
Instead of joining 250,000 x 250,000 rows, this will join 2 x 250,000 rows. 而不是加入250,000 x 250,000行,这将加入2 x 250,000行。
Furthermore, adding an index on skills.name
and another on skills.sid
and candskills.sid
might improve the query further. 此外,在skills.name
上添加一个索引,在skills.sid
和candskills.sid
上添加另一个索引可能会进一步改善查询。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.