Skip to content
This repository has been archived by the owner on Jan 2, 2025. It is now read-only.

Commit

Permalink
refactor(backend): optimize database indices and queries for blob met…
Browse files Browse the repository at this point in the history
…adata
  • Loading branch information
burdiyan committed Mar 20, 2024
1 parent d2dd71f commit c8e6bc7
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 155 deletions.
2 changes: 1 addition & 1 deletion backend/daemon/api/entities/v1alpha/entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ var qGetEntityTimeline = dqb.Str(`
public_keys.principal AS author,
group_concat(change_deps.parent, ' ') AS deps
FROM structural_blobs
JOIN blobs ON blobs.id = structural_blobs.id
JOIN blobs INDEXED BY blobs_metadata ON blobs.id = structural_blobs.id
JOIN public_keys ON public_keys.id = structural_blobs.author
LEFT JOIN change_deps ON change_deps.child = structural_blobs.id
LEFT JOIN drafts ON (drafts.resource, drafts.blob) = (structural_blobs.resource, structural_blobs.id)
Expand Down
2 changes: 1 addition & 1 deletion backend/daemon/api/groups/v1alpha/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ var QCollectBlobs = dqb.Str(`
blobs.codec,
blobs.multihash
FROM selected
JOIN blobs ON blobs.id = selected.id AND selected.kind = 1
JOIN blobs INDEXED BY blobs_metadata ON blobs.id = selected.id AND selected.kind = 1
ORDER BY blobs.id;
`)

Expand Down
2 changes: 1 addition & 1 deletion backend/daemon/api/groups/v1alpha/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ var qListDocumentGroups = dqb.Str(`
structural_blobs.ts AS ts
FROM resource_links
JOIN structural_blobs ON structural_blobs.id = resource_links.source
JOIN blobs ON blobs.id = structural_blobs.id
JOIN blobs INDEXED BY blobs_metadata ON blobs.id = structural_blobs.id
JOIN resources ON resources.id = structural_blobs.resource
WHERE resource_links.type = 'group/content'
AND resource_links.target = :document
Expand Down
28 changes: 28 additions & 0 deletions backend/daemon/storage/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,34 @@ var migrations = []migration{
DELETE FROM kv WHERE key = 'last_reindex_time';
`))
}},
{Version: "2024-03-19.01", Run: func(_ *Dir, conn *sqlite.Conn) error {
return sqlitex.ExecScript(conn, sqlfmt(`
DROP VIEW IF EXISTS key_delegations_view;
CREATE VIEW IF NOT EXISTS key_delegations_view AS
SELECT
kd.id AS blob,
blobs.codec AS blob_codec,
blobs.multihash AS blob_multihash,
iss.principal AS issuer,
del.principal AS delegate
FROM key_delegations kd
JOIN blobs INDEXED BY blobs_metadata ON blobs.id = kd.id
JOIN public_keys iss ON iss.id = kd.issuer
JOIN public_keys del ON del.id = kd.delegate;
DROP VIEW IF EXISTS drafts_view;
CREATE VIEW IF NOT EXISTS drafts_view AS
SELECT
drafts.resource AS resource_id,
drafts.blob AS blob_id,
resources.iri AS resource,
blobs.codec AS codec,
blobs.multihash AS multihash
FROM drafts
JOIN resources ON resources.id = drafts.resource
JOIN blobs INDEXED BY blobs_metadata ON blobs.id = drafts.blob;
`))
}},
}

const (
Expand Down
2 changes: 1 addition & 1 deletion backend/daemon/storage/schema.gensum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
srcs: 73de8491dac69809249c20522cd0a451
srcs: c0bb41a9dd9db2130a02aa83b977269f
outs: 0efc08608cead13c5b8756e4beee2ecd
4 changes: 2 additions & 2 deletions backend/daemon/storage/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ SELECT
iss.principal AS issuer,
del.principal AS delegate
FROM key_delegations kd
JOIN blobs ON blobs.id = kd.id
JOIN blobs INDEXED BY blobs_metadata ON blobs.id = kd.id
JOIN public_keys iss ON iss.id = kd.issuer
JOIN public_keys del ON del.id = kd.delegate;

Expand Down Expand Up @@ -176,7 +176,7 @@ SELECT
blobs.multihash AS multihash
FROM drafts
JOIN resources ON resources.id = drafts.resource
JOIN blobs ON blobs.id = drafts.blob;
JOIN blobs INDEXED BY blobs_metadata ON blobs.id = drafts.blob;

-- View for dependency links between changes.
CREATE VIEW change_deps AS
Expand Down
102 changes: 6 additions & 96 deletions backend/hyper/hypersql/queries.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions backend/hyper/hypersql/queries.gensum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
srcs: 800af070fac742f44f90f3142c9ffcdd
outs: ed8e2acc2115513302d723aab1d251e1
srcs: 2e1bd3b9f4e7baaf51f43553662842dc
outs: 63faeb2bdfe750e843383133579a0550
44 changes: 6 additions & 38 deletions backend/hyper/hypersql/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func generateQueries() error {
code, err := sgen.CodegenQueries("hypersql",
qb.MakeQuery(s.Schema, "BlobsHave", sgen.QueryKindSingle,
"SELECT", qb.Results(qb.ResultExpr("1", "have", sgen.TypeInt)), '\n',
"FROM", s.Blobs, '\n',
"FROM", s.Blobs, "INDEXED BY blobs_metadata_by_hash", '\n',
"WHERE", s.BlobsMultihash, "=", qb.VarCol(s.BlobsMultihash), '\n',
"AND", s.BlobsSize, ">=", "0",
),
Expand All @@ -35,7 +35,7 @@ func generateQueries() error {
s.BlobsID,
s.BlobsSize,
), '\n',
"FROM", s.Blobs, '\n',
"FROM", s.Blobs, "INDEXED BY blobs_metadata_by_hash", '\n',
"WHERE", s.BlobsMultihash, "=", qb.VarCol(s.BlobsMultihash),
),
qb.MakeQuery(s.Schema, "BlobsInsert", sgen.QueryKindSingle,
Expand Down Expand Up @@ -66,7 +66,7 @@ func generateQueries() error {
s.BlobsMultihash,
s.BlobsCodec,
), '\n',
"FROM", s.Blobs, '\n',
"FROM", s.Blobs, "INDEXED BY blobs_metadata", '\n',
"LEFT JOIN", s.Drafts, "ON", s.DraftsBlob, "=", s.BlobsID, '\n',
"WHERE", s.BlobsSize, ">=", "0", '\n',
"AND", s.DraftsBlob, "IS NULL", '\n',
Expand Down Expand Up @@ -173,7 +173,7 @@ func generateQueries() error {
s.KeyDelegationsIssuer,
), '\n',
"FROM", s.KeyDelegations, '\n',
"JOIN", s.Blobs, "ON", s.BlobsID, "=", s.KeyDelegationsID, '\n',
"JOIN", s.Blobs, "INDEXED BY blobs_metadata_by_hash ON", s.BlobsID, "=", s.KeyDelegationsID, '\n',
"WHERE", s.BlobsMultihash, "=", qb.VarCol(s.BlobsMultihash), '\n',
"LIMIT 1",
),
Expand Down Expand Up @@ -242,23 +242,6 @@ func generateQueries() error {
"WHERE", s.ResourcesIRI, "=", qb.VarCol(s.ResourcesIRI), '\n',
"ORDER BY", s.StructuralBlobsTs,
),
qb.MakeQuery(s.Schema, "ChangesListPublicNoData", sgen.QueryKindMany,
"SELECT", qb.Results(
s.StructuralBlobsViewBlobID,
s.StructuralBlobsViewCodec,
s.StructuralBlobsViewResourceID,
s.StructuralBlobsViewTs,
s.StructuralBlobsViewMultihash,
s.StructuralBlobsViewSize,
s.StructuralBlobsViewResource,
s.DraftsBlob,
), '\n',
"FROM", s.StructuralBlobsView, '\n',
"LEFT JOIN", s.Drafts, "ON", s.DraftsResource, "=", s.StructuralBlobsViewResourceID, '\n',
"WHERE", s.DraftsBlob, "IS NULL", '\n',
"AND", s.StructuralBlobsViewBlobType, "= 'Change'",
"ORDER BY", qb.Enumeration(s.StructuralBlobsViewResource, s.StructuralBlobsViewTs),
),
qb.MakeQuery(s.Schema, "ChangesResolveHeads", sgen.QueryKindSingle,
"WITH RECURSIVE changeset (change) AS", qb.SubQuery(
"SELECT value",
Expand Down Expand Up @@ -328,24 +311,9 @@ WHERE blob NOT IN deps`,
s.BlobsMultihash,
), '\n',
"FROM", s.ChangeDeps, '\n',
"JOIN", s.Blobs, "ON", s.BlobsID, "=", s.ChangeDepsParent, '\n',
"JOIN", s.Blobs, "INDEXED BY blobs_metadata ON", s.BlobsID, "=", s.ChangeDepsParent, '\n',
"WHERE", s.ChangeDepsChild, "=", qb.VarCol(s.ChangeDepsChild),
),
qb.MakeQuery(s.Schema, "ChangesInfoForEntity", sgen.QueryKindMany,
"SELECT", qb.Results(
s.BlobsCodec,
s.BlobsMultihash,
s.StructuralBlobsID,
s.StructuralBlobsTs,
s.PublicKeysPrincipal,
qb.ResultExpr(s.C_TrustedAccountsID+" > 0", "is_trusted", sgen.TypeInt),
), '\n',
"FROM", s.StructuralBlobs, '\n',
"JOIN", s.Blobs, "ON", s.BlobsID, "=", s.StructuralBlobsID, '\n',
"JOIN", s.PublicKeys, "ON", s.PublicKeysID, "=", s.StructuralBlobsAuthor, '\n',
"LEFT JOIN", s.TrustedAccounts, "ON", s.TrustedAccountsID, "=", s.StructuralBlobsAuthor, '\n',
"WHERE", s.StructuralBlobsResource, "=", qb.VarCol(s.StructuralBlobsResource),
),

