Skip to content

Instantly share code, notes, and snippets.

@okabe-yuya
Created March 9, 2023 01:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save okabe-yuya/fa70bcd760fe100f54a0bec21cf66c94 to your computer and use it in GitHub Desktop.
Save okabe-yuya/fa70bcd760fe100f54a0bec21cf66c94 to your computer and use it in GitHub Desktop.
中間テーブルには複合インデックスと単一インデックスどちらを作成すれば良いのかで使用するsql

JOINのみ

EXPLAIN ANALYZE SELECT
  *
FROM
  user_products AS up
JOIN
  users AS u
ON
  up.user_id = u.user_id
JOIN
  products AS p
ON
  up.product_id = p.product_id
;

JOINしてWHERE

EXPLAIN ANALYZE SELECT
  *
FROM
  user_products AS up
JOIN
  users AS u
ON
  up.user_id = u.user_id
JOIN
  products AS p
ON
  up.product_id = p.product_id
WHERE
  u.user_name = 'ユーザーNo.5000' AND
  p.product_name = 'プロダクトNo.5000'
;

WHEREのみ

EXPLAIN ANALYZE SELECT * FROM user_products WHERE user_id = 5000 AND product_id = 5000;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment