April 12, 2022
01.Creat table
02.타입
02.1 Json Quert Example
03.Shell
04.DDL
05.Insert
06.SELECT - Alias
06.1 Extract UTC Time
06.2 JSON
06.3 Post List 추출1
06.4 Post List 추출2
06.5 Post List 부분의 문제
07.Update
08.DELETE
09.쿼리 최적화
CREATE TABLE post
(
id SERIAL PRIMARY KEY,
type VARCHAR(16) NOT NULL default 'default',
user_id INTEGER NOT NULL references user(id),
title VARCHAR(256) NOT NULL,
description TEXT NOT NULL,
child_count INTEGER NOT NULLL default 0,
state VARCHAR(16) NOT NULL DEFAULT 'open',
mention_users INTEGER[],
ip_address CHAR(15),
info JSON,
parent references post(id)
create_time TIMESTAMP without time zone NOT NULL defualt(now() at time zone 'utc')
);
검색 속도를 위해서는 INTEGER로 하고 해당 테이블을 연관 테이블로 매칭하는 것도 방법
child_count
국제화를 하려면 신경 써야하는부분
SERIAL :
References:
Integer[]:
Char:
Json:
UTC:
UPDATE user
SET profile_image =
'{"lg"/assets/images/guest_lg.png","md":"assets/images/guest_md.png","sm":"/assets/images/guest_sm.png"}'
WHERE id =1;
명령
Shell에서 직접 쿼리를 적을 수 있지만
SET, Drop not null
ALTER TABLE t_user ALTER COLUMN first_name SET not null;
ALTER TABLE t_user ALTER COLUMN last_name DROP not null;
Change column type
ALTER TABLE t_post ALTER COLUMN title TYPE varchar(256);
Change column name
ALTER TABLE t_user_detail RENAME birth_date TO birth_day;
Setting default value
ALTER TABLE t_signup ALTER COLUMN expire_date SET DEFAULT now() at time zone 'utc' + time '00:10:03';
Create index & Drop index
CREATE INDEX idx_post_poll ON t_post_poll(post_id, poll);
DROP INDEX idx_post_poll;
다른 테이블에서 여러 개수의 데이터 입력
INSERT INTO user(email, nickname)
SELECT email, nickname
FROM signup
WHERE id in(2,5,10,11,12)
데이터가 없다면 입력, 있으면 업데이트 하기
INSERT INTO user_medal_season(user_id, medal_point) VALUES(4,1)
ON CONFLICT (user_id) DO UPDATE
SET medal_point = user_medal_season_point + 1;
INSERT와 WITH구문 같이 쓰기
WITH
daily_count as (SELECT count(*) FROM user WHERE (now() at time zone 'utc' - interval '24 hours') < last_login_time),
weekly_count as (SELECT count(*) FROM user WHERE (now() at time zone 'utc' - interval '7 days') < last_login_time),
total_count as (SELECT count(*) FORM user)
INSERT INTO retention_rate(login_user, login_user_week, total_user, rate) VALUES
(SELECT count FROM daily_count),
(SELECT count FROM weekly_count),
(SELECT count FROM total_count),
(SELECT count FROm daily_count) / (SELECT count::float FORM total_count) * 100
SELECT tb_id as "boardId",
tc.id as "community Id"
...
FROM t_board tb,
t_community tc
WHERE tc.id = tb.community_id
AND tc.name = '3dpit programming'
AND tb.name = 'qna board';
그냥 as boardID로 하면 그냥 소문자로 되고
UTC로 저장된 시간값을 숫자형태로 추출할 때
SELECT email,
extract(epoch from expire_time at time zone 'utc')::Integer as "expireTime"
FROM signup
WHERE id = 'clkmnaslnfaefn-39nalfn1209n=ldfoi3n';
SELECT tp.title
,tp.relative_files
,tf.info->'imageNames'->>'md' as image
FROM post tp
LEFT JOIN file_info tf ON tf.id = tp.relative_files[1]
WHERE tp.id = 198
SELECT tp.id
,tu.id as "userId"
,tu.username
,tc.name as "communityName"
,tp.view_count as "viewCount"
,tp.vote_count as "voteCount"
,tp.comment_count as "commentCount"
,tp.point
,(
SELECT row_to_json(t)
FROM (
SELECT tpp.id
,(SELECT array_agg(row_tojson(t))
)as "relatvieFiles"
,tppu.username
,tppu.type
,extract(epoch from tpp.create_time)::INTEGER as "createTime"
,tpp.title
,tpp.description
FROM t_post tpp
,t_user tppu
WHERE tpp.id = tp.parent_post_id
AND tpp.user_id = tppu.id
)t
)as "parentPostPreview"
row_to_json()
을 이용,(
SELECT count(*)
FROM t_post
WHERE parent_post_id = tp.id
)as "childPostCount"
UPDATE t_community tc
SET member_count = COALESCE(
(
SELECT count(*)
FROM t_user_community tuc
WHERE tc.id = tuc.community_id GROUP BY tuc,community_id
), 0
);
DELETE FROM client_error t
WHERE exists(
SELECT true
FROM client_error t2
WHERE t2.id = 3
AND t2.message = t.message
AND t2.url = t.url
AND t2.line_number = t.line_number
AND t2.column_number = t.column_number
)
쿼리 시간 확인을 위한 방법 explain
PostgreSQL의 경우
EXPLAIN ANALYZE <<QUERY>>