考虑使用三个表:A
,B
和C
以及以下简单查询:
SELECT *
FROM A
LEFT JOIN (
SELECT *
FROM B
INNER JOIN C ON (B.bid = C.bfid)
) s ON (A.bfid = s.bid)
如果子查询选择了太多行,则此查询将成为拖动,因为MySQL不会使用A.bfid = s.bid
的任何索引。如果有多个这样的子查询,这种情况会变得更糟。
有没有人知道如何优化这种情况?
这是表结构和示例数据:
CREATE TABLE A (aid INT, bfid INT, PRIMARY KEY (aid));
CREATE TABLE B (bid INT, PRIMARY KEY (bid));
CREATE TABLE C (cid INT, bfid int, PRIMARY KEY (cid));
INSERT INTO B VALUES (1), (2), (3);
INSERT INTO A VALUES (4, 1), (5, 2);
INSERT INTO C VALUES (6, 2), (7, 3);
使用这些,查询将输出:
+-----+------+------+------+------+
| aid | bfid | bid | cid | bfid |
+-----+------+------+------+------+
| 4 | 1 | NULL | NULL | NULL |
| 5 | 2 | 2 | 6 | 2 |
+-----+------+------+------+------+
答案 0 :(得分:2)
一种可能的方法是使用一对LEFT连接,然后根据是否在C上找到匹配来确定是否返回B.adid: -
SELECT A.*,
IF(C.bfid IS NULL, NULL, B.bid),
IF(C.bfid IS NULL, NULL, C.cid),
IF(C.bfid IS NULL, NULL, C.bfid)
FROM A
LEFT OUTER JOIN B ON A.bfid = B.bid
LEFT OUTER JOIN C ON B.bid = C.bfid
答案 1 :(得分:1)
是的,Mysql派生表不使用索引并在查询中使用它们 大数据库是一个问题。
首先要做的是避免这种情况并使用JOINS从不同的表中收集数据,如果JOIN是在索引列上完成的 然后它将非常快,并将忽略全表扫描。现在,如果所有JOIN都没有达到目的而且你没有选择 使用子查询和dereived表,然后替代是使用TEMPORARY表作为子查询数据和索引TEMPORARY表。
为了说明这一点,我们可以创建表并给出索引,并在不使用TEMPORARY表的情况下完成查询说明 并使用TEMPORARY表。
请注意,TEMPORARY表将存在于当前会话/连接中。
create table A (aid int ,val varchar(20));
insert into A values
(1,'A'),(2,'B'),(3,'C'),(4,'D');
create table B (bid int, afid int);
insert into B values
(1,1),(2,1),(3,1),(4,3),(5,2),(6,2),(7,1),(8,2),(9,3),(10,4);
create table C (cid int , bfid int);
insert into C values
(1,1),(2,2),(3,1),(4,8),(5,6),(6,6),(7,10);
alter table A add index aid_idx (aid);
alter table B add index bid_idx (bid);
alter table B add index afid_idx (afid);
alter table C add index cid_idx (cid);
alter table C add index bfid_idx (bfid);
create TEMPORARY TABLE temp_table
select
B.afid from B
inner join C on B.bid = C.bfid ;
alter table temp_table add index dafid_idx(afid);
EXPLAIN SELECT A.*
FROM A
LEFT JOIN (
SELECT B.afid
FROM B
INNER JOIN C ON (B.bid = C.bfid)
) s ON (A.aid = s.afid);
+----+-------------+------------+-------+---------------+----------+---------+------+------+--------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+-------+---------------+----------+---------+------+------+--------------------------------+
| 1 | PRIMARY | A | ALL | NULL | NULL | NULL | NULL | 4 | |
| 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 7 | |
| 2 | DERIVED | C | index | bfid_idx | bfid_idx | 5 | NULL | 7 | Using index |
| 2 | DERIVED | B | ALL | bid_idx | NULL | NULL | NULL | 10 | Using where; Using join buffer |
+----+-------------+------------+-------+---------------+----------+---------+------+------+--------------------------------+
EXPLAIN
select A.* from A
LEFT JOIN temp_table on temp_table.afid = A.aid;
+----+-------------+------------+------+---------------+-----------+---------+------------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+------+---------------+-----------+---------+------------+------+-------------+
| 1 | SIMPLE | A | ALL | NULL | NULL | NULL | NULL | 4 | |
| 1 | SIMPLE | temp_table | ref | dafid_idx | dafid_idx | 5 | test.A.aid | 2 | Using index |
+----+-------------+------------+------+---------------+-----------+---------+------------+------+-------------+
However this query also be pretty good without using derived tables as
select
A.* from A
LEFT JOIN B on B.afid = A.aid
INNER JOIN C on C.bfid = B.bid
因此,根据您正在查看的结果集,您可以优化查询以避免使用派生表。如果可以使用收集结果集 只需加入表,然后在索引列上使用JOIN。
答案 2 :(得分:0)
这是我想出的(在网站发布之前)
SELECT *
FROM A
LEFT JOIN
b on a.aid = b.afid
left join c on b.bid=c.bfid
where not(bfid is null and bid is not null);
我认为它与原作有关。你需要尝试一下才能知道它是否更好。