Hsqldb 重命名具有相同父项的项名称的重复项

Hsqldb rename duplicates of item names with same parent

我写了一个 LibreOffice / OpenOffice 扩展,允许访问这些 Google Drive 文件。

为此,我使用了 Hsqldb 2.4 数据库,script file is accessible at: https://github.com/prrvchr/gDriveOOo/blob/master/gDriveOOo/hsqldb/vnd.google-apps.script

LibreOffice / OpenOffice UNO API 不处理重复的文件名(例如在文件系统下),而 Google Drive 提供了这种可能性。

为了解决这个问题,我创建了四个视图('ChildUri'、'IdentifierUri'、'ItemUri' 和 'Uri'),允许我构建一个新名称的表单: CONCAT (name, ~, position) 对于第二个和后面的doublon,position是它们在GROUP BY子句中的位置...

CREATE VIEW PUBLIC."ChildUri" ("Id","Name","Parent") AS SELECT "I"."Id","I"."Name","C"."ItemId" FROM PUBLIC."Items" AS "I" JOIN PUBLIC."Children" AS "C" ON "I"."Id"="C"."ChildId"  WHERE "I"."Trashed"=FALSE

CREATE VIEW PUBLIC."IdentifierUri" ("Idx","Name","Parent") AS SELECT ARRAY_AGG("I"."Id" ORDER BY "I"."DateCreated","I"."Id"),"I"."Name","C"."Parent" FROM PUBLIC."Items" AS "I" JOIN PUBLIC."ChildUri" AS "C" ON "I"."Id"="C"."Id" GROUP BY "I"."Name","C"."Parent"

CREATE VIEW PUBLIC."ItemUri" ("Id","Name","Length","Position","Parent") AS SELECT "C"."Id","I"."Name",CARDINALITY("I"."Idx"),POSITION_ARRAY("C"."Id" IN "I"."Idx"),"I"."Parent" FROM PUBLIC."ChildUri" AS "C" JOIN PUBLIC."IdentifierUri" AS "I" ON "C"."Name"="I"."Name" AND "C"."Parent"="I"."Parent"

CREATE VIEW PUBLIC."Uri" ("Id","Name","Uri","Parent") AS SELECT "I"."Id","I"."Name",CASEWHEN("I"."Position"=1,"I"."Name",INSERT("I"."Name", LENGTH("I"."Name") - POSITION('.' IN REVERSE("I"."Name")) + 1,0,CONCAT('~',"I"."Position"))),"I"."Parent" FROM PUBLIC."ItemUri" AS "I"

效果很好,但是在调用'selectChild'过程时速度严重不足,执行需要10秒,而之前只有几秒。

CREATE PROCEDURE PUBLIC."selectChild"(IN USERID VARCHAR(100),IN ITEMID VARCHAR(100),IN URL VARCHAR(250),IN MODE SMALLINT,OUT ROWCOUNT SMALLINT) SPECIFIC "selectChild_1" LANGUAGE SQL NOT DETERMINISTIC READS SQL DATA NEW SAVEPOINT LEVEL DYNAMIC RESULT SETS 1 BEGIN ATOMIC DECLARE TMPCOUNT SMALLINT DEFAULT 0;DECLARE RESULT CURSOR WITH RETURN FOR SELECT "Title","Size","DateModified","DateCreated","IsFolder",CASEWHEN("IsFolder",CONCAT(URL,'/',"Id"),CONCAT(URL,'/',"Uri"))"TargetURL",FALSE "IsHidden",FALSE "IsVolume",FALSE "IsRemote",FALSE "IsRemoveable",FALSE "IsFloppy",FALSE "IsCompactDisc" FROM PUBLIC."Child" WHERE "UserId"=USERID AND "Parent"=ITEMID AND("IsFolder" OR "Loaded">=MODE)FOR READ ONLY;CALL "countChild"(USERID,ITEMID,MODE,TMPCOUNT);SET ROWCOUNT=TMPCOUNT;OPEN RESULT;END

我承认超出我的能力,求助于您

提前致谢。

您需要一个索引来提高 SELECT 在 table 上的查询速度。如果过程中的 SELECT 还不是 FOREIGN KEY

,则需要如下索引
CREATE INDEX "ChildIndex" ON "Child"("Parent", "UserId")

