将结果从一个子查询传递到MySQL中另一个子查询中的IN子句

时间:2011-05-06 18:08:05

标签: mysql sql

不确定这是否可行,但如果是,它会使我的查询更快。

基本上我有这样的查询:

 SELECT *
   FROM (SELECT bar.id
           FROM pivot_table
          WHERE foo.id = x) t1
   JOIN (SELECT count(*) c1, bar.id
           FROM table
       GROUP BY bar.id) t2 ON t1.id = t2.id
   JOIN (SELECT count(*) c2, bar.id
           FROM another_table
       GROUP BY bar.id) t3 ON t1.id = t3.id

但这很慢,因为tableanother_table很大。但实际上我只对t1中的查询产生的值感兴趣。因此,如果我能够以某种方式将这些结果转换为INt2的{​​{1}}子句,则查询应该显着加快。

这可能吗?


我猜不太清楚。好吧,我在想的是将查询更改为:

t3

可能会更快,因为它会缩小在t2和t3中扫描的行数。那不是这样吗?


每个人都想看到它,所以这里是完整的查询:

 SELECT *
   FROM (GROUP_CONCAT (bar.id) as results
                 FROM pivot_table
                WHERE foo.id = x) t1
         JOIN (SELECT count(*) c1, bar.id
                 FROM table
                WHERE bar.id IN (*results from t1*)
                GROUP BY bar.id) t2 ON t1.id = t2.id
         JOIN (SELECT count(*) c2, bar.id
                 FROM another_table
                WHERE bar.id IN (*results from t1*)
                GROUP BY bar.id) t3 ON t1.id = t3.id

3 个答案:

答案 0 :(得分:1)

SELECT COUNT(t1.id) c1, COUNT(t2.id) c2, COUNT(t3.id) c3, t1.id 
FROM pivot_table t1 
JOIN table t2 ON t1.id=t2.id 
JOIN another_table t3 ON t3.id=t1.id where t1.id=x group by t1.id

请确保pivot_table.id,table.id和another_table.id已编入索引

关于您的查询: 您的查询问题是驱动程序表使用连接缓冲区,为了使您的查询更快,您应该增加您的连接缓冲区大小

答案 1 :(得分:1)

我能够完成我想要做的事情:

 SELECT *
   FROM (@var:=GROUP_CONCAT(bar.id) as results
                 FROM pivot_table
                WHERE foo.id = x) t1
         JOIN (SELECT count(*) c1, bar.id
                 FROM table
                WHERE bar.id IN (@var)
                GROUP BY bar.id) t2 ON t1.id = t2.id
         JOIN (SELECT count(*) c2, bar.id
                 FROM another_table
                WHERE bar.id IN (@var)
                GROUP BY bar.id) t3 ON t1.id = t3.id

但速度方面的好处并不太重要。我现在放弃了一种查询方法,转而使用许多较小的查询,这样做要好得多。

答案 2 :(得分:0)

根据实际查询进行修订

我认为你可以将你的查询减为:

Select jk.Count( Distinct jk.keyword_id )
        * jk.Count( Distinct js.skill_id )
        / Power( Count(*), 2 )
        As ratio
    , js.skill_id
    , jk.keyword_id
From jobs_keywords As jk
    Join jobs_skills As js
        On js.job_id = jk.job_id
Where jk.job_id =50100
Group By js.skill_id, jk.keyword_id
Order By ratio Desc
Limit 25