Skip to content

Commit 0a3d855

Browse files
committed
Buff/Debuff Database call Optimization
1 parent a54616c commit 0a3d855

File tree

6 files changed

+265
-528
lines changed

6 files changed

+265
-528
lines changed

Server.Configurations/ZolianPlayers/PlayersDatabaseBuild.sql

Lines changed: 72 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ DROP PROCEDURE IF EXISTS [dbo].[SelectBanked]
3232
DROP PROCEDURE IF EXISTS [dbo].[SelectEquipped]
3333
DROP PROCEDURE IF EXISTS [dbo].[SelectInventory]
3434
DROP PROCEDURE IF EXISTS [dbo].[SelectDiscoveredMaps]
35-
DROP PROCEDURE IF EXISTS [dbo].[SelectDeBuffsCheck]
3635
DROP PROCEDURE IF EXISTS [dbo].[SelectDeBuffs]
37-
DROP PROCEDURE IF EXISTS [dbo].[SelectBuffsCheck]
3836
DROP PROCEDURE IF EXISTS [dbo].[SelectBuffs]
37+
DROP PROCEDURE IF EXISTS [dbo].[PlayerDeBuffSync]
38+
DROP PROCEDURE IF EXISTS [dbo].[PlayerBuffSync]
3939
DROP PROCEDURE IF EXISTS [dbo].[PlayerSecurity]
4040
DROP PROCEDURE IF EXISTS [dbo].[PlayerSaveSpells]
4141
DROP PROCEDURE IF EXISTS [dbo].[PlayerSaveSkills]
@@ -46,16 +46,12 @@ DROP PROCEDURE IF EXISTS [dbo].[PlayerCreation]
4646
DROP PROCEDURE IF EXISTS [dbo].[AccountLockoutCount]
4747
DROP PROCEDURE IF EXISTS [dbo].[PasswordSave]
4848
DROP PROCEDURE IF EXISTS [dbo].[InsertQuests]
49-
DROP PROCEDURE IF EXISTS [dbo].[InsertDeBuff]
50-
DROP PROCEDURE IF EXISTS [dbo].[InsertBuff]
5149
DROP PROCEDURE IF EXISTS [dbo].[IgnoredSave]
5250
DROP PROCEDURE IF EXISTS [dbo].[FoundMap]
53-
DROP PROCEDURE IF EXISTS [dbo].[DeBuffSave]
5451
DROP PROCEDURE IF EXISTS [dbo].[CheckIfPlayerSerialExists]
5552
DROP PROCEDURE IF EXISTS [dbo].[CheckIfPlayerHashExists]
5653
DROP PROCEDURE IF EXISTS [dbo].[CheckIfPlayerExists]
5754
DROP PROCEDURE IF EXISTS [dbo].[LoadItemsToCache]
58-
DROP PROCEDURE IF EXISTS [dbo].[BuffSave]
5955
DROP PROCEDURE IF EXISTS [dbo].[AddLegendMark]
6056
DROP PROCEDURE IF EXISTS [dbo].[ItemUpsert]
6157
DROP PROCEDURE IF EXISTS [dbo].[ItemMassDelete]
@@ -198,15 +194,13 @@ CREATE TABLE PlayersDiscoveredMaps
198194

199195
CREATE TABLE PlayersBuffs
200196
(
201-
[BuffId] INT NOT NULL PRIMARY KEY,
202197
[Serial] BIGINT FOREIGN KEY REFERENCES Players(Serial),
203198
[Name] VARCHAR(30) NULL,
204199
[TimeLeft] INT NOT NULL DEFAULT 0
205200
)
206201

