Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sarfaraz1212's avatar

Inertia React app is slow

I am building a basic project which is not at all complex with React and laravel using inertia but my pages where there is something related to query are loading very slow, even the log/signup provided by breeze is a bit slow.

can anyone suggest me how can i make my app faster?

public function index()
    {
        $query = Task::query();

        $sortField     =  request("sort_field","created_at");
        $sortDirection =  request("sort_direction","asc");

        if(request('name'))
        {
            $query->where("name","like","%".request("name")."%");
        }

        if(request('status'))
        {
            $query->where("status","like","%".request("status")."%");
        }

        $tasks = $query->orderBy($sortField,$sortDirection)->paginate(10)->onEachSide(1);
        
        return inertia("Task/index",
        [
            "tasks"    => TaskResource::collection($tasks),
            "queryParams" => request()->query() ?: null,
        ]);
    }

this is my page it is rendering like 50-60 records.

0 likes
17 replies
martinbean's avatar

@sarfaraz1212 So what have you actually done to try and diagnose the issue…?

Use the browser’s developer console to look at the network requests. Install something like Laravel Debugbar or Laravel Telescope to see if you’re running a lot of database queries on each page load or something.

You can only fix something once you diagnose the problem.

martinbean's avatar

@sarfaraz1212 Quite possibly. So measure database query times to determine whether it is or not.

If you had installed one of the packages I suggested in my last comment, they will show you the execution time for queries.

JussiMannisto's avatar

When you're doing a LIKE query with a wildcard at the start, no index can be used. That really kills performance as the entire table has to be scanned.

You should look into using a full text search engine or just adding full text indexes and matching against those.

Your problem might be elsewhere, but this is a clear performance issue.

Tray2's avatar

@sarfaraz1212 You could clean that up a little by using when instead of if

   return view('books.index')
            ->with([
                'books' => BookIndexView::query()
                    ->when($request['authors'], function ($query, $authors) {
                        $query->whereIn('author_id',
                            $this->numericStringToArray($authors));
                    })
                    ->when($request['published'], function ($query, $published) {
                        $query->where('published_year', $published);
                    })
                    ->when($request['genre'], function ($query, $genre) {
                        $query->where('genre', $genre);
                    })
                    ->when($request['format'], function ($query, $format) {
                        $query->where('format', $format);
                    })
                    ->when($request['search'], function ($query, $search) {
                        $query->where('title', 'LIKE',  "$search%")
                        ->orWhere('author_name', 'LIKE', "$search%")
                        ->orWhere('series', 'LIKE', "$search%");
                    })
                    ->orderBy('author_name')
                    ->orderBy('series')
                    ->orderBy('part')
                    ->orderBy('published_year')
                    ->get(),
            ]);
```	
sarfaraz1212's avatar

@Tray2

public function index()
    {
        
        $query = Task::with(['project', 'assignedUser', 'createdBy', 'updatedBy']);

        
        $sortField = request("sort_field", "created_at");
        $sortDirection = request("sort_direction", "asc");

        if ($name = request('name')) {
            $query->where('name', 'LIKE', $name . '%');
        }

    
        if ($status = request('status')) {
            $query->where('status', 'LIKE', $status . '%');
        }

    
        $tasks = $query->orderBy($sortField, $sortDirection)
                    ->paginate(10)
                    ->onEachSide(1);

        return inertia("Task/index", [
            "tasks" => TaskResource::collection($tasks),
            "queryParams" => request()->query() ?: null,
        ]);
    } 

this helped me to speed up the query. Previosuly the execution time was above 3 seconds , now it is 1 or below 1

sarfaraz1212's avatar

@Tray2

 $table->string('name')->index();
 $table->enum('status', ['pending', 'in_progress', 'completed'])->index();
 $table->unsignedBigInteger('created_by')->index(); 
sarfaraz1212's avatar

@Tray2

i was using remote DB that's why it was slow. Thankyou for the help it made my syntax and query better

nikikoshti's avatar

One of the most common causes of slow pages is inefficient database queries. Check your queries for the following:

N+1 Query Problem: Ensure you're not running extra queries in loops (for example, loading related models without eager loading). Use Laravel's with() method for eager loading relationships.

php // Eager load relationships User::with('profile', 'posts')->get();

Database Indexing: Ensure that the columns you're filtering or sorting by are indexed. This can significantly speed up query execution.

Query Optimization: Use EXPLAIN in MySQL to analyze slow queries and optimize them. Avoid selecting unnecessary columns.

php // Avoid unnecessary columns User::select('id', 'name', 'email')->get();

Please or to participate in this conversation.