Skip to content

Commit 6becf40

Browse files
authored
feat(docs): reorganize DB queries, add tags (#20303)
* reorganize * fix checksum
1 parent ee4ae40 commit 6becf40

File tree

1 file changed

+41
-11
lines changed

1 file changed

+41
-11
lines changed

docs/docs/guides/database-queries.md

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Run `docker exec -it immich_postgres psql --dbname=<DB_DATABASE_NAME> --username
1212

1313
## Assets
1414

15+
### Name
16+
1517
:::note
1618
The `"originalFileName"` column is the name of the file at time of upload, including the extension.
1719
:::
@@ -27,6 +29,8 @@ SELECT * FROM "asset" WHERE "originalPath" = 'upload/library/admin/2023/2023-09-
2729
SELECT * FROM "asset" WHERE "originalPath" LIKE 'upload/library/admin/2023/%';
2830
```
2931

32+
### ID
33+
3034
```sql title="Find by ID"
3135
SELECT * FROM "asset" WHERE "id" = '9f94e60f-65b6-47b7-ae44-a4df7b57f0e9';
3236
```
@@ -35,6 +39,8 @@ SELECT * FROM "asset" WHERE "id" = '9f94e60f-65b6-47b7-ae44-a4df7b57f0e9';
3539
SELECT * FROM "asset" WHERE "id"::text LIKE '%ab431d3a%';
3640
```
3741

42+
### Checksum
43+
3844
:::note
3945
You can calculate the checksum for a particular file by using the command `sha1sum <filename>`.
4046
:::
@@ -51,6 +57,8 @@ SELECT T1."checksum", array_agg(T2."id") ids FROM "asset" T1
5157
WHERE T1."deletedAt" IS NULL GROUP BY T1."checksum";
5258
```
5359

60+
### Metadata
61+
5462
```sql title="Live photos"
5563
SELECT * FROM "asset" WHERE "livePhotoVideoId" IS NOT NULL;
5664
```
@@ -77,27 +85,37 @@ SELECT * FROM "asset"
7785
ORDER BY "asset_exif"."fileSizeInByte" ASC;
7886
```
7987

80-
```sql title="Without thumbnails"
81-
SELECT * FROM "asset" WHERE "asset"."previewPath" IS NULL OR "asset"."thumbnailPath" IS NULL;
82-
```
88+
### Type
8389

8490
```sql title="By type"
8591
SELECT * FROM "asset" WHERE "asset"."type" = 'VIDEO';
8692
SELECT * FROM "asset" WHERE "asset"."type" = 'IMAGE';
8793
```
8894

8995
```sql title="Count by type"
90-
SELECT "asset"."type", COUNT(1) FROM "asset" GROUP BY "asset"."type";
96+
SELECT "asset"."type", COUNT(*) FROM "asset" GROUP BY "asset"."type";
9197
```
9298

9399
```sql title="Count by type (per user)"
94-
SELECT "user"."email", "asset"."type", COUNT(1) FROM "asset"
100+
SELECT "user"."email", "asset"."type", COUNT(*) FROM "asset"
95101
JOIN "user" ON "asset"."ownerId" = "user"."id"
96102
GROUP BY "asset"."type", "user"."email" ORDER BY "user"."email";
97103
```
98104

99-
```sql title="Failed file movements"
100-
SELECT * FROM "move_history";
105+
## Tags
106+
107+
```sql title="Count by tag"
108+
SELECT "t"."value" AS "tag_name", COUNT(*) AS "number_assets" FROM "tag" "t"
109+
JOIN "tag_asset" "ta" ON "t"."id" = "ta"."tagsId" JOIN "asset" "a" ON "ta"."assetsId" = "a"."id"
110+
WHERE "a"."visibility" != 'hidden'
111+
GROUP BY "t"."value" ORDER BY "number_assets" DESC;
112+
```
113+
114+
```sql title="Count by tag (per user)"
115+
SELECT "t"."value" AS "tag_name", "u"."email" as "user_email", COUNT(*) AS "number_assets" FROM "tag" "t"
116+
JOIN "tag_asset" "ta" ON "t"."id" = "ta"."tagsId" JOIN "asset" "a" ON "ta"."assetsId" = "a"."id" JOIN "user" "u" ON "a"."ownerId" = "u"."id"
117+
WHERE "a"."visibility" != 'hidden'
118+
GROUP BY "t"."value", "u"."email" ORDER BY "number_assets" DESC;
101119
```
102120

103121
## Users
@@ -110,18 +128,30 @@ SELECT * FROM "user";
110128
SELECT "user".* FROM "user" JOIN "asset" ON "user"."id" = "asset"."ownerId" WHERE "asset"."id" = 'fa310b01-2f26-4b7a-9042-d578226e021f';
111129
```
112130

113-
## System Config
131+
## Persons
132+
133+
```sql title="Delete person and unset it for the faces it was associated with"
134+
DELETE FROM "person" WHERE "name" = 'PersonNameHere';
135+
```
136+
137+
## System
138+
139+
### Config
114140

115141
```sql title="Custom settings"
116142
SELECT "key", "value" FROM "system_metadata" WHERE "key" = 'system-config';
117143
```
118144

119145
(Only used when not using the [config file](/docs/install/config-file))
120146

121-
## Persons
147+
### File properties
122148

123-
```sql title="Delete person and unset it for the faces it was associated with"
124-
DELETE FROM "person" WHERE "name" = 'PersonNameHere';
149+
```sql title="Without thumbnails"
150+
SELECT * FROM "asset" WHERE "asset"."previewPath" IS NULL OR "asset"."thumbnailPath" IS NULL;
151+
```
152+
153+
```sql title="Failed file movements"
154+
SELECT * FROM "move_history";
125155
```
126156

127157
## Postgres internal

0 commit comments

Comments
 (0)