207202
CREATE TABLE PlayersDebuffs
208203
(
209-
[DebuffId] INT NOT NULL PRIMARY KEY,
210204
[Serial] BIGINT FOREIGN KEY REFERENCES Players(Serial),
211205
[Name] VARCHAR(30) NULL,
212206
[TimeLeft] INT NOT NULL DEFAULT 0
@@ -747,26 +741,6 @@ BEGIN
747741
END
748742
GO
749743

750-
-- BuffSave
751-
SET ANSI_NULLS ON
752-
GO
753-
SET QUOTED_IDENTIFIER ON
754-
GO
755-
CREATE PROCEDURE [dbo].[BuffSave]
756-
@Buffs dbo.BuffType READONLY
757-
AS
758-
BEGIN
759-
SET NOCOUNT ON;
760-
761-
UPDATE target
762-
SET target.[Name] = source.[Name],
763-
target.[TimeLeft] = source.[TimeLeft]
764-
FROM [ZolianPlayers].[dbo].[PlayersBuffs] AS target
765-
INNER JOIN @Buffs AS source
766-
ON target.Serial = source.Serial AND target.Name = source.Name;
767-
END
768-
GO
769-
770744
-- LoadItemsToCache
771745
SET ANSI_NULLS ON
772746
GO
@@ -820,26 +794,6 @@ BEGIN
820794
END
821795
GO
822796

823-
-- DeBuffSave
824-
SET ANSI_NULLS ON
825-
GO
826-
SET QUOTED_IDENTIFIER ON
827-
GO
828-
CREATE PROCEDURE [dbo].[DeBuffSave]
829-
@Debuffs dbo.DebuffType READONLY
830-
AS
831-
BEGIN
832-
SET NOCOUNT ON;
833-
834-
UPDATE target
835-
SET target.[Name] = source.[Name],
836-
target.[TimeLeft] = source.[TimeLeft]
837-
FROM [ZolianPlayers].[dbo].[PlayersDebuffs] AS target
838-
INNER JOIN @Debuffs AS source
839-
ON target.Serial = source.Serial AND target.Name = source.Name;
840-
END
841-
GO
842-
843797
-- FoundMap
844798
SET ANSI_NULLS ON
845799
GO
@@ -870,36 +824,6 @@ BEGIN
870824
END
871825
GO
872826

873-
-- InsertBuff
874-
SET ANSI_NULLS ON
875-
GO
876-
SET QUOTED_IDENTIFIER ON
877-
GO
878-
CREATE PROCEDURE [dbo].[InsertBuff]
879-
@BuffId INT, @Serial BIGINT, @Name VARCHAR(30), @TimeLeft INT
880-
AS
881-
BEGIN
882-
SET NOCOUNT ON;
883-
INSERT INTO [ZolianPlayers].[dbo].[PlayersBuffs] ([BuffId], [Serial], [Name], [TimeLeft])
884-
VALUES (@BuffId, @Serial, @Name, @TimeLeft);
885-
END
886-
GO
887-
888-
-- InsertDeBuff
889-
SET ANSI_NULLS ON
890-
GO
891-
SET QUOTED_IDENTIFIER ON
892-
GO
893-
CREATE PROCEDURE [dbo].[InsertDeBuff]
894-
@DebuffId INT, @Serial BIGINT, @Name VARCHAR(30), @TimeLeft INT
895-
AS
896-
BEGIN
897-
SET NOCOUNT ON;
898-
INSERT INTO [ZolianPlayers].[dbo].[PlayersDeBuffs] ([DebuffId], [Serial], [Name], [TimeLeft])
899-
VALUES (@DebuffId, @Serial, @Name, @TimeLeft);
900-
END
901-
GO
902-
903827
-- InsertQuests
904828
SET ANSI_NULLS ON
905829
GO
@@ -1343,55 +1267,109 @@ BEGIN
13431267
END
13441268
GO
13451269

1346-
-- SelectBuffs
1270+
-- PlayerBuffSync
13471271
SET ANSI_NULLS ON
13481272
GO
13491273
SET QUOTED_IDENTIFIER ON
13501274
GO
1351-
CREATE PROCEDURE [dbo].[SelectBuffs] @Serial BIGINT
1275+
CREATE OR ALTER PROCEDURE dbo.PlayerBuffSync
1276+
(
1277+
@Serial BIGINT,
1278+
@Buffs dbo.BuffType READONLY
1279+
)
13521280
AS
13531281
BEGIN
1354-
SET NOCOUNT ON;
1355-
SELECT * FROM ZolianPlayers.dbo.PlayersBuffs WHERE Serial = @Serial
1356-
END
1282+
SET NOCOUNT ON;
1283+
SET XACT_ABORT ON;
1284+
1285+
;WITH SourceRows AS
1286+
(
1287+
SELECT
1288+
@Serial AS Serial,
1289+
b.[Name],
1290+
b.TimeLeft
1291+
FROM @Buffs b
1292+
WHERE b.Serial = @Serial
1293+
)
1294+
MERGE dbo.PlayersBuffs WITH (HOLDLOCK) AS t
1295+
USING SourceRows AS s
1296+
ON t.Serial = s.Serial
1297+
AND t.[Name] = s.[Name]
1298+
WHEN MATCHED THEN
1299+
UPDATE SET
1300+
t.TimeLeft = s.TimeLeft
1301+
WHEN NOT MATCHED BY TARGET THEN
1302+
INSERT (Serial, [Name], TimeLeft)
1303+
VALUES (s.Serial, s.[Name], s.TimeLeft)
1304+
WHEN NOT MATCHED BY SOURCE
1305+
AND t.Serial = @Serial
1306+
THEN DELETE;
1307+
END;
13571308
GO
13581309

1359-
-- SelectBuffsCheck
1310+
-- PlayerDeBuffSync
13601311
SET ANSI_NULLS ON
13611312
GO
13621313
SET QUOTED_IDENTIFIER ON
13631314
GO
1364-
CREATE PROCEDURE [dbo].[SelectBuffsCheck] @Serial BIGINT, @Name VARCHAR(30)
1315+
CREATE OR ALTER PROCEDURE dbo.PlayerDeBuffSync
1316+
(
1317+
@Serial BIGINT,
1318+
@Debuffs dbo.DebuffType READONLY
1319+
)
13651320
AS
13661321
BEGIN
1367-
SET NOCOUNT ON;
1368-
SELECT * FROM ZolianPlayers.dbo.PlayersBuffs WHERE Serial = @Serial AND Name = @Name
1369-
END
1322+
SET NOCOUNT ON;
1323+
SET XACT_ABORT ON;
1324+
1325+
;WITH SourceRows AS
1326+
(
1327+
SELECT
1328+
@Serial AS Serial,
1329+
b.[Name],
1330+
b.TimeLeft
1331+
FROM @Debuffs b
1332+
WHERE b.Serial = @Serial
1333+
)
1334+
MERGE dbo.PlayersDebuffs WITH (HOLDLOCK) AS t
1335+
USING SourceRows AS s
1336+
ON t.Serial = s.Serial
1337+
AND t.[Name] = s.[Name]
1338+
WHEN MATCHED THEN
1339+
UPDATE SET
1340+
t.TimeLeft = s.TimeLeft
1341+
WHEN NOT MATCHED BY TARGET THEN
1342+
INSERT (Serial, [Name], TimeLeft)
1343+
VALUES (s.Serial, s.[Name], s.TimeLeft)
1344+
WHEN NOT MATCHED BY SOURCE
1345+
AND t.Serial = @Serial
1346+
THEN DELETE;
1347+
END;
13701348
GO
13711349

1372-
-- SelectDeBuffs
1350+
-- SelectBuffs
13731351
SET ANSI_NULLS ON
13741352
GO
13751353
SET QUOTED_IDENTIFIER ON
13761354
GO
1377-
CREATE PROCEDURE [dbo].[SelectDeBuffs] @Serial BIGINT
1355+
CREATE PROCEDURE [dbo].[SelectBuffs] @Serial BIGINT
13781356
AS
13791357
BEGIN
13801358
SET NOCOUNT ON;
1381-
SELECT * FROM ZolianPlayers.dbo.PlayersDebuffs WHERE Serial = @Serial
1359+
SELECT * FROM ZolianPlayers.dbo.PlayersBuffs WHERE Serial = @Serial
13821360
END
13831361
GO
13841362

1385-
-- SelectDeBuffsCheck
1363+
-- SelectDeBuffs
13861364
SET ANSI_NULLS ON
13871365
GO
13881366
SET QUOTED_IDENTIFIER ON
13891367
GO
1390-
CREATE PROCEDURE [dbo].[SelectDeBuffsCheck] @Serial BIGINT, @Name VARCHAR(30)
1368+
CREATE PROCEDURE [dbo].[SelectDeBuffs] @Serial BIGINT
13911369
AS
13921370
BEGIN
13931371
SET NOCOUNT ON;
1394-
SELECT * FROM ZolianPlayers.dbo.PlayersDebuffs WHERE Serial = @Serial AND Name = @Name
1372+
SELECT * FROM ZolianPlayers.dbo.PlayersDebuffs WHERE Serial = @Serial
13951373
END
13961374
GO
13971375

Zolian.Server.Base/Database/AislingStorage.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -398,8 +398,8 @@ private async Task PlayerSaveRoutine(Aisling player, CancellationToken ct)
398398
await ExecTvpAsync(conn, tx, "ItemUpsert", "@Items", "dbo.ItemType", iDt, ct).ConfigureAwait(false);
399399
await ExecTvpAsync(conn, tx, "PlayerSaveSkills", "@Skills", "dbo.SkillType", skillDt, ct).ConfigureAwait(false);
400400
await ExecTvpAsync(conn, tx, "PlayerSaveSpells", "@Spells", "dbo.SpellType", spellDt, ct).ConfigureAwait(false);
401-
await ExecTvpAsync(conn, tx, "BuffSave", "@Buffs", "dbo.BuffType", buffDt, ct).ConfigureAwait(false);
402-
await ExecTvpAsync(conn, tx, "DeBuffSave", "@Debuffs", "dbo.DebuffType", debuffDt, ct).ConfigureAwait(false);
401+
await ExecTvpAsync(conn, tx, "PlayerBuffSync", "@Buffs", "dbo.BuffType", buffDt, ct, new SqlParameter("@Serial", SqlDbType.BigInt) { Value = (long)player.Serial }).ConfigureAwait(false);
402+
await ExecTvpAsync(conn, tx, "PlayerDeBuffSync", "@Debuffs", "dbo.DebuffType", debuffDt, ct, new SqlParameter("@Serial", SqlDbType.BigInt) { Value = (long)player.Serial }).ConfigureAwait(false);
403403

404404
await tx.CommitAsync(ct).ConfigureAwait(false);
405405

@@ -414,15 +414,26 @@ private async Task PlayerSaveRoutine(Aisling player, CancellationToken ct)
414414
}
415415
}
416416

417-
private static async Task ExecTvpAsync(SqlConnection conn, SqlTransaction tx, string procName,
418-
string paramName, string typeName, DataTable tvp, CancellationToken ct)
417+
private static async Task ExecTvpAsync(SqlConnection conn, SqlTransaction tx, string procName, string tvpParamName,
418+
string typeName, DataTable tvp, CancellationToken ct, params SqlParameter[] extraParams)
419419
{
420420
await using var cmd = new SqlCommand(procName, conn, tx)
421421
{
422422
CommandType = CommandType.StoredProcedure
423423
};
424424

425-
var p = cmd.Parameters.AddWithValue(paramName, tvp);
425+
// Add scalar/extra parameters first
426+
if (extraParams is { Length: > 0 })
427+
{
428+
foreach (var ep in extraParams)
429+
{
430+
if (ep is null) continue;
431+
cmd.Parameters.Add(ep);
432+
}
433+
}
434+
435+
// Add TVP parameter
436+
var p = cmd.Parameters.AddWithValue(tvpParamName, tvp);
426437
p.SqlDbType = SqlDbType.Structured;
427438
p.TypeName = typeName;
428439

0 commit comments

Comments
 (0)