快速加入和过滤mysql上多个表的数据

时间:2017-07-10 10:25:42

标签: php mysql

首先,我很抱歉我的英语不好......我开发了一个php应用程序来为我的办公室输入商家访问结果。

这是我的mysql数据库的结构:

merchant

id, name, address, phone, area

device

id, merchid, tid, sn, status

visit

id, date, time, merchid, deviceid, act, result

这3个表的关系如下:

device.merchid -> merchant.id
visit.merchid -> merchant.id
visit.deviceid -> device.id

我想要的是将3个表作为包含波纹管字段的视图加入:

查看joindevice

devid, merchid, merchname, merchaddr, merchph, mercharea, devtid, devsn,
devstat, lastact, lastvisit, lastresult

其中lastact,lastresult和lastvisit使用来自'访问'的数据。每个设备的表。 我现在所做的是使用下面的查询创建mysql视图:

select
device.id AS devid,
device.merchid AS merchid,
merchant.name AS merchname,
merchant.address AS merchaddr,
merchant.phone AS merchph,
merchant.area AS mercharea,
device.tid AS devtid,
device.sn AS devsn,
device.status AS devstat,
ifnull(
(
select visit.act from visit
where visit.devid = device.id
order by visit.date desc limit 1
),''
)AS lastact,
ifnull(
(
select visit.date from visit
where (visit.devid=device.id)
order by visit.date desc limit 1
),''
)AS lastvisit,
ifnull(
(
select visit.result from visit
where (visit.devid=device.id)
order by visit.date desc limit1
),''
)AS lastresult
from (device join merchant) where (merchant.id=device.merchid)

这里是调用JoinDevice视图的PHP脚本:

<?php
$sql = "SELECT * FROM joindevice order by merchname asc";
$qry=mysql_query($sql);
$check=mysql_num_rows($qry);
if($check>0){
?>
<table>
<thead>
<tr>
<th>No.</th>
<th>TID</th>
<th>SN</th>
<th>Phone</th>
<th>Merchant Name</th>
<th>Merchant Address</th>
<th>Merchant Area</th>
<th>Device Status</th>
<th>Last Visit</th>
<th>Last Act</th>
<th>Last Result</th>
</tr>
</thead>
<tbody>
<?php
$no=1;
while($dt=mysql_fetch_assoc($qry)){
?>
<tr>
<td><?php echo $no; ?></td>
<td><?php echo empty($dt['tid'])?'-':$dt['tid']; ?></td>
<td><?php echo empty($dt['sn'])?'-':$dt['sn']; ?></td>
<td><?php echo empty($dt['merchph'])?'-':$dt['merchph']; ?></td>
<td><?php echo empty($dt['merchname'])?'-':$dt['merchname']; ?></td>
<td><?php echo empty($dt['merchaddr'])?'-':$dt['merchaddr']; ?></td>
<td><?php echo empty($dt['mercharea'])?'-':$dt['mercharea']; ?></td>
<td><?php echo empty($dt['devstat'])?'-':$dt['devstat']; ?></td>
<td><?php echo empty($dt['lastvisit'])?'-':$dt['lastvisit']; ?></td>
<td><?php echo empty($dt['lastact'])?'-':$dt['lastact']; ?></td>
<td><?php echo empty($dt['lastresult'])?'-':$dt['lastresult']; ?></td>
</tr>
<?php
$no++;
}
?>
</tbody>
</table>
<?php
}
?>

当“设备”中的数据一切正常时,一切正常表很小但是现在数据大约是1700项,所以执行那个php脚本的时间太长了,有时当我执行那个php脚本时结果是网关超时,但是当我执行php脚本时只能从一个表中获取数据它打开快......任何人都可以帮我减少执行时间吗?我该怎么办?

1 个答案:

答案 0 :(得分:0)

你可以尝试删除那些相关的子查询并用正式连接替换它们:

SELECT
    t1.id AS devid,
    t1.merchid AS merchid,
    t1.tid AS devtid,
    t1.sn AS devsn,
    t1.status AS devstat,
    t2.act AS lastact,
    t2.date AS lastvisit,
    t2.result AS lastresult,
    t3.name AS merchname,
    t3.address AS merchaddr,
    t3.phone AS merchph,
    t3.area AS mercharea,
FROM device t1
INNER JOIN
(
    SELECT t1.*
    FROM visit t1
    INNER JOIN
    (
        SELECT devid, MAX(date) AS date
        FROM visit
        GROUP BY devid
    ) t2
        ON t1.devid = t2.devid AND
           t1.date  = t2.date
) t2
    ON t1.id = t2.devid
INNER JOIN merchant t3
    t1.merchid = t3.id

索引是否证明对上述内容有益取决于许多事情,但无论如何我希望改进原始查询。