我有两个大数据表,http://www.sqlfiddle.com/#! 2/ee61b/3 中的示例
表 1喜欢
CREATE TABLE `t1`(
`id` int(4) ,
`name` varchar(40),
`signal` int(4)
)
;
INSERT INTO `t1`
(`id`, `name`, `signal`)
VALUES
(1, 'a', 1),
(2, 'b', 1),
(3, 'c', 0),
(4, 'd', 0)
;
和表2一样
CREATE TABLE `t2`(
`uid` int(4),
`type` int(4),
`usignal` int(4)
)
;
INSERT INTO `t2`
(`uid`, `type`, `usignal`)
VALUES
(3, 1, 1),
(1, 2, 1),
(4, 1, 0),
(2, 2, 0)
;
现在我尝试让所有记录都有条件,如
1。表 1 中的所有记录都有signal
= 1
2。或(+)表 1 中的所有记录都id
在(SELECT uid FROM t2
WHERE t2.type = 1 AND t2.usignal = 1)
我这样做喜欢
SELECT * FROM `t1` AS g1
WHERE g1.signal = 1
UNION
SELECT * FROM `t1` AS g1
WHERE g1.id
IN (
SELECT uid
FROM `t2` AS g2
WHERE g2.usignal = 1 AND g2.type = 1
)
但我必须做一些有条件的(我的加入,...),我这样做
SELECT * FROM `t1` AS g1
WHERE g1.signal = 1
UNION
SELECT * FROM `t1` AS g1
WHERE g1.id
IN (
SELECT uid
FROM `t2` AS g2
WHERE g2.usignal = 1 AND g2.type = 1
)
/*dynamic other join query here */
and id>=1 /*dynamic conditional query here*/
order by id desc limit 0,20
如果数据很小,那效果很好,但是我的数据更大,并且在执行时会失败
我该怎么做谢谢
编辑:
我的 sql 呢?那是使执行缓慢并使其超时吗?
Edit02:
我使用 phpmyadmin 并单击索引t1(id, signal)
并在http://www.sqlfiddle.com/#!2/d88a9/1t2(type, unsignal)
中为两个表示例添加 id 主键
但实际上我的表有大约 10 列。我使用左连接,但这仍然超时:(