我可以在外部选择的子选择表上执行某个位置

时间:2017-03-22 12:29:43

标签: mysql

比如说我有这个查询,我想用作VIEW“user_data”

SELECT
    a.*
FROM (
    SELECT
        big_tbl.user_id,
        COUNT(big_tbl.id) as `total`
    FROM
        big_tbl
    GROUP BY 
        big_tbl.user_id
) a

如果我SELECT * FROM user_data,则需要大约10秒才能运行。如果我SELECT * FROM user_data WHERE user_id = 1234,则需要大约10秒才能运行。如果我这样做需要1秒钟

SELECT
    a.*
FROM (
    SELECT
        big_tbl.user_id,
        COUNT(big_tbl.id) as `total`
    FROM
        big_tbl
    WHERE
        big_tbl.user_id = 1234
    GROUP BY 
        big_tbl.user_id
) a

无论如何我可以做SELECT * FROM user_data WHERE big_tbl.user_id = 1234之类的事情吗?或者这与我目前的方法无关?

1 个答案:

答案 0 :(得分:0)

你的问题很难理解。如果我理解你的要求是正确的,那么user_id必须存在于两个表中:

SELECT *
FROM user_data
WHERE user_id =
      (SELECT user_id
       FROM big_tbl
       WHERE user_id = 1234);