Peach: Local-first video metadata scraper and media manager.
perf(web): 厂牌索引页的代表作钉住连接顺序,整页查询回到十毫秒量级
longmeidao2b3016b7ea6b
diff --git a/src/peach/web_entity.py b/src/peach/web_entity.pyindex e71c69932..099f0e40b 100644--- a/src/peach/web_entity.py+++ b/src/peach/web_entity.py@@ -507,11 +507,13 @@ def q_index(contract: WebContract, kind, q="", limit=600, offset=0, category="")# 片商按合计排,旗下 label 的片都归它(ADR-0051 修订);有上级的 label 不单列,# 从片商页的名册进去。搜索时照常列出所有叫这个名字的,找 label 不必先猜它归谁。# 从 entity 出发:自己一部片都没挂、片全在旗下的片商(妄想族)也要出现。+ # 两个子查询都用 `CROSS JOIN` 钉住连接顺序,原因见 `scope_predicate`:交给规划器+ # 的话,代表作那句每家片商都把全部有截图的视频回查一遍,一百多家叠起来要两秒多。scope = scope_predicate("studio", "ae.entity_id", "e.id")sql = ("SELECT * FROM (SELECT e.id entity_id,e.canonical_name k,""(SELECT count(DISTINCT ae.asset_id) FROM asset_entity ae"- " JOIN asset a ON a.id=ae.asset_id WHERE a.medium='video' AND " + scope + ") n,"- "(SELECT a2.id FROM asset_entity ae JOIN asset a2 ON a2.id=ae.asset_id "+ " CROSS JOIN asset a ON a.id=ae.asset_id WHERE a.medium='video' AND " + scope + ") n,"+ "(SELECT a2.id FROM asset_entity ae CROSS JOIN asset a2 ON a2.id=ae.asset_id "" WHERE " + scope + " AND a2.medium='video' AND a2.snapshot_path IS NOT NULL "" ORDER BY COALESCE(a2.play_count,0) DESC,COALESCE(a2.play_seconds,0) DESC,"" COALESCE(a2.width,0)*COALESCE(a2.height,0) DESC,a2.size DESC LIMIT 1) rep "diff --git a/tests/test_label_maker.py b/tests/test_label_maker.pyindex ac7b239be..cff9e71d5 100644--- a/tests/test_label_maker.py+++ b/tests/test_label_maker.py@@ -195,6 +195,28 @@ class LabelMakerTests(unittest.TestCase):found = rm_web.q_index(self.contract, "studios", q="BAZOOKA")self.assertEqual([(row["k"], row["n"]) for row in found["items"]], [("BAZOOKA", 1)])+ def test_the_index_picks_each_makers_representative_from_every_layer(self):+ """K M Produce 那格取 BAZOOKA 那部;妄想族自己没挂片,取 ABC/妄想族 那部。++ K M Produce 名下最大的那部没有截图,不当代表作。搜名字时 label 那格取它自己的。+ """+ self.apply()+ self.con.executemany("UPDATE asset SET snapshot_path='s.jpg',size=? WHERE id=?",+ [(500, 1), (200, 2), (900, 3)])+ self.con.execute(+ "INSERT INTO asset(id,location,path,name,medium,size,first_seen)"+ " VALUES(4,'local',?,'d.mp4','video',999,'2026-01-01')", (r"R:\Media\d.mp4",))+ self.con.execute(+ "INSERT INTO asset_entity(asset_id,entity_id,role,source,confidence)"+ " VALUES(4,5607,'studio','test',1.0)")+ self.con.commit()+ index = rm_web.q_index(self.contract, "studios")+ self.assertEqual([(row["k"], row["rep"]) for row in index["items"]],+ [("K M Produce", 1), ("妄想族", 3)])+ found = rm_web.q_index(self.contract, "studios", q="妄想族")+ self.assertEqual([(row["k"], row["rep"]) for row in found["items"]],+ [("ABC/妄想族", 3), ("妄想族", 3)])+def test_the_detail_carries_the_maker_beside_the_label(self):self.apply()studio = rm_web.q_item(self.contract, 3)["entity_refs"]["studio"]