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

imposition's avatar

Query on my model taking too much time to finish.

Hi there! I have this query inside my model:

            $data = TopFiber::select(DB::raw('UserName, sum(AcctInputOctets) as Input , sum(AcctOutputOctets) as Output'))
                    ->where('AcctStartTime' , 'LIKE', $data['date'] . '%')->groupBy('UserName')->orderBy('Input', 'desc')->limit(20)->get();

But this is taking too much time like 5 minutes to complete or so. Can i do this any other way to speed up the query ? I need this exactly like it is.

0 likes
9 replies
shez1983's avatar

instead of get(); do toSql() to dump out the query and post it here (and also run in mysql directly)

Slow query is usually a symptom of no indexes in your table..

also for dates you dont do ->where('AcctStartTime' , 'LIKE', $data['date'] . '%') you canuse the '=' instead..

2 likes
robrogers3's avatar

what @shez1983 says. plus do an explain.

As in Explain select * from foo.

see if it's using indexes.

determine what's the date where clause is about. likes are expensive.

and yeah if AcctStartTime is a DateField then that's a problem.

1 like
imposition's avatar

AcctStartTime is the date, I think This is taking too much time cus i have over 50.000 entries to This table

mattsplat's avatar

Usually when my queries take that long it's because I'm not using indexes on the fields I am searching by. If you don't have an index on 'AcctStartTime' you should try creating one. To check what fields have indexes from tinker you can use

 DB::select('show index from table-name')
1 like
imposition's avatar

@mattsplat

   {#766
       +"Table": "radacct",
       +"Non_unique": 1,
       +"Key_name": "AcctStartTime",
       +"Seq_in_index": 1,
       +"Column_name": "AcctStartTime",
       +"Collation": "A",
       +"Cardinality": 8576707,
       +"Sub_part": null,
       +"Packed": null,
       +"Null": "",
       +"Index_type": "BTREE",
       +"Comment": "",
       +"Index_comment": "",
1 like
imposition's avatar

@mattsplat Added index to AcctStartTime and its getting a little less longer .... But not less enough

mattsplat's avatar
Level 8

Is AcctStartTime a datetime? Like @shez1983 said you shouldn't use like with a date. The reason I believe is it requires additional conversion on every row and doesn't allow the index to work. Try using.

->whereDate('AcctStartTime' ,  $data['date'] )

or maybe

->whereBetween('AcctStartTime' ,[ $data['date'] . '00:00:00',  $data['date']. ' 23:59:59'])
imposition's avatar

@mattsplat The AcctStartTime is a date. I'm searching through my DB with just the year and the month. After reading what you both said i did the code like this:

            $data = TopFiber::select(DB::raw('UserName, sum(AcctInputOctets) as Input , sum(AcctOutputOctets) as Output'))
                    ->whereDate('AcctStartTime' , $data['date'])->groupBy('UserName')->orderBy('Output', 'desc')->limit(20)->get();

And is taking a lot of time yet

imposition's avatar

Changed it to where between and added a daterangepicker instead of datepicker

Please or to participate in this conversation.