SQL の IN と EXISTS: パフォーマンスと使用法を理解する

DDD
リリース: 2024-09-14 06:19:37
オリジナル
694 人が閲覧しました

IN vs EXISTS in SQL: Understanding Performance and Usage

MySQL における IN と EXISTS: 実践的な例と説明

MySQL では、サブクエリ内の行の存在に基づいてデータをフィルタリングするために、クエリで IN と EXISTS の両方が使用されます。ただし、それらは異なる方法で動作するため、どちらを選択するかによってクエリのパフォーマンスに影響を与える可能性があります。説明と実際の例を使用して、それらの違いを詳しく見てみましょう。


1. IN 句

  • 説明:
    IN 句は、列の値がリストまたはサブクエリ内の値と一致するかどうかに基づいて行をフィルター処理するために使用されます。内部クエリからの一致する値をチェックし、それらを外部クエリと比較します。

  • パフォーマンス:
    IN 句は通常、サブクエリが少数のレコードを返す場合に効率的です。ただし、サブクエリが大きなデータセットを返す場合、IN は遅くなる可能性があります。

  • 構文:

  SELECT columns 
  FROM table 
  WHERE column IN (subquery);
ログイン後にコピー

2. EXISTS 条項

  • 説明:
    EXISTS 句は、サブクエリによって返された行の存在をチェックします。サブクエリが行を返す場合、EXISTS は TRUE と評価され、外側のクエリが続行されます。行の内容は考慮されず、行が存在するかどうかのみが考慮されます。

  • パフォーマンス:
    EXISTS は、一致が見つかると処理を停止するため、大規模なデータセットの場合は通常より高速です。これにより、多くの行を返すサブクエリを操作する場合に効率的になります。

  • 構文:

  SELECT columns 
  FROM table 
  WHERE EXISTS (subquery);
ログイン後にコピー

実践例

顧客と注文という 2 つのテーブルを考えてみましょう。

顧客テーブル:

customer_id customer_name
1 John Doe
2 Jane Smith
3 Alice Brown

注文テーブル:

order_id customer_id order_total
1 1 200
2 1 150
3 2 300

We want to find all customers who have placed at least one order.


Using the IN Clause

SELECT customer_name 
FROM customers 
WHERE customer_id IN (SELECT customer_id FROM orders);
ログイン後にコピー
ログイン後にコピー

Explanation:

  • The subquery (SELECT customer_id FROM orders) returns all customer IDs that appear in the orders table.
  • The outer query selects customers whose customer_id is in that result set.

Result:
| customer_name |
|---------------|
| John Doe |
| Jane Smith |


Using the EXISTS Clause

SELECT customer_name 
FROM customers c
WHERE EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.customer_id);
ログイン後にコピー
ログイン後にコピー

Explanation:

  • The subquery SELECT 1 FROM orders o WHERE o.customer_id = c.customer_id checks whether any row in the orders table matches the customer_id of the current row from the customers table.
  • If any match is found, EXISTS returns TRUE, and the customer is included in the result.

Result:
| customer_name |
|---------------|
| John Doe |
| Jane Smith |


Key Differences

  1. Return Values:

    • IN: Compares the values of a column with the result set of the subquery.
    • EXISTS: Returns TRUE or FALSE based on whether the subquery returns any rows.
  2. Efficiency:

    • IN is more efficient for smaller datasets.
    • EXISTS is faster for large datasets, especially when the subquery returns many rows.
  3. Use Case:

    • Use IN when you're comparing a column’s value against a small list of possible values.
    • Use EXISTS when you're checking for the presence of rows in a subquery (e.g., when there's a correlation between the outer and inner queries).

Performance Example

Assume we have:

  • 10,000 customers
  • 100,000 orders

Query with IN:

SELECT customer_name 
FROM customers 
WHERE customer_id IN (SELECT customer_id FROM orders);
ログイン後にコピー
ログイン後にコピー
  • Execution: MySQL will retrieve the entire result set from the subquery and compare it with each row in the outer query.

Query with EXISTS:

SELECT customer_name 
FROM customers c
WHERE EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.customer_id);
ログイン後にコピー
ログイン後にコピー
  • Execution: MySQL will check each row in the outer query and stop once it finds a matching row in the subquery, making it faster for large datasets.

Conclusion

  • Use IN when you have a simple list to compare or a small subquery result.
  • Use EXISTS when you’re dealing with large datasets or need to check for the presence of related data in a subquery.

以上がSQL の IN と EXISTS: パフォーマンスと使用法を理解するの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ソース:dev.to
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!