25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-24 16:23:40 +01:00

PubSub: Use integer type for timestamps (SQL)

Store PubSub item creation/modification timestamps as integers instead
of "$megasec:$sec:$microsec" strings.  This can improve the performance
of certain SQL queries significantly.

Thanks to Ammonit Measurement GmbH for sponsoring this work.
This commit is contained in:
Holger Weiss 2021-08-24 07:07:44 +02:00
parent ebf03a3745
commit 79ab6bb47c
9 changed files with 36 additions and 34 deletions

View File

@ -262,8 +262,8 @@ CREATE TABLE pubsub_item (
nodeid bigint REFERENCES pubsub_node(nodeid) ON DELETE CASCADE, nodeid bigint REFERENCES pubsub_node(nodeid) ON DELETE CASCADE,
itemid text NOT NULL, itemid text NOT NULL,
publisher text NOT NULL, publisher text NOT NULL,
creation varchar(32) NOT NULL, creation BIGINT UNSIGNED NOT NULL,
modification varchar(32) NOT NULL, modification BIGINT UNSIGNED NOT NULL,
payload text NOT NULL DEFAULT '' payload text NOT NULL DEFAULT ''
); );
CREATE INDEX i_pubsub_item_itemid ON pubsub_item (itemid); CREATE INDEX i_pubsub_item_itemid ON pubsub_item (itemid);

View File

@ -238,8 +238,8 @@ CREATE TABLE pubsub_item (
nodeid bigint REFERENCES pubsub_node(nodeid) ON DELETE CASCADE, nodeid bigint REFERENCES pubsub_node(nodeid) ON DELETE CASCADE,
itemid text NOT NULL, itemid text NOT NULL,
publisher text NOT NULL, publisher text NOT NULL,
creation varchar(32) NOT NULL, creation BIGINT UNSIGNED NOT NULL,
modification varchar(32) NOT NULL, modification BIGINT UNSIGNED NOT NULL,
payload text NOT NULL DEFAULT '' payload text NOT NULL DEFAULT ''
); );
CREATE INDEX i_pubsub_item_itemid ON pubsub_item (itemid); CREATE INDEX i_pubsub_item_itemid ON pubsub_item (itemid);

View File

@ -210,8 +210,8 @@ CREATE TABLE [dbo].[pubsub_item] (
[nodeid] [bigint] NULL, [nodeid] [bigint] NULL,
[itemid] [varchar] (255) NOT NULL, [itemid] [varchar] (255) NOT NULL,
[publisher] [varchar] (250) NOT NULL, [publisher] [varchar] (250) NOT NULL,
[creation] [varchar] (32) NOT NULL, [creation] [bigint] NOT NULL,
[modification] [varchar] (32) NOT NULL, [modification] [bigint] NOT NULL,
[payload] [text] NOT NULL DEFAULT '' [payload] [text] NOT NULL DEFAULT ''
) TEXTIMAGE_ON [PRIMARY]; ) TEXTIMAGE_ON [PRIMARY];

View File

@ -277,8 +277,8 @@ CREATE TABLE pubsub_item (
nodeid bigint, nodeid bigint,
itemid text NOT NULL, itemid text NOT NULL,
publisher text NOT NULL, publisher text NOT NULL,
creation varchar(32) NOT NULL, creation BIGINT UNSIGNED NOT NULL,
modification varchar(32) NOT NULL, modification BIGINT UNSIGNED NOT NULL,
payload mediumtext NOT NULL payload mediumtext NOT NULL
) ENGINE=InnoDB CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; ) ENGINE=InnoDB CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE INDEX i_pubsub_item_itemid ON pubsub_item(itemid(36)); CREATE INDEX i_pubsub_item_itemid ON pubsub_item(itemid(36));

View File

@ -253,8 +253,8 @@ CREATE TABLE pubsub_item (
nodeid bigint, nodeid bigint,
itemid text NOT NULL, itemid text NOT NULL,
publisher text NOT NULL, publisher text NOT NULL,
creation varchar(32) NOT NULL, creation BIGINT UNSIGNED NOT NULL,
modification varchar(32) NOT NULL, modification BIGINT UNSIGNED NOT NULL,
payload mediumtext NOT NULL payload mediumtext NOT NULL
) ENGINE=InnoDB CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; ) ENGINE=InnoDB CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE INDEX i_pubsub_item_itemid ON pubsub_item(itemid(36)); CREATE INDEX i_pubsub_item_itemid ON pubsub_item(itemid(36));

View File

@ -427,8 +427,8 @@ CREATE TABLE pubsub_item (
nodeid bigint REFERENCES pubsub_node(nodeid) ON DELETE CASCADE, nodeid bigint REFERENCES pubsub_node(nodeid) ON DELETE CASCADE,
itemid text NOT NULL, itemid text NOT NULL,
publisher text NOT NULL, publisher text NOT NULL,
creation varchar(32) NOT NULL, creation BIGINT NOT NULL,
modification varchar(32) NOT NULL, modification BIGINT NOT NULL,
payload text NOT NULL DEFAULT '' payload text NOT NULL DEFAULT ''
); );
CREATE INDEX i_pubsub_item_itemid ON pubsub_item USING btree (itemid); CREATE INDEX i_pubsub_item_itemid ON pubsub_item USING btree (itemid);

