An SQL query keeps locking the database

Support intro

Sorry to hear you’re facing problems :slightly_frowning_face:

help.nextcloud.com is for home/non-enterprise users. If you’re running a business, paid support can be accessed via portal.nextcloud.com where we can ensure your business keeps running smoothly.

In order to help you as quickly as possible, before clicking Create Topic please provide as much of the below as you can. Feel free to use a pastebin service for logs, otherwise either indent short log examples with four spaces:

example

Or for longer, use three backticks above and below the code snippet:

longer
example
here

Some or all of the below information will be requested if it isn’t supplied; for fastest response please provide as much as you can :heart:

Nextcloud version (eg, 29.0.5): Nextcloud Hub 8 (29.0.9)
Operating system and version (eg, Ubuntu 24.04): Debian Bookworm
Apache or nginx version (eg, Apache 2.4.25): 2.4.62-1~deb12u1
PHP version (eg, 8.3): 8.2.20

The issue you are facing:

Nextcloud executes an SQL query every few minutes that locks up the database for minutes at a time.

This is an old Nextcloud install that I’ve kept up to date for several years. it’s just something I run for friends and family.

As you can see from the screenshots included, I have 4 virtual cores devoted to my database VM. While this query is executing, one of the cores is pegged at 100%

See screenshot for the query being executed. I should mention, the table “oc_filecache” has several million rows.

I really only understand the very basics of SQL so I’m not sure what I can do to improve the performance of this query.

Is this the first time you’ve seen this error? (Y/N): It’s something that’s gotten worse over time as the oc_filecache table has grown.

Steps to replicate it:

  1. Have a nextcloud server with millions of files, i guess. not sure.

The output of your Nextcloud log in Admin > Logging:

There doesn't seem to be any relevant log output at the warn level.

The output of your config.php file in /path/to/nextcloud (make sure you remove any identifiable information!):

<?php
$CONFIG = array (
  'instanceid' => 'REDACTED',
  'passwordsalt' => 'REDACTED',
  'secret' => 'REDACTED',
  'trusted_domains' => 
  array (
    0 => 'REDACTED',
    1 => 'REDACTED',
    2 => 'REDACTED',
  ),
  'datadirectory' => '/home/ocdata/',
  'overwrite.cli.url' => 'REDACTED',
  'dbtype' => 'mysql',
  'version' => '29.0.9.2',
  'dbname' => 'nextcloud',
  'dbhost' => 'REDACTED-databaseVM',
  'dbtableprefix' => 'oc_',
  'dbuser' => 'REDACTED',
  'dbpassword' => 'REDACTED',
  'logtimezone' => 'UTC',
  'installed' => true,
  'default_phone_region' => 'US',
  'mail_smtpmode' => 'sendmail',
  'mail_smtpauthtype' => 'LOGIN',
  'mail_from_address' => 'admin',
  'mail_domain' => 'REDACTED',
  'loglevel' => 0,
  'memcache.local' => '\\OC\\Memcache\\APCu',
  'memcache.locking' => '\\OC\\Memcache\\Redis',
  'redis' => 
  array (
    'host' => '/var/run/redis/redis-server.sock',
    'port' => 0,
    'dbindex' => 0,
    'timeout' => 1.5,
  ),
  'filelocking.enabled' => 'true',
  'htaccess.RewriteBase' => '/',
  'appstore.experimental.enabled' => true,
  'maintenance' => false,
  'theme' => '',
  'has_rebuilt_cache' => true,
  'mail_sendmailmode' => 'smtp',
  'simpleSignUpLink.shown' => false,
  'twofactor_enforced' => 'true',
  'twofactor_enforced_groups' => 
  array (
    0 => 'admin',
    1 => 'registered',
    2 => 'Users',
  ),
  'twofactor_enforced_excluded_groups' => 
  array (
  ),
  'mysql.utf8mb4' => true,
  'updater.release.channel' => 'stable',
  'enforce_theme' => '',
  'maintenance_window_start' => 3,
);

The output of your Apache/nginx/system log in /var/log/____:

redacted log provided if needed. I dont see any relevant entries

Output errors in nextcloud.log in /var/www/ or as admin user in top right menu, filtering for errors. Use a pastebin service if necessary.

There doesn't seem to be any relevant log output at the warn level.

I would start digging into this point. Verify if this table contains obsolete entries. This could happen if you had external storage in the past, moved data directory etc. If multiple user mount the same external storage like SMB individually files appear multiplied.

we had quite few discussions about oc_filecache size and e.g. How to shrink oc_filecache (440GB, vs 360GB files hosted) review and check if you find any obvious problem like files on old storages still stored in the tables and try to recover.

My nextcloud instance has 5.7 million files (at least according to the statistics in settings>system), so I should expect that table to be quite large, Right?