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

GodziLaravel's avatar

Eloquent needs much time than phpmyadmin sql query

hello , first I added to mysql ST_Distance_Sphere() function witch calculate the distance between two coordinates (lat,lon) I created one controller to return the result as json response :

public function index( $from_location)
    {

        $response = station::select('id','name','label','address','longitude','latitude')
            ->whereRaw('ST_Distance_Sphere( POINT(latitude, longitude), POINT( '.$from_location.') ) < 1000 ')
            ->take(50)
             ->get();


        return response()->json($response);
...

this controller load in 5.5 seconds but when I execute it from phpMyAdmin it needs only 0.05 second!!!

Showing rows 0 - 24 (25 total, Query took 0.0500 seconds.)
select `id`, `name`, `label`, `address`, `longitude`, `latitude` from `stations` where ST_Distance_Sphere( POINT(latitude, longitude), POINT( 50.9130988,4.4302298) ) < 1000 limit 50

how to minimize the response 's time ? thanks

0 likes
5 replies
tykus's avatar

Well, you're not comparing like with like - the framework needs to boot, handle the request, build the query, instantiate the model instances and return a response. Try to seek out where the time is being spent by installing laravel-debugbar which will give you at least some rudimentary timings.

jlrdw's avatar

Laravel is going to be a little shower, but not that much.

If you just run the query above and

dd($response);

Does the time improve?

Edit: Could you show the exact query you use in phpmyadmin?

Maybe getPdo() would be faster, not sure.

Snapey's avatar

not to mention dns resolution and web server startup time, but 5.5 seconds is at least 10 x longer than it should be

tykus's avatar

not to mention dns resolution and web server startup time

I assumed this would be similar as phpMyAdmin was likely on the same server.

GodziLaravel's avatar

hello , thanks for your help , when I remove this lien it returns the response in 0.56 s witch is very well:

->whereRaw('ST_Distance_Sphere( POINT(latitude, longitude), POINT( '.$from_location.') ) < 1000 ')

the problem is only with this line, but I dont understand why when I try it on the local server it works fine and also when I run it on phpMyAdmin on the hosting it works good???

Please or to participate in this conversation.