2

My website is performing very slow and taking lot of time to load the data. Its like 2-3 mins to load the data. Can you please suggest me how to make it fast. I am fetching data from multiple table. The database has many entries almost 25000 entries.

Below Is the Code I am currently using.

<table class="table table-striped table-bordered bootstrap-datatable datatable">
<tbody>
  <?php // get all state
     $sql=" SELECT bm.bank_name,b.bank_ifsc,e.emp_id,e.emp_code,
                      e.first_name,e.middle_name,e.last_name,
                      e.active_status as emp_status,
                      e.account_no FROM tblemployee e 
        Left Join tblbank_mst bm  on bm.bank_id=e.fk_bank_id
        Left Join tblbank b  on b.bank_ifsc_id=e.fk_bank_ifsc_id 
        where e.del_status=0
          and e.role_id=4
          and e.is_admin=0 ";
     if($_SESSION["loggedin_role_id"]==2)
     {
     $sql.=" and  e.added_by=".$_SESSION["loggedin_emp_id"];
     }
     $sql.=" order by first_name";

     // echo $sql; exit;

     if(mysql_num_rows($result = mysql_query($sql))>0)
     { 
     while($row = mysql_fetch_array($result))
     { ?>
  <tr>
     <td><?php echo $row['emp_code'];?> </td>
     <td><?php echo $row['first_name'].' '.$row['middle_name'].' '.$row['last_name'];?> </td>
     <td><?php echo $row['bank_name'];?> </td>
     <td><?php echo $row['account_no'];?> </td>
     <td><?php echo $row['bank_ifsc'];?> </td>
     <td class="center">
        <?php if($row['emp_status']==1){ ?>
        <span class="label label-success">Active</span>
        <?php }else{?>
        <span class="label label-danger">Inactive</span>
        <?php }?>
     </td>
     <td class="center">
        <!--<a class="btn btn-success" href="#">
           <i class="halflings-icon white zoom-in"></i>  
           </a>-->
        <?php if($row['approve_status']==0){ ?>
        <a class="btn btn-info" href="edit_employee.php?emp_id=<?php echo $row['emp_id'];;?>">
        <i class="halflings-icon white edit"></i>  
        </a>
        <a class="btn btn-danger" href="#" onClick="ConfirmDelete(<?php echo $row['emp_id'];?>)">
        <i class="halflings-icon white trash"></i> 
        </a>
        <?php }else{ echo "--"; }?>
     </td>
  </tr>

Rick James
  • 135,179
  • 13
  • 127
  • 222
pratik
  • 25
  • 2
  • 8

4 Answers4

1

Assuming your largest table is tblemployee, try creating a compound index on the three columns mentioned in your WHERE clause, which is:

 WHERE e.del_status=0 and e.role_id=4 and e.is_admin=0

You can do this with

 CREATE INDEX emp_del_role_admin 
           ON tblemployee
              (del_status, role_id, is_admin);

Why does this help? Because MySQL's query planner can random-access the index to find the first row of your table matching your WHERE statement, then it can read the index sequentially to find the rest of the matching rows.

Of course, if your WHERE filter matches many thousands of rows in your table, you will still have a slow page; it takes time to load, transmit, and render a very large page.

O. Jones
  • 103,626
  • 17
  • 118
  • 172
  • Well, that index may work well for the base query, but not so good for the one where `and e.added_by` and/or `order by` are tacked on. – Rick James Dec 14 '17 at 06:18
0

If you are trying to display 25000 (or too many) entries in one time :

This is slow and that's normal, you should paginate your results : use some infinite scroll plugin to display the same thing and limiting results for each query -EDIT : or DataTable pagination options-.

If you are not

First you should have a look at slow queries , configuration to change in my.cnf file. If your query is slow, you may then optimize your query by adding INDEX where you have to. You should use EXPLAIN (documentation) to help you doing this. Be sure you have foreign keys declared as foreign keys (bm.bank_id & e.fk_bank_id - b.bank_ifsc_id & e.fk_bank_ifsc_id), that will speed up your query. Adding index on things like e.role_id could do it aswell.

Only you can know which index to add in this case.

Oulalahakabu
  • 504
  • 3
  • 6
0

Do not use PHP's mysql_* interface; switch to either mysqli_* or PDO.

Add two composite indexes:

(del_status, role_id, is_admin, first_name)
(del_status, role_id, is_admin, added_by, first_name)

The first handles the case where you skip the additional AND.

The following pattern may help performance more: Change

... bm.bank_name,
...
    Left Join  tblbank_mst bm  ON bm.bank_id=e.fk_bank_id

to

... ( SELECT bank_name FROM tblbank_mst WHERE bank_id=e.fk_bank_id
    ) AS bank_name,
...

Ditto for the other LEFT JOIN.

Rick James
  • 135,179
  • 13
  • 127
  • 222
-1

Please see this answer as it might help you: https://stackoverflow.com/a/35757120/1276062

In short you should check if you have indices on theese columns:

  • tblbank_mst.bank_id
  • tblemployee.fk_bank_id
  • tblbank.bank_ifsc_id
  • tblemployee.fk_bank_ifsc_id
  • tblemployee.del_status
  • tblemployee.role_id
  • tblemployee.is_admin
  • tblemployee.added_by
  • first_name

If the indices won't help you should run EXPLAIN on the query and post the results into the question

Kyborek
  • 1,519
  • 11
  • 20
  • 2
    Creating many single-column indexes is generally **harmful** unless those indexes are chosen to accelerate specific queries. – O. Jones Dec 13 '17 at 08:57
  • Furthermore, a single-column index on a "flag" (or other value with low cardinality) will essentially _never_ be used. – Rick James Dec 14 '17 at 06:25
  • @RickJames Could you please provide a link to some source that supports your statement? I would love to educate myself regarding the low cardinality indices. – Kyborek Dec 14 '17 at 13:35
  • @Kyborek - This forum is littered with examples. Usually the title is something like "why is mysql not using my index?". Often, I am one of the answerers, repeating myself. Others chime in. Often there is an concrete example. There are some links. Also, search for cardinality in https://dev.mysql.com/doc/refman/5.7/en/table-scan-avoidance.html – Rick James Dec 14 '17 at 17:59
  • 1
    @Kyborek - and I have tried to digest the main tips into http://mysql.rjweb.org/doc.php/index_cookbook_mysql . This gives more tips: http://mysql.rjweb.org/doc.php/ricksrots – Rick James Dec 14 '17 at 18:01
  • 1
    @Kyborek - to experiment yourself, find (or create) a table where some value of an indexed column occurs in more than, say, 30% of the rows. Then do `EXPLAIN SELECT` to see that the index is not used when it "should". – Rick James Dec 14 '17 at 18:02
  • 1
    @Kyborek - to further experiment, time the `SELECT`, and time the query with `FORCE INDEX(..)`. It _may_ demonstrate that the index makes it slower. (There are many factors going on, so the index _may not_ make it slower.) – Rick James Dec 14 '17 at 18:04