0

我有两个大数据表,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 t2WHERE 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 列。我使用左连接,但这仍然超时:(

4

6 回答 6

1

尝试摆脱您的子查询并使用联接,我还从查询中删除了联合,为此我将条件括在大括号中以满足您的联合需求

SELECT g1.* FROM `t1` AS g1
left join `t2` AS g2 on (g1.id=g2.uid)
WHERE (
(g2.usignal = 1 AND g2.type = 1 )
 OR g1.signal = 1      
  )
order by id desc limit 0,20

还要确保你的列上有正确的索引

看到这个小提琴

于 2013-09-17T07:30:14.253 回答
0

您是否尝试过加入而不是嵌套选择(不会很快)?

SELECT g1.* FROM `t1` AS g1
LEFT JOIN `t2` AS g2 ON g2.uid = g1.id 
WHERE g1.signal = 1
 OR (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
于 2013-09-17T07:28:04.733 回答
0

What error do you get so that we can answer your question .. you can set the maximum execution time from php ini like this :ini_set('max_execution_time', 300); And your task will work for 300 seconds instead of 30 .

于 2013-09-17T07:18:54.940 回答
0

和( , )t1上的索引。signalt2usignalutype

定义你的主键。

于 2013-09-17T07:25:40.427 回答
0

你有指数吗?确保您创建了正确的。

因为t1您似乎需要 ( id, signal) 元组上的索引。因为t2您需要 ( usignal, type) 元组上的索引。

如果您还没有(根据您没有的创建表),您可以通过以下方式添加它们:

create index 'id_signal_index' on t1 (`id`, `signal`);
create index 'id_usignal_index' on t2 (`usignal`, `type`);

id在像t1.

还将内部查询转换为左连接查询,其他答案中对此进行了说明。

于 2013-09-17T07:26:41.893 回答
0

在您的 SQL-Fiddle http://www.sqlfiddle.com/#!2/ee61b/3 下方的绿色导航栏中有一个选项:“+ 查看执行计划”。您还可以通过在查询前面添加“EXPLAIN”来获取此信息。

在您的情况下,结果如下所示:

+------+--------------+-------------+--------+---------------+--------------+---------+------+------+-------------+
| id   | select_type  | table       | type   | possible_keys | key          | key_len | ref  | rows | Extra       |
+------+--------------+-------------+--------+---------------+--------------+---------+------+------+-------------+
|    1 | PRIMARY      | g1          | ALL    | NULL          | NULL         | NULL    | NULL |    4 | Using where |
|    2 | UNION        | g1          | ALL    | NULL          | NULL         | NULL    | NULL |    4 |             |
|    2 | UNION        | <subquery3> | eq_ref | distinct_key  | distinct_key | 4       | func |    1 |             |
|    3 | MATERIALIZED | g2          | ALL    | NULL          | NULL         | NULL    | NULL |    4 | Using where |
| NULL | UNION RESULT | <union1,2>  | ALL    | NULL          | NULL         | NULL    | NULL | NULL |             |
+------+--------------+-------------+--------+---------------+--------------+---------+------+------+-------------+

查看“possible_keys”列:到处都有 NULL。这很糟糕,这意味着数据库必须按顺序搜索您的数据才能找到一些东西。

如果您将正确的主键和索引添加到数据库中,那么每次搜索都将使用像树这样的巧妙数据结构来完成,并且会更快,尤其是对于大日期:

+------+--------------+------------+--------+-----------------+---------+---------+-------------+------+-------------+
| id   | select_type  | table      | type   | possible_keys   | key     | key_len | ref         | rows | Extra       |
+------+--------------+------------+--------+-----------------+---------+---------+-------------+------+-------------+
|    1 | PRIMARY      | g1         | ref    | signal          | signal  | 5       | const       |    2 |             |
|    2 | UNION        | g2         | ref    | PRIMARY,usignal | usignal | 5       | const       |    2 | Using where |
|    2 | UNION        | g1         | eq_ref | PRIMARY         | PRIMARY | 4       | test.g2.uid |    1 |             |
| NULL | UNION RESULT | <union1,2> | ALL    | NULL            | NULL    | NULL    | NULL        | NULL |             |
+------+--------------+------------+--------+-----------------+---------+---------+-------------+------+-------------+

所以:阅读主键和索引并将这些知识应用到您的示例中。

于 2013-09-17T07:35:57.843 回答