Slow size query on filecache table
I am noticing a major performance issue when synchronizing a directory with a lot of small files from Gnucash financial-accounting software.
Owncloud (version 8.0.2) is running on a low end system, an ARM-based Cubietruck with Allwinner A20 SoC and 2GiB of RAM. Operating System is Gentoo GNU/Linux with kernel 3.19.3. The database is PostgreSQL 9.4.1.
Looking at "SELECT * from pg_stat_activity" I see a lot of the following queries accumulating:
SELECT SUM("size") AS f1, MIN("size") AS f2, SUM("unencrypted_size") AS f3 FROM "oc_filecache" WHERE "parent" = $1 AND "storage" = $2
Using EXPLAIN i found out that it is performing a full table scan. So the solution for me was to add a new index helping with this query:
CREATE INDEX fs_storage_parent ON oc_filecache (storage ASC, parent ASC)
Afterwards the performance is much better.
As I can see in https://github.com/owncloud/core/blob/master/db_structure.xml there has been no such index been introduced into Git master. Please consider creating an index like that.
@icewind1991 @DeepDiver1975 Any thoughts? THX. – I am by far no database magician ;-)
As I can see in https://github.com/owncloud/core/blob/master/db_structure.xml there has been no such index been introduced into Git master. Please consider creating an index like that.
@fkoester feel free to submit a pull request - THX
I am running oc 7.0.5 on Postgres 9.3 and don't see that full table scan:
owncloud=# explain SELECT SUM("size") AS f1, SUM("unencrypted_size") AS f2 FROM "oc_filecache" WHERE "parent" = 456 AND "storage" = 123 AND "size" >= 0;
QUERY PLAN
---------------------------------------------------------------------------------------------------
Aggregate (cost=173.02..173.03 rows=1 width=16)
-> Bitmap Heap Scan on oc_filecache (cost=169.00..173.02 rows=1 width=16)
Recheck Cond: ((parent = 456) AND (storage = 123))
Filter: (size >= 0)
-> BitmapAnd (cost=169.00..169.00 rows=1 width=0)
-> Bitmap Index Scan on fs_parent_name_hash (cost=0.00..14.88 rows=309 width=0)
Index Cond: (parent = 456)
-> Bitmap Index Scan on fs_storage_mimetype (cost=0.00..153.87 rows=6591 width=0)
Index Cond: (storage = 123)
(our oc_filecache table has around 16 million rows)
I am not going to create that index as it seems that we are doing just fine. @fkoester can you post your EXPLAIN output please?
I am neither a Postgres nor an SQL expert, so I just report my observations as detailed as possible:
I am not sure but to the best of my recollection explain was showing a full table scan operation on the query stated above, before creating the additional index.
I now dropped the additional index and explain is giving me this:
owncloud=> EXPLAIN SELECT SUM("size") AS f1, MIN("size") AS f2, SUM("unencrypted_size") AS f3 FROM "oc_filecache" WHERE "parent" = 107029 AND "storage" = 1;
QUERY PLAN
---------------------------------------------------------------------------------------------------
Aggregate (cost=1937.43..1937.44 rows=1 width=16)
-> Index Scan using fs_storage_path_hash on oc_filecache (cost=0.54..1937.42 rows=1 width=16)
Index Cond: (storage = 1)
Filter: (parent = 107029)
I cannot see the sequential scan anymore but I also do not see the bitmap heap scan you posted. I am really confused, maybe someone can explain this.
Here's my table definition - it should look pretty similar to yours:
owncloud=# \d oc_filecache
Table "public.oc_filecache"
Column | Type | Modifiers
------------------+-------------------------+---------------------------------------------------------------
fileid | integer | not null default nextval('oc_filecache_fileid_seq'::regclass)
storage | integer | not null default 0
path | character varying(4000) | default NULL::character varying
path_hash | character varying(32) | not null default ''::character varying
parent | integer | not null default 0
name | character varying(250) | default NULL::character varying
mimetype | integer | not null default 0
mimepart | integer | not null default 0
size | bigint | not null default (0)::bigint
mtime | integer | not null default 0
storage_mtime | integer | not null default 0
encrypted | integer | not null default 0
unencrypted_size | bigint | not null default (0)::bigint
etag | character varying(40) | default NULL::character varying
permissions | integer | default 0
Indexes:
"oc_filecache_pkey" PRIMARY KEY, btree (fileid)
"fs_storage_path_hash" UNIQUE, btree (storage, path_hash)
"fs_parent_name_hash" btree (parent, name)
"fs_storage_mimepart" btree (storage, mimepart)
"fs_storage_mimetype" btree (storage, mimetype)
"fs_storage_size" btree (storage, size, fileid)
how many rows do you have in your oc_filecache table? And how many dead tuples (that is what's killing our database regularly at the moment)
owncloud=# SELECT psut.relname,
to_char(psut.last_vacuum, 'YYYY-MM-DD HH24:MI') as last_vacuum,
to_char(psut.last_autovacuum, 'YYYY-MM-DD HH24:MI') as last_autovacuum,
to_char(pg_class.reltuples, '9G999G999G999') AS n_tup,
to_char(psut.n_dead_tup, '9G999G999G999') AS dead_tup,
to_char(CAST(current_setting('autovacuum_vacuum_threshold') AS bigint)
+ (CAST(current_setting('autovacuum_vacuum_scale_factor') AS numeric)
* pg_class.reltuples), '9G999G999G999') AS av_threshold,
CASE
WHEN CAST(current_setting('autovacuum_vacuum_threshold') AS bigint)
+ (CAST(current_setting('autovacuum_vacuum_scale_factor') AS numeric)
* pg_class.reltuples) < psut.n_dead_tup
THEN '*'
ELSE ''
END AS expect_av
FROM pg_stat_user_tables psut
JOIN pg_class on psut.relid = pg_class.oid
Table definition:
owncloud=> \d oc_filecache
Table "public.oc_filecache"
Column | Type | Modifiers
------------------+-------------------------+---------------------------------------------------------------
fileid | integer | not null default nextval('oc_filecache_fileid_seq'::regclass)
storage | integer | not null default 0
path | character varying(4000) | default NULL::character varying
path_hash | character varying(32) | not null default ''::character varying
parent | integer | not null default 0
name | character varying(250) | default NULL::character varying
mimetype | integer | not null default 0
mimepart | integer | not null default 0
size | bigint | not null default (0)::bigint
mtime | integer | not null default 0
storage_mtime | integer | not null default 0
encrypted | integer | not null default 0
unencrypted_size | bigint | not null default (0)::bigint
etag | character varying(40) | default NULL::character varying
permissions | integer | default 0
Indexes:
"oc_filecache_pkey" PRIMARY KEY, btree (fileid)
"fs_storage_path_hash" UNIQUE, btree (storage, path_hash)
"fs_parent_name_hash" btree (parent, name)
"fs_storage_mimepart" btree (storage, mimepart)
"fs_storage_mimetype" btree (storage, mimetype)
"fs_storage_size" btree (storage, size, fileid)
Row count:
owncloud=> SELECT COUNT(*) FROM oc_filecache;
count
-------
49017
(1 row)
Dead tuples:
relname | last_vacuum | last_autovacuum | n_tup | dead_tup | av_threshold | expect_av
------------------------------+-------------+-----------------+----------------+----------------+----------------+-----------
oc_activity | | | 0 | 0 | 50 |
oc_appconfig | | | 88 | 68 | 68 | *
oc_activity_mq | | | 0 | 0 | 50 |
oc_bookmarks | | | 0 | 0 | 50 |
oc_clndr_repeat | | | 0 | 0 | 50 |
oc_clndr_calendars | | | 0 | 31 | 50 |
oc_clndr_objects | | | 0 | 4 | 50 |
oc_bookmarks_tags | | | 0 | 0 | 50 |
oc_clndr_share_calendar | | | 0 | 0 | 50 |
oc_contacts_cards_properties | | | 0 | 38 | 50 |
oc_clndr_share_event | | | 0 | 0 | 50 |
oc_contacts_addressbooks | | | 0 | 7 | 50 |
oc_documents_member | | | 0 | 0 | 50 |
oc_contacts_cards | | | 0 | 5 | 50 |
oc_gallery_sharing | | | 0 | 0 | 50 |
oc_documents_revisions | | | 0 | 0 | 50 |
oc_documents_op | | | 0 | 0 | 50 |
oc_documents_session | | | 0 | 0 | 50 |
oc_filecache | | | 48.734 | 7.094.698 | 9.797 | *
oc_file_map | | | 0 | 0 | 50 |
oc_files_trash | | | 0 | 594 | 50 | *
oc_documents_invite | | | 0 | 0 | 50 |
oc_group_admin | | | 0 | 0 | 50 |
oc_group_user | | | 1 | 0 | 50 |
oc_lucene_status | | | 0 | 0 | 50 |
oc_jobs | | | 0 | 550 | 50 | *
oc_locks | | | 0 | 0 | 50 |
oc_mimetypes | | | 0 | 0 | 50 |
oc_pictures_images_cache | | | 0 | 0 | 50 |
oc_groups | | | 1 | 0 | 50 |
oc_preferences | | | 10 | 84 | 52 | *
oc_privatedata | | | 0 | 0 | 50 |
oc_properties | | | 0 | 0 | 50 |
oc_users | | | 0 | 1 | 50 |
oc_share | | | 0 | 0 | 50 |
oc_vcategory | | | 0 | 0 | 50 |
oc_vcategory_to_object | | | 0 | 0 | 50 |
oc_storages | | | 0 | 0 | 50 |
oc_share_external | | | 0 | 0 | 50 |
Table definition looks equal to me.
woah - you will want to do a VACUUM FULL ANALYZE oc_filecache - 7 million dead tuples (i.e. tuples that were updated) and are now wasting space
Yeah, I just saw that too :(
I just checked and saw that my vacuum cronjob failed for some time. Oops..
Sorry for the trouble, I think I probably can close this issue than.
Thank you very much for helping me understand this!
Yeah, vacuuming helped :-)
owncloud=> EXPLAIN SELECT SUM("size") AS f1, MIN("size") AS f2, SUM("unencrypted_size") AS f3 FROM "oc_filecache" WHERE "parent" = 126296 AND "storage" = 1;
QUERY PLAN
-------------------------------------------------------------------------------------------
Aggregate (cost=408.44..408.45 rows=1 width=16)
-> Bitmap Heap Scan on oc_filecache (cost=236.87..408.11 rows=43 width=16)
Recheck Cond: (parent = 126296)
Filter: (storage = 1)
-> Bitmap Index Scan on fs_parent_name_hash (cost=0.00..236.86 rows=43 width=0)
Index Cond: (parent = 126296)
(6 rows)