修正有外部筛选的 total
This commit is contained in:
11
README.md
11
README.md
@@ -52,6 +52,17 @@ CREATE FULLTEXT INDEX idx_tags ON web_article (tags);
|
|||||||
-- 同步收藏
|
-- 同步收藏
|
||||||
ALTER TABLE web_praise ADD COLUMN gorse BOOLEAN DEFAULT FALSE;
|
ALTER TABLE web_praise ADD COLUMN gorse BOOLEAN DEFAULT FALSE;
|
||||||
ALTER TABLE web_praise ADD COLUMN gorse BOOLEAN DEFAULT FALSE;
|
ALTER TABLE web_praise ADD COLUMN gorse BOOLEAN DEFAULT FALSE;
|
||||||
|
|
||||||
|
|
||||||
|
-- 排序内存
|
||||||
|
SET GLOBAL sort_buffer_size = 268435456; -- 设置为 256MB (268435456 字节)
|
||||||
|
|
||||||
|
-- 降序索引
|
||||||
|
CREATE INDEX idx_id_desc ON web_images (id DESC);
|
||||||
|
|
||||||
|
-- 复合筛选降序索引 article_category_top_id
|
||||||
|
CREATE INDEX idx_acti_id_desc ON web_images (article_category_top_id, id DESC);
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|
@@ -921,7 +921,7 @@ func NewSchema(config Config) (graphql.Schema, error) {
|
|||||||
},
|
},
|
||||||
"_source": false,
|
"_source": false,
|
||||||
"sort": []string{"_score"},
|
"sort": []string{"_score"},
|
||||||
"size": 100000,
|
"size": 200000,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("ZincSearch 获取图像列表失败", err)
|
fmt.Println("ZincSearch 获取图像列表失败", err)
|
||||||
@@ -936,12 +936,13 @@ func NewSchema(config Config) (graphql.Schema, error) {
|
|||||||
return map[string]interface{}{"list": []Image{}, "total": 0}, nil
|
return map[string]interface{}{"list": []Image{}, "total": 0}, nil
|
||||||
}
|
}
|
||||||
query = query.Where(goqu.Ex{"web_images.id": goqu.Op{"in": item}})
|
query = query.Where(goqu.Ex{"web_images.id": goqu.Op{"in": item}})
|
||||||
|
total = len(item)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 筛选:相似图像
|
// 筛选:相似图像
|
||||||
if args.Similar != 0 {
|
if p.Args["similar"] != nil {
|
||||||
var item []int
|
var item []int
|
||||||
for _, id := range models.GetSimilarImagesIdList(args.Similar, 200) {
|
for _, id := range models.GetSimilarImagesIdList(p.Args["similar"].(int), 200) {
|
||||||
item = append(item, int(id))
|
item = append(item, int(id))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -952,6 +953,7 @@ func NewSchema(config Config) (graphql.Schema, error) {
|
|||||||
query = query.Where(goqu.Ex{"web_images.id": goqu.Op{"in": item}}).Select("web_images.id", goqu.L(
|
query = query.Where(goqu.Ex{"web_images.id": goqu.Op{"in": item}}).Select("web_images.id", goqu.L(
|
||||||
fmt.Sprintf("ROW_NUMBER() OVER(ORDER BY FIELD(%s, %s))", "web_images.id", regexp.MustCompile(`[\[\]]`).ReplaceAllString(strings.Join(strings.Fields(fmt.Sprint(item)), ", "), "")),
|
fmt.Sprintf("ROW_NUMBER() OVER(ORDER BY FIELD(%s, %s))", "web_images.id", regexp.MustCompile(`[\[\]]`).ReplaceAllString(strings.Join(strings.Fields(fmt.Sprint(item)), ", "), "")),
|
||||||
).As("row_num"))
|
).As("row_num"))
|
||||||
|
total = len(item)
|
||||||
|
|
||||||
// 收集阅读行为
|
// 收集阅读行为
|
||||||
if p.Context.Value("user_id") != nil && p.Args["after"] == nil {
|
if p.Context.Value("user_id") != nil && p.Args["after"] == nil {
|
||||||
@@ -989,6 +991,7 @@ func NewSchema(config Config) (graphql.Schema, error) {
|
|||||||
query = query.Where(goqu.Ex{"web_images.id": goqu.Op{"in": list}}).Select("web_images.id", goqu.L(
|
query = query.Where(goqu.Ex{"web_images.id": goqu.Op{"in": list}}).Select("web_images.id", goqu.L(
|
||||||
fmt.Sprintf("ROW_NUMBER() OVER(ORDER BY FIELD(%s, %s))", "web_images.id", regexp.MustCompile(`[\[\]]`).ReplaceAllString(strings.Join(strings.Fields(fmt.Sprint(list)), ", "), "")),
|
fmt.Sprintf("ROW_NUMBER() OVER(ORDER BY FIELD(%s, %s))", "web_images.id", regexp.MustCompile(`[\[\]]`).ReplaceAllString(strings.Join(strings.Fields(fmt.Sprint(list)), ", "), "")),
|
||||||
).As("row_num"))
|
).As("row_num"))
|
||||||
|
total = len(list)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 筛选:时间段
|
// 筛选:时间段
|
||||||
@@ -1188,6 +1191,8 @@ func NewSchema(config Config) (graphql.Schema, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 如果没有外部排序则使用指定排序(正则sort只能是字母数字下划线)
|
||||||
|
if p.Args["similar"] == nil && p.Args["interest"] == nil && p.Args["praise"] == nil {
|
||||||
// 如果查询了 total 字段
|
// 如果查询了 total 字段
|
||||||
if existField(p.Info.FieldASTs[0].SelectionSet.Selections, "total") {
|
if existField(p.Info.FieldASTs[0].SelectionSet.Selections, "total") {
|
||||||
sql, _, _ := query.ToSQL()
|
sql, _, _ := query.ToSQL()
|
||||||
@@ -1196,25 +1201,22 @@ func NewSchema(config Config) (graphql.Schema, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
query = query.Select("web_images.id", goqu.L(fmt.Sprintf(
|
||||||
// 如果没有外部排序则使用指定排序(正则sort只能是字母数字下划下)
|
"ROW_NUMBER() OVER(ORDER BY web_images.%s %s)",
|
||||||
if p.Args["similar"] == nil && p.Args["interest"] == nil && p.Args["praise"] == nil {
|
regexp.MustCompile(`[^a-zA-Z0-9_]`).ReplaceAllString(p.Args["sort"].(string), ""),
|
||||||
sort := regexp.MustCompile(`[^a-zA-Z0-9_]`).ReplaceAllString(p.Args["sort"].(string), "")
|
p.Args["order"].(string),
|
||||||
query = query.Select("web_images.id", goqu.L(
|
)).As("row_num"))
|
||||||
fmt.Sprintf("ROW_NUMBER() OVER(ORDER BY web_images.%s %s)", sort, p.Args["order"].(string)),
|
|
||||||
).As("row_num"))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 图像按点赞顺序排序 p.Args["sort"].(string) == "praise_time"
|
// 图像按点赞顺序排序 p.Args["sort"].(string) == "praise_time"
|
||||||
if p.Args["praise"] != nil {
|
if p.Args["praise"] != nil {
|
||||||
query = query.Select("web_images.id", goqu.L(
|
query = query.Select("web_images.id", goqu.L(
|
||||||
fmt.Sprintf("ROW_NUMBER() OVER(ORDER BY web_images.%s %s)", "create_time", p.Args["order"].(string)),
|
fmt.Sprintf("ROW_NUMBER() OVER(ORDER BY web_praise.%s %s)", "id", p.Args["order"].(string)),
|
||||||
).As("row_num"))
|
).As("row_num"))
|
||||||
}
|
}
|
||||||
|
|
||||||
// 取所有数据的前N条
|
// 取所有数据的前N条
|
||||||
sql, _, _ := query.Where(goqu.Ex{"article_category_top_id": 22}).ToSQL()
|
sql, _, _ := query.Where(goqu.Ex{"article_category_top_id": 22}).ToSQL()
|
||||||
fmt.Println(sql)
|
|
||||||
|
|
||||||
// 遊標截取篩選結果集的前N条
|
// 遊標截取篩選結果集的前N条
|
||||||
var cursor string
|
var cursor string
|
||||||
@@ -1229,16 +1231,14 @@ func NewSchema(config Config) (graphql.Schema, error) {
|
|||||||
limit = args.Last
|
limit = args.Last
|
||||||
}
|
}
|
||||||
|
|
||||||
//fmt.Println("SQL:", sql)
|
|
||||||
|
|
||||||
// 優化查詢: 如果是第一頁(未傳入遊標), 可以在分頁前(窗口函數)立即截取長度以大幅降低排序選取耗時
|
|
||||||
sql = fmt.Sprintf(`
|
sql = fmt.Sprintf(`
|
||||||
WITH RankedArticles AS (%s)
|
WITH RankedArticles AS (%s)
|
||||||
SELECT * FROM web_images INNER JOIN(
|
SELECT web_images.* FROM web_images INNER JOIN(
|
||||||
SELECT id, row_num FROM RankedArticles %s
|
SELECT id, row_num FROM RankedArticles %s
|
||||||
) AS LimitedRanked ON LimitedRanked.id = web_images.id
|
) AS LimitedRanked ON LimitedRanked.id = web_images.id
|
||||||
ORDER BY LimitedRanked.row_num ASC LIMIT %d
|
ORDER BY LimitedRanked.row_num ASC LIMIT %d
|
||||||
`, sql, cursor, limit)
|
`, sql, cursor, limit)
|
||||||
|
fmt.Println(sql)
|
||||||
|
|
||||||
if err := db.Raw(sql).Scan(&images).Error; err != nil {
|
if err := db.Raw(sql).Scan(&images).Error; err != nil {
|
||||||
fmt.Println("获取图像列表失败", err)
|
fmt.Println("获取图像列表失败", err)
|
||||||
@@ -1250,7 +1250,7 @@ func NewSchema(config Config) (graphql.Schema, error) {
|
|||||||
ids = append(ids, item.ID)
|
ids = append(ids, item.ID)
|
||||||
}
|
}
|
||||||
|
|
||||||
var find = db.Where("web_images.id IN ?", ids).Select("*", "CASE WHEN activity = '1' THEN TRUE ELSE FALSE END AS is_active")
|
var find = db.Where("web_images.id IN ?", ids).Select("*", "CASE WHEN activity = '1' THEN TRUE ELSE FALSE END")
|
||||||
for _, item := range LoadItem(p.Info.FieldASTs[0].SelectionSet.Selections) {
|
for _, item := range LoadItem(p.Info.FieldASTs[0].SelectionSet.Selections) {
|
||||||
find = find.Preload(item)
|
find = find.Preload(item)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user