Where Inner Join

INNER JOIN / WHERE ์ฐจ์ด

๋™๋ฃŒ์˜ ์ฝ”๋“œ๋ฅผ ๋ณด๋‹ค INNER JOIN๊ณผ WHERE์„ ํ˜ผ์šฉํ•ด์„œ ์‚ฌ์šฉํ•˜๊ธธ๋ž˜ ์–ด๋–ค๊ฒƒ์ด ๋” ์ข‹์€ ์ฝ”๋“œ์ธ์ง€ ๊ถ๊ธˆํ•ด์„œ ์ฐพ์•„๋ณด๊ธฐ๋กœ ํ–ˆ๊ณ  ์ด๋ถ€๋ถ„์„ ์ ์šฉํ•˜์˜€๋‹ค.

๋จผ์ € JOIN๊ณผ WHERE์˜ ํผํฌ๋จผ์Šค ์ธก๋ฉด์—์„œ๋Š” ๋™์ผํ–ˆ๋‹ค. ๊ณ„์† ์‚ฌ์šฉํ•ด๋„ ๋ญ”๊ฐ€ ํ•˜๋‚˜ ํŠน๋ณ„ํžˆ ์ข‹์€ ์ง€๋„ ๋ชฐ๋ž์—ˆ์ง€๋งŒ, ์ฝ”๋“œ๊ฐ€ ๊ธธ์–ด์ง€๋ฉด ๊ธธ์–ด์งˆ์ˆ˜๋ก ์ฟผ๋ฆฌ๊ฐ€ ๋ณต์žกํ•˜๋ฉด ๋ณต์žกํ• ์ˆ˜๋ก WHERE๊ณผ ํ…Œ์ด๋ธ”์„ JOINํ•˜๋Š” ์ฝ”๋“œ๊ฐ€ ๋ฉ€์–ด์ ธ์„œ ๊ฒฐ๊ตญ ํ˜ผ์šฉํ•ด์„œ ์‚ฌ์šฉํ•˜๋ฉด ์–ด๋–ค ์กฐ๊ฑด์ด ๋“ค์–ด๊ฐ€ ์žˆ๋Š”์ง€ ํ•œ๋ˆˆ์— ์ฝ๊ธฐ ํž˜๋“ค๋‹ค๋Š” ๊ฒƒ์„ ๋Š๊ผˆ๋‹ค.

๊ทธ๋ž˜์„œ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ์˜๊ฒฌ์„ ์ฐธ๊ณ ํ•˜์—ฌ ์ฟผ๋ฆฌ๋ฌธ์ด ์‹ฑ๊ธ€ ํ…Œ์ด๋ธ” ์ผ ๊ฒฝ์šฐ WHERE์„ ์‚ฌ์šฉ, ๋ฉ€ํ‹ฐ ํ…Œ์ด๋ธ”์ผ ๊ฒฝ์šฐ INNER JOIN์„ ์‚ฌ์šฉํ•˜๊ธฐ๋กœ ์ปจ๋ฒค์…˜์„ ์ •ํ–ˆ๋‹ค.

์•„๋ž˜ ์ฝ”๋“œ๋Š” ๋™๋ฃŒ๊ฐ€ ๊ฐœ๋ฐœํ•œ ์ฝ”๋“œ๋‹ค

SELECT ai.brand,
       ai.model,
       ai.pattern,
       ai.min_bid,
       b.name_image,
       im.image,
       ga.idx,
       ga.start_dt,
       ga.finish_dt,
       ai.size,
       ai.special_gift,
       b.white_name
FROM auction_info ai
         INNER JOIN goods g ON ai.goods_idx = g.idx
         INNER JOIN item i ON g.item_idx = i.idx
         INNER JOIN model m ON i.model_idx = m.idx
         INNER JOIN brand b ON m.brand_code = b.code
         INNER JOIN auction_image im ON im.auction_info_idx = ai.idx
         INNER JOIN goods_auction ga ON ai.idx = ga.auction_info_idx
WHERE im.type = 'MF'
  AND g.status = 'A005'
  AND ga.is_ready_active = 1
  AND ga.start_dt IS NOT NULL
  AND ga.start_dt >= now()
ORDER BY ga.start_dt ASC
LIMIT 5;

์•„๋ž˜ ์ฝ”๋“œ๋Š” WHERE ์กฐ๊ฑด์„ ์ „๋ถ€ INNER JOIN์œผ๋กœ ์˜ฎ๊ฒผ๋‹ค

