使用共享子查询的MySQL子查询优化

时间:2019-04-17 05:14:20

标签: mysql sql

从这些表中,我编写了此子查询及其根据要求给出的结果。

需要专家指导来改进此查询,或者我们是否也可以对这些表使用联接。

查询:

select ps,st from pac where con in (select 
config from config where logi in 
( select id from logicalnode where physi 
in (select id from ysicalnode where mas =11)));

5 个答案:

答案 0 :(得分:0)

您可以尝试以下操作-使用JOIN

select payloadstr,starttime 
    from packetdb.packet a inner join packetdb.configuration b on a.configid=b.idconfig
    inner join packetdb.logicalnode c on logicalnodeid=c.id
    inner join packetdb.physicalnode d on physicalnodeid=d.id
    where macaddress =117769729

答案 1 :(得分:0)

尝试一下

    select pa,sta 
    from 
   pack p
    INNER JOIN
  confi c
    ON
    p.confi = c.idco
    INNER JOIN
   logice l
    ON
    c.logic = l.id
    INNER JOIN
  physiode pn
    ON
    l.physicalnodeid  = pn.id
    WHERE macaddress =123

答案 2 :(得分:0)

您可以尝试使用exists

select payloadstr,starttime from packetdb.packet p
where exists(select 1 from packetdb.configuration c
             where p.configid = id
             and exists(select 1 from packetdb.logicalnode l
                          where c.logicalnodeid = id
                          and exists(select 1 from packetdb.physicalnode
                                     where macaddress = 117769729
                                     and l.physicalnodeid = id)

答案 3 :(得分:0)

使用Left join

select payloadstr,starttime 
from packet 
left join Configuration on Configuration.IDconfig = packet.configID
left join logicalnode on logicalnode.ID = Configuration.logicalnodeid
left join physicalnode on physicalnode.ID = logicalnode.physicalnodeid
 where macaddress =117769729

答案 4 :(得分:0)

SELECT
    payloadstr
    ,starttime
FROM packetdb.packet
INNER JOIN packetdb.configuration
    ON packetdb.packet.configid = packetdb.configuration.idconfig
INNER JOIN packetdb.logicalnode
    ON packetdb.configuration.idconfig = packetdb.logicalnode.id
INNER JOIN packetdb.physicalnode
    ON packetdb.logicalnode.physicalnodeid = packetdb.physicalnode.id and packetdb.physicalnode.macaddress=117769729