Convert SQL WHERE IN to JOIN

后端 未结 2 1880
再見小時候
再見小時候 2020-12-22 09:57

I have a database storing various information about fictional people. There is a table person with general information, such as name, adress etc and some more specific table

相关标签:
2条回答
  • 2020-12-22 10:38

    Depends on your SQL engine. Newer SQL systems that have reasonable query optimizers will most likely rewrite both IN and JOIN queries to the same plan. Typically, a sub-query (IN Clause) is rewritten using a join.

    In simple SQL engines that may not have great query optimizers, the join should be faster because they may run sub-queries into a temporary in-memory table before running the outer query.

    In some SQL engines that have limited memory footprint, however, the sub-query may be faster because it doesn't require joining -- which produces more data.

    0 讨论(0)
  • 2020-12-22 10:43

    I think I understand what you're trying to do. There is more than one way to skin a cat, but may I suggest splitting your query into two separate queries, and then replacing the complicated WHERE clause with a couple inner joins? So, something like this:

    /* Find connections based on health care */
    SELECT p2.p_id as id, p2.fname, p2.lname, p2.image
    FROM person p
    JOIN health_case hc on hc.patient = p.p_id
    JOIN health_case hc2 on hc2.doctor = hc.doctor and hc2.healthcenter = hc.healthcenter and hc.start <= hc2.end and hc.end >= hc2.start and hc2.patient <> hc.patient
    JOIN person p2 on p2.p_id = hc2.patient and p2.p_id <> p.p_id
    WHERE p.p_id = :id
    

    Then, create a separate query to get connections based on education:

    /* Find connections based on education */
    SELECT p2.p_id as id, p2.fname, p2.lname, p2.image
    FROM person p
    JOIN education e on e.pupil = p.p_id
    JOIN education e2 on e2.school = e.school and e2.start <= e.end AND e2.end >= e.start and e.pupil <> e2.pupil
    JOIN person p2 on p2.p_id = e2.pupil and p2.p_id <> p.p_id
    WHERE p.p_id = :id
    

    If you really want the data results to be combined, you can use UNION since both queries return the same columns from the person table.

    0 讨论(0)
提交回复
热议问题