View File

@ -256,8 +256,8 @@ CREATE TABLE pubsub_item (
nodeid bigint REFERENCES pubsub_node(nodeid) ON DELETE CASCADE, nodeid bigint REFERENCES pubsub_node(nodeid) ON DELETE CASCADE,
itemid text NOT NULL, itemid text NOT NULL,
publisher text NOT NULL, publisher text NOT NULL,
creation varchar(32) NOT NULL, creation BIGINT NOT NULL,
modification varchar(32) NOT NULL, modification BIGINT NOT NULL,
payload text NOT NULL DEFAULT '' payload text NOT NULL DEFAULT ''
); );
CREATE INDEX i_pubsub_item_itemid ON pubsub_item USING btree (itemid); CREATE INDEX i_pubsub_item_itemid ON pubsub_item USING btree (itemid);

View File

@ -820,16 +820,16 @@ set_item(Item) ->
P = encode_jid(JID), P = encode_jid(JID),
Payload = Item#pubsub_item.payload, Payload = Item#pubsub_item.payload,
XML = str:join([fxml:element_to_binary(X) || X<-Payload], <<>>), XML = str:join([fxml:element_to_binary(X) || X<-Payload], <<>>),
SM = encode_now(M), SM = misc:now_to_usec(M),
SC = encode_now(C), SC = misc:now_to_usec(C),
?SQL_UPSERT_T( ?SQL_UPSERT_T(
"pubsub_item", "pubsub_item",
["!nodeid=%(Nidx)d", ["!nodeid=%(Nidx)d",
"!itemid=%(ItemId)s", "!itemid=%(ItemId)s",
"publisher=%(P)s", "publisher=%(P)s",
"modification=%(SM)s", "modification=%(SM)d",
"payload=%(XML)s", "payload=%(XML)s",
"-creation=%(SC)s" "-creation=%(SC)d"
]), ]),
ok. ok.
@ -1063,20 +1063,27 @@ rsm_page(Count, Index, Offset, Items) ->
first = #rsm_first{index = Offset, data = First}, first = #rsm_first{index = Offset, data = First},
last = Last}. last = Last}.
%% Convert <<"2021-08-22T19:25:52.817368Z">> to <<"1629660352817368">>.
-spec encode_stamp(binary()) -> binary().
encode_stamp(Stamp) -> encode_stamp(Stamp) ->
try xmpp_util:decode_timestamp(Stamp) of try xmpp_util:decode_timestamp(Stamp) of
Now -> Now ->
encode_now(Now) encode_now(Now)
catch _:{bad_timestamp, _} -> catch _:{bad_timestamp, _} ->
Stamp % We should return a proper error to the client instead. <<"0">> % We should return a proper error to the client instead.
end. end.
%% Convert <<"1629660352817368">> to <<"2021-08-22T19:25:52.817368Z">>.
-spec decode_stamp(binary()) -> binary().
decode_stamp(Stamp) -> decode_stamp(Stamp) ->
xmpp_util:encode_timestamp(decode_now(Stamp)). xmpp_util:encode_timestamp(decode_now(Stamp)).
encode_now({T1, T2, T3}) -> %% Convert {1629, 660352, 817368} to <<"1629660352817368">>.
<<(misc:i2l(T1, 6))/binary, ":", -spec encode_now(erlang:timestamp()) -> binary().
(misc:i2l(T2, 6))/binary, ":", encode_now(Now) ->
(misc:i2l(T3, 6))/binary>>. integer_to_binary(misc:now_to_usec(Now)).
decode_now(NowStr) ->
[MS, S, US] = binary:split(NowStr, <<":">>, [global]), %% Convert <<"1629660352817368">> to {1629, 660352, 817368}.
{binary_to_integer(MS), binary_to_integer(S), binary_to_integer(US)}. -spec decode_now(binary()) -> erlang:timestamp().
decode_now(Str) ->
misc:usec_to_now(binary_to_integer(Str)).

View File

@ -184,15 +184,10 @@ export(_Server) ->
payload = Payload}) -> payload = Payload}) ->
P = jid:encode(JID), P = jid:encode(JID),
XML = str:join([fxml:element_to_binary(X) || X<-Payload], <<>>), XML = str:join([fxml:element_to_binary(X) || X<-Payload], <<>>),
SM = encode_now(M), SM = misc:now_to_usec(M),
SC = encode_now(C), SC = misc:now_to_usec(C),
[?SQL("insert into pubsub_item(itemid,nodeid,creation,modification,publisher,payload)" [?SQL("insert into pubsub_item(itemid,nodeid,creation,modification,publisher,payload)"
" values (%(ItemId)s, %(Nidx)d, %(SC)s, %(SM)s, %(P)s, %(XML)s);")]; " values (%(ItemId)s, %(Nidx)d, %(SC)d, %(SM)d, %(P)s, %(XML)s);")];
(_Host, _R) -> (_Host, _R) ->
[] []
end}]. end}].
encode_now({T1, T2, T3}) ->
<<(misc:i2l(T1, 6))/binary, ":",
(misc:i2l(T2, 6))/binary, ":",
(misc:i2l(T3, 6))/binary>>.