안녕하세요
마푸입니다.
너무 게임만 올려서 디비 쪽 정리한거 좀 풀어보려합니다.
첫번째 이슈는 stored procedure(DB SP)를 작성할때 팁입니다.
대부분의 절차적 프로그램에서는
보통 쿼리를 실행하고 결과에 따라서 확인한다음에 업데이트를 하게 됩니다.
허나 디비에서는 그런식으로 처리하게되면 쿼리가 두번 날아가게 되는것이지요
만약, 정말 100% 확실한 데이터로 업데이터가 진행되는 거라고 가정을 한다면
먼저 업데이터를 하고 업데이트의 결과유무를 체크한다음 업데이트가 안되었다면 입력하는 식으로 만들 수 있는 것이지요
자세한 내용은 아래를 참고합시다 ^^
[정리]
1. Type A
IF(SELECT * FROM ~~)
update ~~
ELSE
INSERT ~~
--> 테이블을 무조껀 두번 읽는다
Update~~
IF @@ROWCOUNT > 0
return 1
ELSE
INSERT
--> 업데이트는 한번 인서트는 두번 읽는다
-- Type A
select @PlayerIdx = PlayerIndex, @LoginServerID = LoginServerID from LoginKey where LoginKey = @GUID AND WorldIndex = @WorldNumber
if(@PlayerIdx = 0 )
begin
return 0;
end
select @ID = PlayerID from Player where PlayerIndex = @PlayerIdx
if(@ID = '')
begin
return 0;
end
-- Type B
IF EXISTS
(
SELECT *
FROM dbo .Player p inner join dbo.LoginKey l on p .PlayerIndex = l .PlayerIndex
WHERE l .LoginKey = @GUID AND l.WorldIndex = @WorldNumber
)
BEGIN
update Player set State = 'MANAGER' where PlayerID = @ID
return 1
END
ELSE
BEGIN
return 0
END
-- Type C
UPDATE dbo .Player
SET State = 'MANAGER'
FROM dbo.Player P inner join dbo.LoginKey L on P .PlayerIndex = L.PlayerIndex
WHERE l .LoginKey = @GUID AND l.WorldIndex = @WorldNumber
If @@ROWCOUNT > 0
return 0
ELSE
return 1
반응형
'Database' 카테고리의 다른 글
[MySQLdb] python에서 mysql db api 설치하기 (0) | 2017.02.22 |
---|---|
[DBMS] 선택도와 밀도의 차이점 (2) | 2017.02.21 |
[MySQL] 복제 (Replication)에 대해서 (0) | 2017.02.14 |
[MySQL] Binary Log (0) | 2017.02.14 |
[MySQL] Tirgger를 사용해 보자 (0) | 2017.02.14 |