I am using the Laravel PHP Framework.
Say I have several queries like this:
public function order($orderby){ \DB::connection()->disableQueryLog(); if($orderby == "level"){ $clan = Clans::orderBy('level', 'DESC') ->orderBy('exp', 'DESC') ->paginate(100,['id', 'clanid', 'name', 'level', 'exp', 'warwinpercent', 'warswon', 'playercount', 'score']); }elseif($orderby == "score"){ $clan = Clans::orderBy('score', 'DESC') ->paginate(100,['id', 'clanid', 'name', 'level', 'exp', 'warwinpercent', 'warswon', 'playercount', 'score']); }elseif($orderby == "warwinpercent"){ $clan = Clans::orderBy('warwinpercent', 'DESC') ->where('warswon', '>=', '100') ->paginate(100,['id', 'clanid', 'name', 'level', 'exp', 'warwinpercent', 'warswon', 'playercount', 'score']); }else $clan = Clans::paginate(100,['id', 'clanid', 'name', 'level', 'exp', 'warwinpercent', 'warswon', 'playercount', 'score']); \DB::connection()->enableQueryLog(); return view('clans.index')->with('clan', $clan); }
It takes about 10-15 seconds.
I have one such:
public function index(){ $clan = Clans::orderBy('clanid', 'ASC') ->paginate(100,['id', 'clanid', 'name', 'level', 'exp', 'warwinpercent', 'warswon', 'playercount', 'score']); return view('clans.index')->with('clan', $clan); }
Which loads almost instantly.
Why do the first 3 take a lot longer than the last? I added an index for all the variables that I need to search in my table, but it still requires age. Do I need to do anything on the MySql side to rebuild the index? I have already tried optimizing the table, and I restart the MySql service again.
If it's not possible to speed them up, is there an easy way to show the loading animation to the user when the page loads?
Thanks!
UPDATE
Here is the result of an explanation for a quick query:
explain extended select `id`, `clanid`, `name`, `level`, `exp`, `warwinpercent`, `warswon`, `playercount`, `score` from `clans` order by `clanid` asc limit 100 offset 0

Here is the result of the explanation for the orderBy level query.
explain extended select `id`, `clanid`, `name`, `level`, `exp`, `warwinpercent`, `warswon`, `playercount`, `score` from `clans` order by `level` desc, `exp` desc limit 100 offset 0

Here is the result of SHOW INDEX FROM clans . 
UPDATE 2
I also have this code that searches for a row in the name column
public function clans(){ if((isset($_GET['search'])) && $_GET['search'] != ""){ $search = $_GET['search']; $result = Clans::where('name', 'LIKE', '%'.$search.'%')->paginate(100,['id', 'clanid', 'name', 'level', 'exp', 'warwinpercent', 'warswon', 'playercount', 'score']); $data = array( 'result' => $result, 'search' => $search ); return view('search.clans')->with($data); }else return view('errors.1')->with('message', 'You entered an invalid search term'); }
This may take a few seconds.
Explain the request:
explain extended select `id`, `clanid`, `name`, `level`, `exp`, `warwinpercent`, `warswon`, `playercount`, `score` from `clans` where `name` LIKE '%lol%' limit 100 offset 0
