584. Find Customer Referee
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| id | int |
| name | varchar |
| referee_id | int |
+-------------+---------+
In SQL, id is the primary key column for this table.
Each row of this table indicates the id of a customer, their name,
and the id of the customer who referred them.
Find the names of the customer that are not referred by the customer with id = 2.
Return the result table in any order.
해결방법
WHERE(row)
| id | name | referee_id |
| -- | ---- | ---------- |
| 1 | Will | null |
| 2 | Jane | null |
| 3 | Alex | 2 |
| 4 | Bill | null |
| 5 | Zack | 1 |
| 6 | Mark | 2 |
가설) WHERE 절의 조건문들의 순서를 바꿔서 필터링을 더 많이 할 수 있다면 성능이 향상될 것이다
SELECT name
FROM customer
WHERE referee_id !=2 or referee_id is null
SELECT name
FROM customer
WHERE referee_id is null or referee_id !=2
EXPLAIN 결과
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | SIMPLE | Customer | null | ALL | null | null | null | null | 6 | 86.11 | Using where |
두 쿼리의 EXPLAIN 결과는 동일하게 위와 같다. 즉, 성능의 차이가 없다
결론 : WHERE문에서의 필터링은 MySQL 옵티마이저가 자동으로 최적 순서를 찾아내기 때문에, 쿼리 작성자는 조건문의 순서에 신경 쓸 필요는 없다
불필요한 컬럼
위 문제는 총 3개의 컬럼이 있으며, 필요한 하는 컬럼은 2개이다.
동일한 문제에 대해 컬럼의 수가 많은데, 필요한 컬럼은 2개라고 가정해보자.
SELECT name
FROM (
SELECT name, referee_id
FROM Customer
) AS C
WHERE referee_id != 2 OR referee_id IS NULL
LIMIT
-- 예시) id기준으로 오름차순 정렬한 5개의 결과만 확인하기
SELECT *
FROM Customer
ORDER BY id
LIMIT 5
index