Add FTS to tag_llist* field
Browse files- create_fts.py +35 -0
- danbooru2023.db +2 -2
- db.py +15 -1
- profiling.py +41 -0
create_fts.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from tqdm import tqdm
|
2 |
+
from db import *
|
3 |
+
|
4 |
+
|
5 |
+
PostFTS.create_table()
|
6 |
+
for data in tqdm(
|
7 |
+
chunked(
|
8 |
+
db.execute_sql(
|
9 |
+
"SELECT "
|
10 |
+
"id, "
|
11 |
+
"tag_list, "
|
12 |
+
"tag_list_general, "
|
13 |
+
"tag_list_artist, "
|
14 |
+
"tag_list_character, "
|
15 |
+
"tag_list_copyright, "
|
16 |
+
"tag_list_meta "
|
17 |
+
"FROM post;"
|
18 |
+
).fetchall(),
|
19 |
+
5000,
|
20 |
+
)
|
21 |
+
):
|
22 |
+
PostFTS.insert_many(
|
23 |
+
[
|
24 |
+
dict(
|
25 |
+
rowid=i[0],
|
26 |
+
tag_list=i[1],
|
27 |
+
tag_list_general=i[2],
|
28 |
+
tag_list_artist=i[3],
|
29 |
+
tag_list_character=i[4],
|
30 |
+
tag_list_copyright=i[5],
|
31 |
+
tag_list_meta=i[6],
|
32 |
+
)
|
33 |
+
for i in data
|
34 |
+
]
|
35 |
+
).on_conflict_ignore().execute()
|
danbooru2023.db
CHANGED
@@ -1,3 +1,3 @@
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
-
oid sha256:
|
3 |
-
size
|
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:16c23b0d61161aa777802dc4d0414402c8f7cf2b1535998449afdec8ae51dd61
|
3 |
+
size 10739814400
|
db.py
CHANGED
@@ -1,6 +1,8 @@
|
|
1 |
import pathlib
|
2 |
import sqlite3
|
|
|
3 |
from peewee import *
|
|
|
4 |
|
5 |
|
6 |
class MemoryConnection(sqlite3.Connection):
|
@@ -46,7 +48,7 @@ def get_tag_by_id(id):
|
|
46 |
return tag_cache_map[id]
|
47 |
|
48 |
|
49 |
-
class TagListField(TextField):
|
50 |
def db_value(self, value):
|
51 |
if isinstance(value, str):
|
52 |
return value
|
@@ -111,6 +113,18 @@ class Post(BaseModel):
|
|
111 |
tag_count_meta = IntegerField()
|
112 |
|
113 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
class Tag(BaseModel):
|
115 |
id = IntegerField(primary_key=True)
|
116 |
name = CharField(unique=True)
|
|
|
1 |
import pathlib
|
2 |
import sqlite3
|
3 |
+
|
4 |
from peewee import *
|
5 |
+
from playhouse.sqlite_ext import FTS5Model, SearchField
|
6 |
|
7 |
|
8 |
class MemoryConnection(sqlite3.Connection):
|
|
|
48 |
return tag_cache_map[id]
|
49 |
|
50 |
|
51 |
+
class TagListField(TextField, SearchField):
|
52 |
def db_value(self, value):
|
53 |
if isinstance(value, str):
|
54 |
return value
|
|
|
113 |
tag_count_meta = IntegerField()
|
114 |
|
115 |
|
116 |
+
class PostFTS(FTS5Model):
|
117 |
+
class Meta:
|
118 |
+
database = db
|
119 |
+
|
120 |
+
tag_list = TagListField()
|
121 |
+
tag_list_general = TagListField()
|
122 |
+
tag_list_artist = TagListField()
|
123 |
+
tag_list_character = TagListField()
|
124 |
+
tag_list_copyright = TagListField()
|
125 |
+
tag_list_meta = TagListField()
|
126 |
+
|
127 |
+
|
128 |
class Tag(BaseModel):
|
129 |
id = IntegerField(primary_key=True)
|
130 |
name = CharField(unique=True)
|
profiling.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from time import time_ns as tns
|
2 |
+
from db import *
|
3 |
+
|
4 |
+
|
5 |
+
print('warm up for tag_cache')
|
6 |
+
posts = list(Post.select().where(Post.tag_list.contains("$6KMR#")))
|
7 |
+
|
8 |
+
|
9 |
+
print('start profiling')
|
10 |
+
|
11 |
+
t0 = tns()
|
12 |
+
posts = list(
|
13 |
+
Post.select(Post, PostFTS)
|
14 |
+
.join(PostFTS, on = (Post.id == PostFTS.rowid))
|
15 |
+
.where(PostFTS.tag_list.match('"$6KMR#"'))
|
16 |
+
)
|
17 |
+
t1 = tns()
|
18 |
+
print((t1 - t0) / 1e9)
|
19 |
+
print(len(posts))
|
20 |
+
|
21 |
+
|
22 |
+
t0 = tns()
|
23 |
+
data = list(
|
24 |
+
i[0]
|
25 |
+
for i in db.execute_sql(
|
26 |
+
"SELECT id FROM post WHERE tag_list LIKE '%$6KMR#%';"
|
27 |
+
).fetchall()
|
28 |
+
)
|
29 |
+
posts = list(Post.select().where(Post.id << data))
|
30 |
+
t1 = tns()
|
31 |
+
print((t1 - t0) / 1e9)
|
32 |
+
print(len(data))
|
33 |
+
print(posts[0].tag_list)
|
34 |
+
|
35 |
+
|
36 |
+
t0 = tns()
|
37 |
+
posts = list(Post.select().where(Post.tag_list.contains("$6KMR#")))
|
38 |
+
t1 = tns()
|
39 |
+
print((t1 - t0) / 1e9)
|
40 |
+
print(len(posts))
|
41 |
+
print(posts[0].tag_list)
|