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>
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.