简体   繁体   中英

My Odd SubSelect, Need a LEFT JOIN Improvement

Here is a sample SQL dump: https://gist.github.com/JREAM/99287d033320b2978728

  • I have a SELECT that grabs a bundle of users.
  • I then do a foreach loop to attach all the associated tree_processes to that user.
  • So I end up doing X Queries: users * tree.

Wouldn't it be much more efficient to fetch the two together?

  • I've thought about doing a LEFT JOIN Subselect, but I'm having a hard time getting it correct.
  • Below I've done a query to select the correct data in the SELECT, however I would have to do this for all 15 rows and it seems like a TERRIBLE waste of memory.

This is my dirty Ateempt:

-

    SELECT
            s.id,
            s.firstname,
            s.lastname,
            s.email,
            (
                SELECT tp.id FROM tree_processes AS tp
                JOIN tree AS t ON (
                    t.id = tp.tree_id
                )
                WHERE subscribers_id = s.id
                ORDER BY tp.id DESC
                LIMIT 1
            ) AS newest_tree_id,
            #
            # Don't want to have to do this below for every row
            (
                SELECT t.type FROM tree_processes AS tp
                JOIN tree AS t ON (
                    t.id = tp.tree_id
                )
                WHERE subscribers_id = s.id
                ORDER BY tp.id DESC
                LIMIT 1
            ) AS tree_type


    FROM subscribers AS s
    INNER JOIN scenario_subscriptions AS ss ON (
        ss.subscribers_id = s.id
    )

    WHERE ss.scenarios_id = 1
    AND ss.completed != 1
    AND ss.purchased_exit != 1
    AND deleted != 1

    GROUP BY s.id
    LIMIT 0, 100

This is my LEFT JOIN attempt, but I am having trouble getting the SELECT values

SELECT
        s.id,
        s.firstname,
        s.lastname,
        s.email,
        freshness.id,
        # freshness.subscribers_id < -- Cant get multiples out of the LEFT join


FROM subscribers AS s
INNER JOIN scenario_subscriptions AS ss ON (
    ss.subscribers_id = s.id
)


LEFT JOIN ( SELECT tp.id, tp.subscribers_id AS tp FROM tree_processes AS tp
            JOIN tree AS t ON (
                t.id = tp.tree_id
            )
            ORDER BY tp.id DESC
            LIMIT 1 ) AS freshness 
ON (
    s.id = subscribers_id
)

WHERE ss.scenarios_id = 1
AND ss.completed != 1
AND ss.purchased_exit != 1
AND deleted != 1

GROUP BY s.id
LIMIT 0, 100

In the LEFT JOIN you are using 'freshness' as the table alias. This in you select you need to additionally state what column(s) you want from it. Since there is only one column (id) you need to add:

freshness.id

to the select clause.

Your ON clause of the left join looks pretty dodgy too. Maybe freshness.id = ss.subscribers_id?

Cheers -

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM