Performance
Loading performance is key to streamlining your operations. Here are a few steps we recommend taking to ensure your Forest is optimized.
Please find here all the hands-on best practices to keep your admin panel performant. Depending on your user's needs, you might either hide or optimize some fields to limit the number of components, avoid a large datasets display or rework complex logic.
You can display bellow performances improvement tricks in this video. For any further help to improve admin panel performances, get in touch with the community.

Layout optimization

1. Show only Smart fields
As you can see in the Loading time benchmark below, Smart fields can be quite costly in terms of loading performance. Limiting them to those you need is key.
2. Reduce the number of records per page
3. Reduce the number of fields displayed
You can hide some fields in your table view; this will not prevent you from seeing them in the record details view.
Relationship fields are links to other collection records within your table view:
Having Relationship fields can decrease your performance, especially if your tables have a lot of records. Therefore you should display only those you need and use!

Optimize smart fields performance

To optimize your smart field performances, please check out this section.

Restrict search on specific fields

Sometimes, searching in all fields is not relevant and may even result in big performance issues. You can restrict your search to specific fields only using the searchFields option.
SQL
Mongodb
Rails
In this example, we configure Forest Admin to only search on the fields name and industry of our collection companies.
/forest/companies.js
1
const { collection } = require('forest-express-sequelize');
2
3
collection('companies', {
4
searchFields: ['name', 'industry'],
5
});
Copied!
In this example, we configure Forest to only search on the fields name and industry of our collection companies.
/forest/companies.js
1
const { collection } = require('forest-express-mongoose');
2
3
collection('companies', {
4
searchFields: ['name', 'industry'],
5
});
Copied!
In this example, we configure Forest to only search on the fields name and industry of our collection Company.
/lib/forest_liana/collections/company.rb
1
class Forest::Company
2
include ForestLiana::Collection
3
4
collection :Company
5
6
search_fields ['name', 'industry']
7
8
action 'Mark as Live'
9
10
# ...
11
end
Copied!

Disable pagination count

This feature is only available if you're using the forest-express-sequelize (v8.5.3+), forest-express-mongoose (v8.6.5+), forest-rails (v7.5.0+) or django-forestadmin (v1.2.0+) agent.
To paginate tables properly, Forest Admin triggers a separate request to fetch the number of records.
In certain conditions, usually, when your database reaches a point where it has a lot of records, this request can decrease your loading performance. In this case, you can choose to disable it...
SQL
Mongodb
Rails
Django
Laravel
  • adding the deactivateCountMiddleware like so:
/routes/books.js
1
const { PermissionMiddlewareCreator, deactivateCountMiddleware } = require('forest-express-sequelize');
2
3
...
4
5
// Get a number of Books
6
router.get('/books/count', deactivateCountMiddleware);
Copied!
  • adding the deactivateCountMiddleware like so:
/routes/books.js
1
const { PermissionMiddlewareCreator, deactivateCountMiddleware } = require('forest-express-sequelize');
2
3
...
4
5
// Get a number of Books
6
router.get('/books/count', deactivateCountMiddleware);
Copied!
  • creating a controller in the repository lib/forest_liana/controllers for override the count action
1
class Forest::BooksController < ForestLiana::ResourcesController
2
def count
3
deactivate_count_response
4
end
5
end
Copied!
  • adding a route in app/config/routes.rb before mount ForestLiana::Engine => '/forest'
1
namespace :forest do
2
get '/Book/count' , to: 'books#count'
3
end
Copied!
..adding the following middleware in settings.py and set the collection(s) to deactivate.
myproject/settings.py
1
MIDDLEWARE = [
2
'django_forest.middleware.DeactivateCountMiddleware',
3
# ...
4
]
5
6
# To deactivate the count on /apps_books/count
7
FOREST = {
8
# ...,
9
DEACTIVATED_COUNT = [
10
'apps_books', # apps_model
11
],
12
# ...
13
}
Copied!
app/Http/Controllers/BooksController.php
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use ForestAdmin\LaravelForestAdmin\Facades\JsonApi;
6
use ForestAdmin\LaravelForestAdmin\Http\Controllers\ResourcesController;
7
use Illuminate\Http\JsonResponse;
8
9
class BooksController extends ResourcesController
10
{
11
public function callAction($method, $parameters)
12
{
13
$parameters['collection'] = 'Book';
14
return parent::callAction($method, $parameters);
15
}
16
17
public function count(): JsonResponse
18
{
19
return JsonApi::deactivateCountResponse();
20
}
21
}
Copied!
adding a route in app/routes/web.php
routes/web.php
1
<?php
2
3
use App\Http\Controllers\BooksController;
4
use Illuminate\Support\Facades\Route;
5
6
Route::get('forest/book/count', [BooksController::class, 'count']);
Copied!
To disable the count request in the table of a relationship (Related data section):
SQL
Mongodb
Rails
Django
Laravel
/routes/books.js
1
router.get('/books/:recordId/relationships/companies/count', deactivateCountMiddleware);
Copied!
/routes/books.js
1
router.get('/books/:recordId/relationships/companies/count', deactivateCountMiddleware);
Copied!
1
class Forest::BookCompaniesController < ForestLiana::AssociationsController
2
def count
3
if (params[:search])
4
params[:collection] = 'Book'
5
params[:association_name] = 'company'
6
super
7
else
8
deactivate_count_response
9
end
10
end
11
end
Copied!
1
namespace :forest do
2
get '/Book/:id/relationships/companies/count' , to: 'book_companies#count'
3
end
Copied!
myproject/settings.py
1
MIDDLEWARE = [
2
'django_forest.middleware.DeactivateCountMiddleware',
3
# ...
4
]
5
6
# To deactivate the count on /apps_books/<pk>/company/count
7
FOREST = {
8
# ...,
9
DEACTIVATED_COUNT = [
10
'apps_books:company', # apps_model:related_field
11
],
12
# ...
13
}
Copied!
Furthermore, if you want to disable on all relationships at once:
1
# To deactivate the count on all the related data of the apps_book model
2
FOREST = {
3
# ...,
4
DEACTIVATED_COUNT = [
5
'apps_books:*', # apps_model:*
6
],
7
# ...
8
}
Copied!
app/Http/Controllers/BookCompaniesController.php
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use ForestAdmin\LaravelForestAdmin\Facades\JsonApi;
6
use ForestAdmin\LaravelForestAdmin\Http\Controllers\RelationshipsController;
7
use Illuminate\Http\JsonResponse;
8
9
class BookCompaniesController extends RelationshipsController
10
{
11
public function callAction($method, $parameters)
12
{
13
$parameters['collection'] = 'Book';
14
$parameters['association_name'] = 'companies';
15
return parent::callAction($method, $parameters);
16
}
17
18
public function count(): JsonResponse
19
{
20
if (request()->has('search')) {
21
return JsonApi::deactivateCountResponse();
22
} else {
23
return parent::count();
24
}
25
}
26
}
Copied!
routes/web.php
1
<?php
2
3
use App\Http\Controllers\BookCompaniesController;
4
use Illuminate\Support\Facades\Route;
5
6
Route::get('forest/book/{id}/relationships/companies/count', [BookCompaniesController::class, 'count']);
Copied!
You can also disable the count request in a collection only in certain conditions. For instance, you can disable the count if you're using a filter:
SQL
Mongodb
Rails
Django
Laravel
/routes/books.js
1
// Get a number of Books when you have a filtering
2
router.get('/books/count', (request, response, next) => {
3
if (request.query.filters) {
4
next(); // count will be done
5
} else {
6
deactivateCountMiddleware(request, response);
7
}
8
});
Copied!
/routes/books.js
1
// Get a number of Books when you have a filter
2
router.get('/books/count', (request, response, next) => {
3
if (request.query.filters) {
4
next(); // count will be done
5
} else {
6
deactivateCountMiddleware(request, response);
7
}
8
});
Copied!
1
class Forest::BooksController < ForestLiana::ResourcesController
2
def count
3
if (params[:filters])
4
params[:collection] = 'Book'
5
super
6
else
7
deactivate_count_response
8
end
9
end
10
end
Copied!
myproject/myapp/middlewares.py
1
class CustomDeactivateCountMiddleware(DeactivateCountMiddleware):
2
3
def is_deactivated(self, request, view_func, *args, **kwargs):
4
is_deactivated = super().is_deactivated(request, view_func, *args, **kwargs)
5
return is_deactivated and 'search' not in request.GET
Copied!
myproject/settings.py
1
MIDDLEWARE = [
2
'myproject.myapp.middlewares.CustomDeactivateCountMiddleware',
3
# ...
4
]
5
6
# To deactivate the count on /apps_books/count if there is no search argument
7
FOREST = {
8
# ...,
9
DEACTIVATED_COUNT = [
10
'apps_books', # apps_model
11
],
12
# ...
13
}
Copied!
app/Http/Controllers/BooksController.php
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use ForestAdmin\LaravelForestAdmin\Facades\JsonApi;
6
use ForestAdmin\LaravelForestAdmin\Http\Controllers\ResourcesController;
7
use Illuminate\Http\JsonResponse;
8
9
class BooksController extends ResourcesController
10
{
11
public function callAction($method, $parameters)
12
{
13
$parameters['collection'] = 'Book';
14
return parent::callAction($method, $parameters);
15
}
16
17
public function count(): JsonResponse
18
{
19
if (request()->has('filters')) {
20
return parent::count();
21
} else {
22
return JsonApi::deactivateCountResponse();
23
}
24
}
25
}
Copied!
One more example: you may want to deactivate the pagination count request for a specific team:
SQL
Mongodb
Rails
Laravel
1
router.get('/books/count', (request, response, next) => {
2
// Count is deactivated for the Operations team
3
if (request.user.team === 'Operations') {
4
deactivateCountMiddleware(request, response);
5
// Count is made for all other teams
6
} else {
7
next();
8
}
9
});
Copied!
1
router.get('/books/count', (request, response, next) => {
2
// Count is deactivated for the Operations team
3
if (request.user.team === 'Operations') {
4
deactivateCountMiddleware(request, response);
5
// Count is made for all other teams
6
} else {
7
next();
8
}
9
});
Copied!
1
class Forest::BooksController < ForestLiana::ResourcesController
2
def count
3
if forest_user['team'] == 'Operations'
4
deactivate_count_response
5
else
6
params[:collection] = 'Book'
7
super
8
end
9
end
10
end
Copied!
app/Http/Controllers/BooksController.php
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use ForestAdmin\LaravelForestAdmin\Facades\JsonApi;
6
use ForestAdmin\LaravelForestAdmin\Http\Controllers\ResourcesController;
7
use Illuminate\Http\JsonResponse;
8
use Illuminate\Support\Facades\Auth;
9
10
class BooksController extends ResourcesController
11
{
12
public function callAction($method, $parameters)
13
{
14
$parameters['collection'] = 'Book';
15
return parent::callAction($method, $parameters);
16
}
17
18
public function count(): JsonResponse
19
{
20
if (Auth::guard('forest')->user()->getAttribute('team') === 'Operations') {
21
return JsonApi::deactivateCountResponse();
22
} else {
23
return parent::count();
24
}
25
}
26
}
Copied!

Database Indexing

Indexes are a powerful tool used in the background of a database to speed up querying. It power queries by providing a method to quickly lookup the requested data. As Forest Admin generates SQL queries to fetch your data, creating indexes can improve the query response time.
5. Index the Primary and Unique Key Columns
The syntax for creating an index will vary depending on the database. However, the syntax typically includes a CREATE keyword followed by the INDEX keyword and the name we’d like to use for the index. Next should come the ON keyword followed by the name of the table that has the data we’d like to quickly access. Finally, the last part of the statement should be the name(s) of the columns to be indexed.
1
CREATE INDEX <index_name>ON <table_name> (column1, column2, ...)
Copied!
For example, if we would like to index phone numbers from a customers table, we could use the following statement:
1
CREATE INDEX customers_by_phoneON customers (phone_number)
Copied!
The users cannot see the indexes, they are just used to speed up searches/queries.
6. Index the Foreign Key Columns
Foreign key columns should be indexed if they are used intensively in Smart fields. In the table below, you can see how drastically it reduces the loading time of the page.
Updating a table with indexes takes more time than updating a table without (because the indexes also need an update). So, only create indexes on columns that will be frequently searched against.

Loading time benchmark

Below is the outcome of a performance test on page load time of the Table view. It highlights the importance of using indexes and limiting the number of columns and lines.