Improve speed of user list

This commit is contained in:
DariusIII
2026-04-01 15:05:43 +02:00
parent f8d93ea7fd
commit 1aa9ec1785
4 changed files with 95 additions and 45 deletions
@@ -52,17 +52,12 @@ class AdminUserController extends BasePageController
$variables['email'],
$variables['host'],
$variables['role'],
true,
$variables['created_from'],
$variables['created_to']
);
$results = $this->paginate($result, User::getCount($variables['role'], $variables['username'], $variables['host'], $variables['email'], $variables['created_from'], $variables['created_to']), config('nntmux.items_per_page'), $page, $request->url(), $request->query());
// Note: API request counts are already included via the getRange query when $apiRequests = true
// Country lookups and additional counts removed to improve performance on large datasets
// These can be added back via individual user profile pages or AJAX calls if needed
// Build order by URLs
$orderByUrls = [];
foreach ($ordering as $orderType) {
+21 -37
View File
@@ -1293,8 +1293,6 @@ final class User extends Authenticatable implements MustVerifyEmailContract
* Get paginated user list with filters.
*
* @return Collection<int, static>
*
* @throws \Throwable
*/
public static function getRange(
int|false $start,
@@ -1304,47 +1302,33 @@ final class User extends Authenticatable implements MustVerifyEmailContract
?string $email = '',
?string $host = '',
?string $role = '',
bool $apiRequests = false,
?string $createdFrom = '',
?string $createdTo = '',
): Collection {
$order = self::getBrowseOrder($orderBy);
if ($apiRequests) {
UserRequest::clearApiRequests(false);
$query = '
SELECT users.*, roles.name AS rolename, COUNT(user_requests.id) AS apirequests
FROM users
INNER JOIN roles ON roles.id = users.roles_id
LEFT JOIN user_requests ON user_requests.users_id = users.id
WHERE users.id != 0 %s %s %s %s %s %s
AND email != \'sharing@nZEDb.com\'
GROUP BY users.id
ORDER BY %s %s %s';
} else {
$query = '
SELECT users.*, roles.name AS rolename
FROM users
INNER JOIN roles ON roles.id = users.roles_id
WHERE 1=1 %s %s %s %s %s %s
ORDER BY %s %s %s';
}
return static::fromQuery(
sprintf(
$query,
$userName ? 'AND users.username LIKE '.escapeString("%{$userName}%") : '',
$email ? 'AND users.email LIKE '.escapeString("%{$email}%") : '',
$host ? 'AND users.host LIKE '.escapeString("%{$host}%") : '',
$role ? "AND users.roles_id = {$role}" : '',
$createdFrom ? 'AND users.created_at >= '.escapeString("{$createdFrom} 00:00:00") : '',
$createdTo ? 'AND users.created_at <= '.escapeString("{$createdTo} 23:59:59") : '',
$order[0],
$order[1],
$start === false ? '' : "LIMIT {$offset} OFFSET {$start}"
return self::query()
->withTrashed()
->select('users.*')
->selectSub(
'SELECT name FROM roles WHERE roles.id = users.roles_id',
'rolename'
)
);
->excludeSharing()
->when($userName, fn (Builder $q) => $q->where('username', 'like', "%{$userName}%"))
->when($email, fn (Builder $q) => $q->where('email', 'like', "%{$email}%"))
->when($host, fn (Builder $q) => $q->where('host', 'like', "%{$host}%"))
->when($role, fn (Builder $q) => $q->where('roles_id', $role))
->when($createdFrom, fn (Builder $q) => $q->where('created_at', '>=', "{$createdFrom} 00:00:00"))
->when($createdTo, fn (Builder $q) => $q->where('created_at', '<=', "{$createdTo} 23:59:59"))
->withCount([
'requests as daily_api_count' => fn (Builder $q) => $q->where('timestamp', '>', now()->subDay()),
'downloads as daily_download_count' => fn (Builder $q) => $q->where('timestamp', '>', now()->subDay()),
])
->with('roles:id,name')
->orderBy($order[0], $order[1])
->when($start !== false, fn (Builder $q) => $q->offset($start)->limit($offset))
->get();
}
/**
@@ -0,0 +1,72 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
/**
* Add indexes on users columns used for ORDER BY in the admin user list.
*
* The admin user list supports sorting by these columns; without indexes
* the database must filesort the entire table before applying LIMIT.
*/
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
$indexes = [
'ix_users_username' => 'username',
'ix_users_email' => 'email',
'ix_users_host' => 'host',
'ix_users_lastlogin' => 'lastlogin',
'ix_users_apiaccess' => 'apiaccess',
'ix_users_grabs' => 'grabs',
'ix_users_rolechangedate' => 'rolechangedate',
'ix_users_verified' => 'verified',
];
foreach ($indexes as $indexName => $column) {
if (! $this->indexExists('users', $indexName)) {
Schema::table('users', function (Blueprint $table) use ($indexName, $column) {
$table->index($column, $indexName);
});
}
}
}
/**
* Reverse the migrations.
*/
public function down(): void
{
$indexes = [
'ix_users_username',
'ix_users_email',
'ix_users_host',
'ix_users_lastlogin',
'ix_users_apiaccess',
'ix_users_grabs',
'ix_users_rolechangedate',
'ix_users_verified',
];
foreach ($indexes as $indexName) {
if ($this->indexExists('users', $indexName)) {
Schema::table('users', function (Blueprint $table) use ($indexName) {
$table->dropIndex($indexName);
});
}
}
}
private function indexExists(string $table, string $indexName): bool
{
$indexes = DB::select("SHOW INDEX FROM `{$table}` WHERE Key_name = ?", [$indexName]);
return count($indexes) > 0;
}
};
+2 -3
View File
@@ -215,18 +215,17 @@
</td>
<td class="px-6 py-4 whitespace-nowrap">
<span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-blue-100 dark:bg-blue-900 text-blue-800 dark:text-blue-200">
{{ $user->roles->first()->name ?? 'N/A' }}
{{ $user->rolename ?? 'N/A' }}
</span>
</td>
<td class="px-6 py-4 whitespace-nowrap">
@if(!empty($user->pending_roles_id) && !empty($user->pending_role_start_date))
@php
$pendingRole = \Spatie\Permission\Models\Role::find($user->pending_roles_id);
$pendingStartDate = \Carbon\Carbon::parse($user->pending_role_start_date);
@endphp
<div class="flex flex-col gap-1">
<span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-purple-100 dark:bg-purple-900 text-purple-800 dark:text-purple-200 w-fit">
<i class="fas fa-layer-group mr-1"></i>{{ $pendingRole->name ?? 'Unknown' }}
<i class="fas fa-layer-group mr-1"></i>{{ $role_names[$user->pending_roles_id] ?? 'Unknown' }}
</span>
<span class="text-xs text-gray-500 dark:text-gray-400">
<i class="fas fa-clock mr-1"></i>{{ $pendingStartDate->diffForHumans() }}