Skip to content

Commit d7f6a6f

Browse files
authored
Merge pull request #1835 from seltonmt012/perf/multichar-delete-indexes
perf(esx_multicharacter): index the columns used by character deletion
2 parents 46b9ecf + d3aaab0 commit d7f6a6f

2 files changed

Lines changed: 67 additions & 7 deletions

File tree

[SQL]/legacy.sql

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,8 @@ ALTER TABLE `addon_account`
750750
ALTER TABLE `addon_account_data`
751751
ADD PRIMARY KEY (`id`),
752752
ADD UNIQUE KEY `index_addon_account_data_account_name_owner` (`account_name`,`owner`),
753-
ADD KEY `index_addon_account_data_account_name` (`account_name`);
753+
ADD KEY `index_addon_account_data_account_name` (`account_name`),
754+
ADD KEY `esx_addon_account_data_owner` (`owner`);
754755

755756
--
756757
-- Indexes for table `addon_inventory`
@@ -765,13 +766,15 @@ ALTER TABLE `addon_inventory_items`
765766
ADD PRIMARY KEY (`id`),
766767
ADD KEY `index_addon_inventory_items_inventory_name_name` (`inventory_name`,`name`),
767768
ADD KEY `index_addon_inventory_items_inventory_name_name_owner` (`inventory_name`,`name`,`owner`),
768-
ADD KEY `index_addon_inventory_inventory_name` (`inventory_name`);
769+
ADD KEY `index_addon_inventory_inventory_name` (`inventory_name`),
770+
ADD KEY `esx_addon_inventory_items_owner` (`owner`);
769771

770772
--
771773
-- Indexes for table `billing`
772774
--
773775
ALTER TABLE `billing`
774-
ADD PRIMARY KEY (`id`);
776+
ADD PRIMARY KEY (`id`),
777+
ADD KEY `esx_billing_identifier` (`identifier`);
775778

776779
--
777780
-- Indexes for table `cardealer_vehicles`
@@ -791,7 +794,8 @@ ALTER TABLE `datastore`
791794
ALTER TABLE `datastore_data`
792795
ADD PRIMARY KEY (`id`),
793796
ADD UNIQUE KEY `index_datastore_data_name_owner` (`name`,`owner`),
794-
ADD KEY `index_datastore_data_name` (`name`);
797+
ADD KEY `index_datastore_data_name` (`name`),
798+
ADD KEY `esx_datastore_data_owner` (`owner`);
795799

796800
--
797801
-- Indexes for table `items`
@@ -822,7 +826,8 @@ ALTER TABLE `licenses`
822826
-- Indexes for table `owned_vehicles`
823827
--
824828
ALTER TABLE `owned_vehicles`
825-
ADD PRIMARY KEY (`plate`);
829+
ADD PRIMARY KEY (`plate`),
830+
ADD KEY `esx_owned_vehicles_owner` (`owner`);
826831

827832
--
828833
--
@@ -841,7 +846,8 @@ ALTER TABLE `rented_vehicles`
841846
-- Indexes for table `society_moneywash`
842847
--
843848
ALTER TABLE `society_moneywash`
844-
ADD PRIMARY KEY (`id`);
849+
ADD PRIMARY KEY (`id`),
850+
ADD KEY `esx_society_moneywash_identifier` (`identifier`);
845851

846852
--
847853
-- Indexes for table `users`
@@ -857,7 +863,8 @@ ALTER TABLE `users`
857863
-- Indexes for table `user_licenses`
858864
--
859865
ALTER TABLE `user_licenses`
860-
ADD PRIMARY KEY (`id`);
866+
ADD PRIMARY KEY (`id`),
867+
ADD KEY `esx_user_licenses_owner` (`owner`);
861868

862869
--
863870
-- Indexes for table `vehicle_categories`
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
local esxVersion = "v1.14.2"
2+
3+
Core.Migrations = Core.Migrations or {}
4+
Core.Migrations[esxVersion] = Core.Migrations[esxVersion] or {}
5+
6+
if GetResourceKvpInt(("esx_migration:%s"):format(esxVersion)) == 1 then
7+
return
8+
end
9+
10+
local targets = {
11+
{ table = "billing", column = "identifier" },
12+
{ table = "owned_vehicles", column = "owner" },
13+
{ table = "user_licenses", column = "owner" },
14+
{ table = "society_moneywash", column = "identifier" },
15+
{ table = "addon_account_data", column = "owner" },
16+
{ table = "addon_inventory_items", column = "owner" },
17+
{ table = "datastore_data", column = "owner" },
18+
}
19+
20+
---@return boolean restartRequired
21+
Core.Migrations[esxVersion].multicharDeleteIndexes = function()
22+
print("^4[esx_migration:v1.14.2:multicharDeleteIndexes]^7 Indexing character deletion columns.")
23+
24+
for i = 1, #targets do
25+
local target = targets[i]
26+
27+
local tableExists = MySQL.scalar.await([[
28+
SELECT COUNT(*)
29+
FROM INFORMATION_SCHEMA.TABLES
30+
WHERE TABLE_SCHEMA = DATABASE()
31+
AND TABLE_NAME = ?
32+
]], { target.table })
33+
34+
if tableExists ~= 0 then
35+
local leadingIndex = MySQL.scalar.await([[
36+
SELECT COUNT(*)
37+
FROM INFORMATION_SCHEMA.STATISTICS
38+
WHERE TABLE_SCHEMA = DATABASE()
39+
AND TABLE_NAME = ?
40+
AND COLUMN_NAME = ?
41+
AND SEQ_IN_INDEX = 1
42+
]], { target.table, target.column })
43+
44+
if leadingIndex == 0 then
45+
MySQL.update.await(("CREATE INDEX `esx_%s_%s` ON `%s` (`%s`)"):format(target.table, target.column, target.table, target.column))
46+
print(("^4[esx_migration:v1.14.2:multicharDeleteIndexes]^7 Indexed ^5%s.%s^7."):format(target.table, target.column))
47+
end
48+
end
49+
end
50+
51+
print("^4[esx_migration:v1.14.2:multicharDeleteIndexes]^7 Migration complete.")
52+
return false
53+
end

0 commit comments

Comments
 (0)