Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:zap: Improve database loading performance https://github.com/siyuan-note/siyuan/issues/15115

Daniel17369d28c9fd
diff --git a/kernel/filesys/tree.go b/kernel/filesys/tree.go
index 346e98f272..820bd6573e 100644
--- a/kernel/filesys/tree.go
+++ b/kernel/filesys/tree.go
@@ -41,6 +41,10 @@ import (
func LoadTrees(ids []string) (ret map[string]*parse.Tree) {
ret = map[string]*parse.Tree{}
+ if 1 > len(ids) {
+ return ret
+ }
+
bts := treenode.GetBlockTrees(ids)
luteEngine := util.NewLute()
var boxIDs []string
diff --git a/kernel/sql/av_gallery.go b/kernel/sql/av_gallery.go
index d565769d3d..c7e7f5f38f 100644
--- a/kernel/sql/av_gallery.go
+++ b/kernel/sql/av_gallery.go
@@ -10,6 +10,7 @@ import (
"github.com/88250/lute/parse"
"github.com/88250/lute/render"
"github.com/siyuan-note/siyuan/kernel/av"
+ "github.com/siyuan-note/siyuan/kernel/filesys"
"github.com/siyuan-note/siyuan/kernel/treenode"
"github.com/siyuan-note/siyuan/kernel/util"
)
@@ -66,6 +67,16 @@ func RenderAttributeViewGallery(attrView *av.AttributeView, view *av.View, query
cardsValues := generateAttrViewItems(attrView) // 生成卡片
filterNotFoundAttrViewItems(&cardsValues) // 过滤掉不存在的卡片
+ // 批量加载绑定块对应的树
+ var ialIDs []string
+ for _, card := range ret.Cards {
+ block := card.GetBlockValue()
+ if nil != block && !block.IsDetached {
+ ialIDs = append(ialIDs, card.ID)
+ }
+ }
+ boundTrees := filesys.LoadTrees(ialIDs)
+
// 生成卡片字段值
for cardID, cardValues := range cardsValues {
var galleryCard av.GalleryCard
@@ -97,19 +108,12 @@ func RenderAttributeViewGallery(attrView *av.AttributeView, view *av.View, query
galleryCard.Values = append(galleryCard.Values, fieldValue)
}
- fillAttributeViewGalleryCardCover(attrView, view, cardValues, &galleryCard, cardID, luteEngine)
+ fillAttributeViewGalleryCardCover(attrView, view, cardValues, &galleryCard, cardID, luteEngine, boundTrees)
ret.Cards = append(ret.Cards, &galleryCard)
}
// 批量获取块属性以提升性能
- var ialIDs []string
- for _, card := range ret.Cards {
- block := card.GetBlockValue()
- if nil != block && !block.IsDetached {
- ialIDs = append(ialIDs, card.ID)
- }
- }
- ials := BatchGetBlockAttrs(ialIDs)
+ ials := BatchGetBlockAttrsWitTrees(ialIDs, boundTrees)
// 渲染自动生成的字段值,比如关联字段、汇总字段、创建时间字段和更新时间字段
avCache := map[string]*av.AttributeView{}
@@ -139,7 +143,7 @@ func RenderAttributeViewGallery(attrView *av.AttributeView, view *av.View, query
return
}
-func fillAttributeViewGalleryCardCover(attrView *av.AttributeView, view *av.View, cardValues []*av.KeyValues, galleryCard *av.GalleryCard, cardID string, luteEngine *lute.Lute) {
+func fillAttributeViewGalleryCardCover(attrView *av.AttributeView, view *av.View, cardValues []*av.KeyValues, galleryCard *av.GalleryCard, cardID string, luteEngine *lute.Lute, trees map[string]*parse.Tree) {
switch view.Gallery.CoverFrom {
case av.CoverFromNone:
case av.CoverFromContentImage:
@@ -148,7 +152,7 @@ func fillAttributeViewGalleryCardCover(attrView *av.AttributeView, view *av.View
break
}
- tree := loadTreeByBlockID(blockValue.BlockID)
+ tree := trees[blockValue.BlockID]
if nil == tree {
break
}
diff --git a/kernel/sql/block.go b/kernel/sql/block.go
index dd3458fa2f..6b9514f388 100644
--- a/kernel/sql/block.go
+++ b/kernel/sql/block.go
@@ -276,6 +276,34 @@ func nodeStaticContent(node *ast.Node, excludeTypes []string, includeTextMarkATi
return buf.String()
}
+func BatchGetBlockAttrsWitTrees(ids []string, trees map[string]*parse.Tree) (ret map[string]map[string]string) {
+ ret = map[string]map[string]string{}
+
+ hitCache := true
+ for _, id := range ids {
+ ial := cache.GetBlockIAL(id)
+ if nil != ial {
+ ret[id] = ial
+ continue
+ }
+ hitCache = false
+ break
+ }
+ if hitCache {
+ return
+ }
+
+ for _, id := range ids {
+ tree := trees[id]
+ if nil == tree {
+ continue
+ }
+
+ ret[id] = getBlockAttrsFromTree(id, tree)
+ }
+ return
+}
+
func BatchGetBlockAttrs(ids []string) (ret map[string]map[string]string) {
ret = map[string]map[string]string{}