fredt,很高兴你能回答...找不到更好的...

我承认索引管理肯定惨淡

所以我修改了底层的tables (Users, Identifiers, Capabilities, Children)来添加UNIQUE CONSTRAINT和FOREIGN KEY CONSTRAINT。我混淆了 UNIQUE INDEX 和 UNIQUE CONSTRAINT...

CREATE CACHED TABLE PUBLIC."Users"("Id" VARCHAR(100) NOT NULL PRIMARY KEY,"UserName" VARCHAR(100) NOT NULL,"DisplayName" VARCHAR(100),"RootId" VARCHAR(100) NOT NULL,"TimeStamp" TIMESTAMP DEFAULT CURRENT_TIMESTAMP(3) NOT NULL,CONSTRAINT "UniqueUserName" UNIQUE ("UserName"))
CREATE CACHED TABLE PUBLIC."Identifiers"("Id" VARCHAR(100) NOT NULL PRIMARY KEY,"UserId" VARCHAR(100) NOT NULL,"TimeStamp" TIMESTAMP DEFAULT CURRENT_TIMESTAMP(3) NOT NULL,CONSTRAINT "ForeignIdentifiersUsers" FOREIGN KEY ("UserId") REFERENCES "Users"("Id"))
CREATE CACHED TABLE PUBLIC."Children"("ChildId" VARCHAR(100) NOT NULL,"ItemId" VARCHAR(100) NOT NULL,"TimeStamp" TIMESTAMP DEFAULT CURRENT_TIMESTAMP(3) NOT NULL,CONSTRAINT "UniqueChildItem" UNIQUE ("ChildId", "ItemId"),CONSTRAINT "ForeignChildrenItems" FOREIGN KEY ("ItemId") REFERENCES "Items"("Id"))
CREATE CACHED TABLE PUBLIC."Capabilities"("UserId" VARCHAR(100) NOT NULL,"ItemId" VARCHAR(100) NOT NULL,"CanAddChild" BOOLEAN DEFAULT FALSE NOT NULL,"CanRename" BOOLEAN DEFAULT FALSE NOT NULL,"IsReadOnly" BOOLEAN DEFAULT FALSE NOT NULL,"IsVersionable" BOOLEAN DEFAULT FALSE NOT NULL,"Loaded" SMALLINT DEFAULT 0 NOT NULL,"SyncMode" SMALLINT DEFAULT 0 NOT NULL,"TimeStamp" TIMESTAMP DEFAULT CURRENT_TIMESTAMP(3) NOT NULL,CONSTRAINT "ForeignCapabilitiesUsers" FOREIGN KEY ("UserId") REFERENCES "Users"("Id"),CONSTRAINT "ForeignCapabilitiesItems" FOREIGN KEY ("ItemId") REFERENCES "Items"("Id"))

我还在 VIEW 项目上添加了 INDEX,Child 和 VIEW 允许我构建新名称(ChildUri、IdentifierUri、ItemUri 和 Uri)。

我不知道我们可以把 INDEX 放在 VIEW 上...

CREATE INDEX "ItemIndex" ON "Item"("UserId", "Id")
CREATE INDEX "ChildUriIndex" ON "ChildUri"("Id","Parent")
CREATE INDEX "IdentifierUriIndex" ON "IdentifierUri"("Idx","Parent")
CREATE INDEX "ItemUriIndex" ON "ItemUri"("Id","Parent")
CREATE INDEX "UriIndex" ON "Uri"("Id","Parent")
CREATE INDEX "ChildIndex" ON "Child"("UserId", "Parent")

我在项目 table 上保留了索引 "TrashedIndex" 和 "MimeTypeIndex",这些列出现在 WHERE 子句中

CREATE INDEX "TrashedIndex" ON PUBLIC."Items"("Trashed")
CREATE INDEX "MimeTypeIndex" ON PUBLIC."Items"("MimeType")

"selectChild" 请求现在在几秒钟内完成,但我不确定我的索引......我认为我有冗余或缺失,或者我很幸运......

再次感谢。

编辑: 经过几次性能测试,显然只有底层table层级的CONSTRAINT UNIQUE和FOREIGN KEY提高了性能,VIEW上的INDEX并没有提高查询,或者在没有性能测试工具的情况下潜移默化。