게시판을 작성하다 보면 페이징으로 인해서 부득이 하게 limit 20,10 이런식으로 쿼리를 작성하게 된다. 문제는 LIMIT 20,10 테이블 행의 풀 스캔을 일으킬수 있다는 것이다. 그리고 비용도 많이 드는데 이를 피할 수 있는 방법은 다음과 같다.
[code sql]
select * from xe_comments_list limit 20, 10;
[/code]
이게 과연 효과가 있을까? 다음을 보자.
[code sql]
mysql> explain extended select * from xe_comments_list limit 20, 10;
+----+-------------+------------------+------+---------------+------+---------+------+---------+----------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+------------------+------+---------------+------+---------+------+---------+----------+-------+
| 1 | SIMPLE | xe_comments_list | ALL | NULL | NULL | NULL | NULL | 1016616 | 100.00 | |
+----+-------------+------------------+------+---------------+------+---------+------+---------+----------+-------+
1 row in set, 1 warning (0.00 sec)
mysql> show status like 'last_query_cost';
+-----------------+---------------+
| Variable_name | Value |
+-----------------+---------------+
| Last_query_cost | 208867.199000 |
+-----------------+---------------+
1 row in set (0.00 sec)
mysql> explain extended select head, arrange from xe_comments_list inner join ( select comment_srl from xe_comments_list limit 20, 10) as lim using(comment_srl);
+----+-------------+------------------+--------+---------------+---------+---------+-----------------+---------+----------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+------------------+--------+---------------+---------+---------+-----------------+---------+----------+-------------+
| 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 10 | 100.00 | |
| 1 | PRIMARY | xe_comments_list | eq_ref | PRIMARY | PRIMARY | 8 | lim.comment_srl | 1 | 100.00 | |
| 2 | DERIVED | xe_comments_list | index | NULL | PRIMARY | 8 | NULL | 1016616 | 100.00 | Using index |
+----+-------------+------------------+--------+---------------+---------+---------+-----------------+---------+----------+-------------+
3 rows in set, 1 warning (0.00 sec)
mysql> show status like 'last_query_cost';
+-----------------+----------+
| Variable_name | Value |
+-----------------+----------+
| Last_query_cost | 0.000000 |
+-----------------+----------+
1 row in set (0.00 sec)
[/code]
last_query_cost 가 join 을 하는 방법이 더 적다.
0 개의 댓글:
댓글 쓰기