Commit 7aea4629 authored by Robert Lyon's avatar Robert Lyon Committed by Cecilia Vela Gurovic
Browse files

Bug 1889340: Adding new index to 'tag' table



So that queries to the table go faster

Change-Id: I8313e37dc50b923610f1d02375fe402e64939d79
Signed-off-by: default avatarRobert Lyon <robertl@catalyst.net.nz>
parent e4cabd37
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -811,7 +811,7 @@ class BlockInstance {
        }
        $this->tags = check_case_sensitive($tags, 'tag');
        delete_records('tag', 'resourcetype', 'blocktype', 'resourceid', $this->get('id'));
        foreach ($this->tags as $tag) {
        foreach (array_unique($this->tags) as $tag) {
            // truncate the tag before insert it into the database
            $tag = substr($tag, 0, 128);
            $tag = check_if_institution_tag($tag);
+29 −2
Original line number Diff line number Diff line
@@ -68,10 +68,37 @@ function edit_tag_submit(Pieform $form, $values) {
        redirect(get_config('wwwroot') . 'edittags.php?tag=' . urlencode($tag));
    }
    db_begin();
    // Check we don't end up with a clash where the tag value change matches another tag on the same object
    if ($newtags = get_records_sql_assoc("SELECT id, tag, resourcetype, resourceid
                                          FROM {tag} WHERE tag = ? AND ownertype = ? AND ownerid = ? AND resourcetype IN ('artefact', 'view', 'collection', 'blocktype')",
                                          array($values['tagname'], 'user', $userid))) {
        $newtagarray = array();
        foreach ($newtags as $nk => $nv) {
            $tagstr = $nv->tag . $nv->resourcetype . $nv->resourceid;
            $newtagarray[$tagstr] = 1;
        }

        if ($oldtags = get_records_sql_assoc("SELECT id, tag, resourcetype, resourceid
                                              FROM {tag} WHERE tag = ? AND ownertype = ? AND ownerid = ? AND resourcetype IN ('artefact', 'view', 'collection', 'blocktype')",
                                              array($tag, 'user', $userid))) {
            $deltags = array();
            foreach ($oldtags as $ok => $ov) {
                $potentialtagstr = $values['tagname'] . $ov->resourcetype . $ov->resourceid;
                if (isset($newtagarray[$potentialtagstr])) {
                    $deltags[] = $ov->id;
                }
            }
            if ($deltags) {
                execute_sql("DELETE FROM {tag} WHERE id IN (" . implode(',', $deltags) . ")");
            }
        }
    }

    execute_sql(
        "UPDATE {tag} SET tag = ? WHERE tag = ? AND ownertype = ? AND ownerid = ? AND resourcetype IN ('artefact', 'view', 'collection')",
        "UPDATE {tag} SET tag = ? WHERE tag = ? AND ownertype = ? AND ownerid = ? AND resourcetype IN ('artefact', 'view', 'collection', 'blocktype')",
        array($values['tagname'], $tag, 'user', $userid)
    );

    db_commit();
    $SESSION->add_ok_msg(get_string('tagupdatedsuccessfully'));
    redirect(get_config('wwwroot') . 'tags.php?tag=' . urlencode($values['tagname']));
@@ -84,7 +111,7 @@ function delete_tag_submit(Pieform $form, $values) {
    }
    db_begin();
    execute_sql(
        "DELETE FROM {tag} WHERE tag = ? AND ownertype = ? AND ownerid = ? AND resourcetype IN ('artefact', 'view', 'collection')",
        "DELETE FROM {tag} WHERE tag = ? AND ownertype = ? AND ownerid = ? AND resourcetype IN ('artefact', 'view', 'collection', 'blocktype')",
        array($tag, 'user', $userid)
    );
    db_commit();
+1 −1
Original line number Diff line number Diff line
@@ -214,7 +214,7 @@ class Collection {
            }
            delete_records('tag', 'resourcetype', 'collection', 'resourceid', $this->get('id'));
            $tags = check_case_sensitive($this->get_tags(), 'tag');
            foreach ($tags as $tag) {
            foreach (array_unique($tags) as $tag) {
                //truncate the tag before insert it into the database
                $tag = substr($tag, 0, 128);
                $tag = check_if_institution_tag($tag);
+1 −0
Original line number Diff line number Diff line
@@ -1437,6 +1437,7 @@
            </FIELDS>
            <KEYS>
                <KEY NAME="primary" TYPE="primary" FIELDS="id" />
                <KEY NAME="taguk" TYPE="unique" FIELDS="tag,resourcetype,resourceid" />
                <KEY NAME="editedbyfk" TYPE="foreign" FIELDS="editedby" REFTABLE="usr" REFFIELDS="id" />
            </KEYS>
        </TABLE>
+25 −0
Original line number Diff line number Diff line
@@ -1368,5 +1368,30 @@ function xmldb_core_upgrade($oldversion=0) {
        }
    }

    if ($oldversion < 2019031923) {
        log_debug('Adding unique key to tag table');
        $table = new XMLDBTable('tag');
        // Add the new unique index
        $index = new XMLDBIndex('taguk');
        $index->setAttributes(XMLDB_INDEX_UNIQUE, array('tag', 'resourcetype', 'resourceid'));
        if (!index_exists($table, $index)) {
            // make sure there are no doubleups in tags
            if ($taginfo = get_records_sql_array("SELECT tag, resourcetype, resourceid, ownertype, ownerid
                                                   FROM {tag}
                                                   GROUP BY tag, resourcetype, resourceid, ownertype, ownerid
                                                   HAVING COUNT(*) > 1")) {
                // we have duplicates so we need to delete all but the first one
                foreach ($taginfo as $tag) {
                    $ids = get_column_sql("SELECT t.id FROM {tag} t WHERE t.tag = ? AND t.resourcetype = ?
                                           AND t.resourceid = ? AND t.ownertype = ? AND t.ownerid = ?",
                                          array($tag->tag, $tag->resourcetype, $tag->resourceid, $tag->ownertype, $tag->ownerid));
                    array_shift($ids);
                    execute_sql("DELETE FROM {tag} WHERE id IN (" . implode(', ', $ids) . ")");
                }
            }
            add_index($table, $index);
        }
    }

    return $status;
}
Loading