I have the following PHP code in Laravel is executing a MySql query:
DB::statement(DB::raw("SET @rownum = 0"));
$sql = "@rownum := @rownum+1 AS rank, id, clanid, name, location, level, exp, warslost, warstied, warwinpercent, warswon, playercount, score";
$clan = Clans::orderBy('clanid', 'asc')
->selectRaw($sql)
->take(7000)
->get();
This query is taking a long time to execute.
I have indexes on the columns I am ordering by as well as many of the other ones for other queries.
What should I do?
Update:
Queries executed:
SET @rownum = 0
select @rownum := @rownum+1 AS rank, id, clanid, name, location, level, exp, warslost, warstied, warwinpercent, warswon, playercount, score from `clans` order by `clanid` asc limit 7000
EXPLAIN EXTENDED
result: