Merge pull request #14925 from ywk253100/210520_perf

Improve the performance of artifact related APIs
This commit is contained in:
Daniel Jiang 2021-05-20 14:10:45 +08:00 committed by GitHub
commit 8600b06975
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 18 deletions

View File

@ -1,2 +1,7 @@
ALTER TABLE replication_policy ADD COLUMN IF NOT EXISTS dest_namespace_replace_count int; ALTER TABLE replication_policy ADD COLUMN IF NOT EXISTS dest_namespace_replace_count int;
UPDATE replication_policy SET dest_namespace_replace_count=-1 WHERE dest_namespace IS NULL; UPDATE replication_policy SET dest_namespace_replace_count=-1 WHERE dest_namespace IS NULL;
CREATE INDEX IF NOT EXISTS idx_artifact_push_time ON artifact (push_time);
CREATE INDEX IF NOT EXISTS idx_tag_push_time ON tag (push_time);
CREATE INDEX IF NOT EXISTS idx_tag_artifact_id ON tag (artifact_id);
CREATE INDEX IF NOT EXISTS idx_artifact_reference_child_id ON artifact_reference (child_id);

View File

@ -53,22 +53,22 @@ type DAO interface {
} }
const ( const (
// both tagged and untagged artifacts // the QuerySetter of beego doesn't support "EXISTS" directly, use qs.FilterRaw("id", "=id AND xxx") to workaround the limitation
both = `IN ( // base filter: both tagged and untagged artifacts
SELECT DISTINCT art.id FROM artifact art both = `=id AND (
LEFT JOIN tag ON art.id=tag.artifact_id EXISTS (SELECT 1 FROM tag WHERE tag.artifact_id = T0.id)
LEFT JOIN artifact_reference ref ON art.id=ref.child_id OR
WHERE tag.id IS NOT NULL OR ref.id IS NULL)` NOT EXISTS (SELECT 1 FROM artifact_reference ref WHERE ref.child_id = T0.id)
// only untagged artifacts )`
untagged = `IN ( // tag filter: only untagged artifacts
SELECT DISTINCT art.id FROM artifact art // the "untagged" filter is based on "base" filter, so we consider the tag only
LEFT JOIN tag ON art.id=tag.artifact_id untagged = `=id AND NOT EXISTS(
WHERE tag.id IS NULL)` SELECT 1 FROM tag WHERE tag.artifact_id = T0.id
// only tagged artifacts )`
tagged = `IN ( // tag filter: only tagged artifacts
SELECT DISTINCT art.id FROM artifact art tagged = `=id AND EXISTS (
JOIN tag ON art.id=tag.artifact_id SELECT 1 FROM tag WHERE tag.artifact_id = T0.id
WHERE tag.id IS NOT NULL)` )`
) )
// New returns an instance of the default DAO // New returns an instance of the default DAO
@ -273,7 +273,7 @@ func querySetter(ctx context.Context, query *q.Query) (beegoorm.QuerySeter, erro
// handle q=base=* // handle q=base=*
// when "q=base=*" is specified in the query, the base collection is the all artifacts of database, // when "q=base=*" is specified in the query, the base collection is the all artifacts of database,
// otherwise the base connection is only the tagged artifacts and untagged artifacts that aren't // otherwise the base collection is only the tagged artifacts and untagged artifacts that aren't
// referenced by others // referenced by others
func setBaseQuery(qs beegoorm.QuerySeter, query *q.Query) (beegoorm.QuerySeter, error) { func setBaseQuery(qs beegoorm.QuerySeter, query *q.Query) (beegoorm.QuerySeter, error) {
if query == nil || len(query.Keywords) == 0 { if query == nil || len(query.Keywords) == 0 {