Perfomance Issue with Granular Permissions

Hi Everyone!

First of all, thank you whom is investing time in Metabase.
We are facing long request associated with granular permissions. For example, there is a page with path like admin/permissions/data/group/<number>/database/<number> which triggers api/database/<number>/metadata?include_hidden=true&remove_inactive=true and it takes more than more than 2 minutes.

Could anyone please help to understand is low perfomance caused by non optimised code somewhere or it is something wrong with configuration on our side?

Mentioned issue is appearing on page with Granular Permissions and with open source version v0.49.7.
Please let me know, if I need to provide more data.

Diagnostic Info:

{
  "browser-info": {
    "language": "en-US",
    "platform": "MacIntel",
    "userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36",
    "vendor": "Google Inc."
  },
  "system-info": {
    "file.encoding": "UTF-8",
    "java.runtime.name": "OpenJDK Runtime Environment",
    "java.runtime.version": "11.0.22+7",
    "java.vendor": "Eclipse Adoptium",
    "java.vendor.url": "https://adoptium.net/",
    "java.version": "11.0.22",
    "java.vm.name": "OpenJDK 64-Bit Server VM",
    "java.vm.version": "11.0.22+7",
    "os.name": "Linux",
    "os.version": "5.10.198-187.748.amzn2.x86_64",
    "user.language": "en",
    "user.timezone": "GMT"
  },
  "metabase-info": {
    "databases": [
      "redshift",
      "postgres"
    ],
    "hosting-env": "unknown",
    "application-database": "postgres",
    "application-database-details": {
      "database": {
        "name": "PostgreSQL",
        "version": "14.10"
      },
      "jdbc-driver": {
        "name": "PostgreSQL JDBC Driver",
        "version": "42.7.2"
      }
    },
    "run-mode": "prod",
    "version": {
      "date": "2024-04-22",
      "tag": "v0.49.7",
      "hash": "f0ff786"
    },
    "settings": {
      "report-timezone": null
    }
  }
}

How many Groups and Databases do you have setup?

There are 24 Groups ups and 22 Databases.
Also, based on my experience if a group has granular permissions, it works slower.
On example above, group has granular permissions on one DB. Mentioned DB contains 21 schemas and three schemas have granular permissions options.