2024/03(19)
-
소수인지 판단할 때 시간 복잡도 줄이기
https://school.programmers.co.kr/learn/courses/30/lessons/92335 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 위 문제를 풀 때 소수인지 판단하는 메소드를 다음과 같이 구현했었다. 그런데 시간 초과가 났다. public boolean isPrime(long num) { for (int i = 2; i < num; i++) { if (num % i == 0) { return false; } } return true; } 만약 100이 소수인지 판단한다고 가정하자. 100의 약수는 [1, 2, 4, 5, 10..
2024.03.21 -
[Real MySQL] 9. 옵티마이저와 힌트
옵티마이저 옵티마이저는 가장 효율적인 방법으로 SQL을 수행할 최적의 처리 경로를 생성해주는 DBMS의 핵심 엔진이다. MySQL 서버로 요청된 쿼리는 결과는 동일하지만 내부적으로 그 결과를 만들어내는 방법은 매우 다양하다. MySQL의 옵티마이저는 쿼리를 최적으로 실행하기 위한 실행 계획을 수립하는 작업을 진행한다. EXPLAIN이라는 명령으로 쿼리의 실행 계획을 확인할 수 있다. 그리고 실행 계획을 제대로 이해하려면 MySQL 서버 옵티마이저가 실행하는 최적화에 대해 어느 정도 지식을 갖추고 있어야 한다. 쿼리 실행 절차 MySQL 서버에서 쿼리가 실행되는 과정은 다음과 같다. SQL 파싱: 사용자로부터 요청된 SQL 문장을 잘게 쪼개서 MySQL 서버가 이해할 수 있도록 파싱하여 트리 형태(파스 트..
2024.03.19 -
[Real MySQL] 8. 인덱스 - 클러스터링 인덱스
클러스터링 인덱스(클러스터링 테이블) 클러스터링 인덱스는 테이블의 PK에 대해서 적용되는 내용이다. 즉 PK 값이 비슷한 레코드끼리 묶어서 저장하는 것을 클러스터링 인덱스라고 표현한다. 참고로 MySQL에서 클러스터링 인덱스는 InnoDB 스토리지 엔진에서 디폴트로 설정된다. 클러스터링 인덱스, 즉 PK 값에 의해 레코드의 저장 위치가 결정된다. 따라서 PK 값이 바뀌면 그 레코드의 물리적 저장 위치가 바뀌어야 한다. 즉, PK 값으로 클러스터링된 테이블은 PK 값에 대한 의존도가 크기 때문에 신중히 PK 값을 설정해야 한다. 클러스터링 인덱스는 PK 값에 의해 레코드의 저장 위치가 결정되므로 사실 인덱스 알고리즘이라기보다 테이블 레코드의 저장 방식이라고 볼 수 있다. 그래서 "클러스터링 인덱스"와 "클..
2024.03.19 -
[Real MySQL] 8. 인덱스 - B-Tree, B+Tree, Hash 인덱스
B-Tree 인덱스 B-Tree 인덱스에 대해 알기 전에 먼저 B-Tree 자료구조에 대해 알아보자. B-Tree는 자식을 2개만 갖는 이진 트리를 확장하여 N개의 자식을 갖는다. 그리고 좌우 자식 간의 균형이 맞지 않을 경우 비효율적이기 때문에 항상 균형을 맞춘다는 의미에서 균형 트리(Balanced Tree) 라고 한다. 즉, B-Tree의 B는 Binary(이진)의 약자가 아니라 Balanced(균형)의 약자이다. B-Tree는 데이터베이스의 인덱싱 알고리즘 중 가장 일반적으로 사용되고, 가장 먼저 도입된 알고리즘이다. B-Tree는 칼럼의 원래 값을 변형시키지 않으며 항상 정렬된 상태로 유지한다. 전문 검색과 같은 특수한 요건이 아닌 경우 대부분의 인덱스는 거의 B-Tree를 사용할 정도로 일반적..
2024.03.18 -
[Real MySQL] 8. 인덱스 - 디스크 읽기 방식, 인덱스
디스크 읽기 방식 컴퓨터의 CPU나 메모리처럼 전기적 특성을 띤 장치의 성능은 짧은 시간 동안 매우 빠른 속도로 발전했지만, 디스크 같은 기계식 장치의 성능을 상당히 제한적으로 발전했다. 비록 최근에는 자기 디스크 원판에 의존하는 HDD(하드 디스크 드라이브)보다 SSD가 많이 활용되고 있지만, 여전히 데이터 저장 매체는 컴퓨터에서 가장 느린 부분이다. 따라서 데이터베이스의 성능 튜닝은 어떻게 디스크 I/O를 줄이느냐가 관건일 때가 상당히 많다. 하드 디스크 드라이브(HDD)와 솔리드 스테이트 드라이브(SSD) 컴퓨터에서 CPU나 메모리 같은 주요 장치는 대부분 전자식 장치이지만 하드 디스크 드라이브(HDD)는 기계식 장치이다. 따라서 데이터베이스 서버에서 항상 디스크 장치가 병목이 된다. 이러한 기계식..
2024.03.18 -
SQL 알아보기
SQL SQL은 Structed Query Language의 약자로 데이터 관리 시스템인 DBMS에서 데이터를 조작하고 조회하기 위해 사용하는 언어이다. DBMS의 종류는 다양한데 이러한 DBMS 벤더들이 각자의 언어와 문법만 고집한다면 사용자들은 DBMS 사용이 어려워진다. 따라서 ANSI는 표준이 되는 SQL을 만들었고 이를 ANSI SQL이라 부른다. 이 ANSI SQL은 현대의 대부분의 DBMS에서 작동한다. 따라서 학습 시간을 줄여주고 벤더 변경시 변경 비용이 적다는 장점이 있다. SQL을 프로그래밍 언어와 비교하면, SQL은 데이터베이스 관리를 위한 언어이며 데이터 조작을 위한 목적으로 사용되지만, C언어 같은 프로그래밍 언어는 다양한 프로그래밍 영역에서 사용되는 일반 목적의 프로그래밍 언어이..
2024.03.14