SELECT CONCAT(ai.model, ' ', ai.pattern, ' ', ai.size) as name,
              ai.min_bid as minBbd,
              b.name_image as nameImage,
              im.image,
              ga.idx,
              ga.start_dt as startDt,
              ga.finish_dt as finishDt,
              ai.size,
              ai.special_gift as specialGift,
              b.white_name as whiteName
       FROM auction_info ai
            INNER JOIN goods g
                   ON ai.goods_idx = g.idx AND g.status = 'A005'
            INNER JOIN item i ON g.item_idx = i.idx
            INNER JOIN model m ON i.model_idx = m.idx
            INNER JOIN brand b ON m.brand_code = b.code
            INNER JOIN auction_image im
                   ON im.auction_info_idx = ai.idx
                   and im.type = 'MF'
            INNER JOIN goods_auction ga
                   ON ai.idx = ga.auction_info_idx
                   AND ga.is_ready_active = 1
                   AND ga.start_dt IS NOT NULL
                   AND ga.start_dt >= now()
       ORDER BY ga.start_dt ASC
       LIMIT 5

๋ฌผ๋ก  ๋‘˜์ค‘ ์–ด๋–ค ์กฐ๊ฑด์‹์„ ์ฃผ์–ด๋„ ๋™์ผํ•œ ๊ฒฐ๊ณผ์™€ ๋™์ผํ•œ ํผํฌ๋จผ์Šค๊ฐ€ ๋‚˜์˜ค๊ฒ ์ง€๋งŒ, ์ง€์†๊ฐ€๋Šฅํ•œ ์ฝ”๋“œ๋ฅผ ์œ„ํ•ด ๊ฐ€๋…์„ฑ์„ ๋จผ์ € ์ƒ๊ฐํ•˜๋Š” ๋‚ด ์Šคํƒ€์ผ๋กœ๋Š” ์ด ๋ฐฉ์‹์ด ๋” ์ž˜๋งž๋Š”๋“ฏ ํ•˜๋‹ค.

What is different between โ€œINNER JOINโ€ and WHERE in mariaDB

I was reading my coworkerโ€™s query code suddenly I was curios what is more better code between INNER JOIN and WHERE so I looked up and I changed code convention about this part.

First, JOIN and WHERE are equal performance. so I didnโ€™t know what is better code before I read complicated query. When I try to read complicated query source, I realized it when I read complicated code.

Therefore, referring to the opinions of others, I got dcided to use WHERE if the query statement was a single table and INNER JOIN if it was a multi-table.

This is the coworker's code

SELECT ai.brand,
       ai.model,
       ai.pattern,
       ai.min_bid,
       b.name_image,
       im.image,
       ga.idx,
       ga.start_dt,
       ga.finish_dt,
       ai.size,
       ai.special_gift,
       b.white_name
FROM auction_info ai
         INNER JOIN goods g ON ai.goods_idx = g.idx
         INNER JOIN item i ON g.item_idx = i.idx
         INNER JOIN model m ON i.model_idx = m.idx
         INNER JOIN brand b ON m.brand_code = b.code
         INNER JOIN auction_image im ON im.auction_info_idx = ai.idx
         INNER JOIN goods_auction ga ON ai.idx = ga.auction_info_idx
WHERE im.type = 'MF'
  AND g.status = 'A005'
  AND ga.is_ready_active = 1
  AND ga.start_dt IS NOT NULL
  AND ga.start_dt >= now()
ORDER BY ga.start_dt ASC
LIMIT 5;

so I changed like this

SELECT CONCAT(ai.model, ' ', ai.pattern, ' ', ai.size) as name,
              ai.min_bid as minBbd,
              b.name_image as nameImage,
              im.image,
              ga.idx,
              ga.start_dt as startDt,
              ga.finish_dt as finishDt,
              ai.size,
              ai.special_gift as specialGift,
              b.white_name as whiteName
       FROM auction_info ai
            INNER JOIN goods g
                   ON ai.goods_idx = g.idx AND g.status = 'A005'
            INNER JOIN item i ON g.item_idx = i.idx
            INNER JOIN model m ON i.model_idx = m.idx
            INNER JOIN brand b ON m.brand_code = b.code
            INNER JOIN auction_image im
                   ON im.auction_info_idx = ai.idx
                   and im.type = 'MF'
            INNER JOIN goods_auction ga
                   ON ai.idx = ga.auction_info_idx
                   AND ga.is_ready_active = 1
                   AND ga.start_dt IS NOT NULL
                   AND ga.start_dt >= now()
       ORDER BY ga.start_dt ASC
       LIMIT 5

Of course, any conditional expression of the two will produce the same result and the same performance, but this method seems to fit better with my style of thinking about readability first for sustainable code.