qb.MakeQuery(s.Schema, "BacklinksForDocument", sgen.QueryKindMany,
"SELECT", qb.Results(
Expand All @@ -361,7 +329,7 @@ WHERE blob NOT IN deps`,
"FROM", s.ResourceLinks, '\n',
"JOIN", s.StructuralBlobs, "ON", s.StructuralBlobsID, "=", s.ResourceLinksSource, '\n',
"JOIN", s.Resources, "ON", s.ResourcesID, "=", s.StructuralBlobsResource, '\n',
"JOIN", s.Blobs, "ON", s.BlobsID, "=", s.StructuralBlobsID, '\n',
"JOIN", s.Blobs, "INDEXED BY blobs_metadata ON", s.BlobsID, "=", s.StructuralBlobsID, '\n',
"WHERE", s.ResourceLinksType, "GLOB 'doc/*'", '\n',
"AND", s.ResourceLinksTarget, "=", qb.VarCol(s.ResourceLinksTarget),
),
Expand Down
2 changes: 1 addition & 1 deletion backend/mttnet/list_blobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ var qListBlobs = dqb.Str(`
blobs.id,
blobs.codec,
blobs.multihash
FROM blobs
FROM blobs INDEXED BY blobs_metadata
LEFT OUTER JOIN drafts ON drafts.blob = blobs.id
WHERE blobs.size >= 0
AND drafts.blob IS NULL
Expand Down
11 changes: 0 additions & 11 deletions backend/mttnet/providing.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"mintter/backend/hyper"
"mintter/backend/hyper/hypersql"
"mintter/backend/logging"
"mintter/backend/pkg/dqb"
"time"

"crawshaw.io/sqlite/sqlitex"
Expand All @@ -15,16 +14,6 @@ import (
"go.uber.org/zap"
)

var qAllPublicBlobs = dqb.Str(`
SELECT
blobs.codec,
blobs.multihash
FROM blobs
LEFT OUTER JOIN drafts ON drafts.blob = blobs.id
WHERE blobs.size >= 0
AND drafts.blob IS NULL;
`)

var randSrc = rand.NewSource(time.Now().UnixNano())

func makeProvidingStrategy(db *sqlitex.Pool, logLevel string) provider.KeyChanFunc {
Expand Down
Loading

0 comments on commit c8e6bc7

Please sign in to comment.