如何以更好的方式使用mysql来缩短加载时间?

时间:2019-02-14 16:49:20

标签: php mysql

我有一个php脚本,它从6个不同的数据库中收集信息。而且,如果该列表包含50个项目,那么我将有大约50-200个查询可以在while循环中获取所有项目的所有信息。

我今天尝试在PHP中使用不同的“ function()”以在所有项目上使用,而且我尝试在MySQL中进行内部联接,但没有任何结果。而且我所有的数据库都有类似的名称,例如ID,设备等。

我用于查看页面的PHP代码(稍作改动,只是为了展示我的想法):

<?php
if(!empty($_GET['tel_sort'])) {
    global $conn; //SQL connection
    global $device_kost;
    $tel_sort = $_GET['tel_sort'];
    $result = mysqli_query($conn,"SELECT * FROM $device_kost WHERE id = $tel_sort ORDER BY prod, model");
} elseif(!empty($_GET['prod_sort'])) {
    global $conn; //SQL connection
    global $device_kost;
    $prod_sort = $_GET['prod_sort'];
    $result = mysqli_query($conn,"SELECT * FROM $device_kost WHERE prod = '$prod_sort' AND priv = 1 ORDER BY prod, model");
} else {
    $result = mysqli_query($conn,"SELECT * FROM $device_kost WHERE priv = 1 ORDER BY prod, model");
}

$telesort = mysqli_query($conn,"SELECT * FROM $device_kost WHERE priv = 1 ORDER BY prod, model");
$prodsort = mysqli_query($conn,"SELECT * FROM $device_kost WHERE priv = 1 GROUP BY prod ORDER BY prod");

function ab($tel) {
    global $conn;
    global $device_max;
    $telque = mysqli_query($conn,"SELECT * FROM $device_max WHERE device = '$tel'");
    $telres = mysqli_fetch_assoc($telque);
    return $telres;
}

function ts($tel) {
    global $conn;
    global $device_ts;
    $current_date = date("Y-m-d");
    $telque = mysqli_query($conn,"SELECT * FROM $device_ts  WHERE device = '$tel' AND date_f <= '$current_date' AND date_t >= '$current_date'");
    $telres = mysqli_fetch_assoc($telque);
    return $telres;
}

function device($tel) {
    global $conn;
    global $device_kost;
    $telque = mysqli_query($conn,"SELECT * FROM $device_kost WHERE type = '1'");
    $telres = mysqli_fetch_assoc($telque);
    return $telres;
}

function color($price, $kost, $ukon, $ts, $ab = NULL) {
    if(!isset($price)) { $priceumoms="0"; } else { $priceumoms = $price; }
    $priceumoms = $priceumoms / 1.25;
    if($ts=="n/a" || empty($ts)) { $ts="0"; }
    $totpris = $priceumoms + $ts;
    $db = $totpris - $kost;
    $dg = $db * 100;
    if ($totpris == 0) {
        $dg = "";
    } else {
        $dg = $dg / $totpris;
    }
    $dg = round($dg, 2);
    $dg = $dg . "%";
    if($db>=1200) { $colors="#006600"; } elseif($db>=900) { $colors="#00AA00"; } elseif($db>=700) { $colors="#00FF00"; } elseif($db>=500) { $colors="#FFFF00"; } elseif($db>=300) { $colors="#FF6600"; } else { $colors="#FF0000"; }
    if($price==NULL) { $db = "n/a"; $colors = "#888888"; $price = "n/a"; $dg = "n/a";}
    $color = array(
    "db" => "$db",
    "color" => "$colors",
    "ts" => "$ts",
    "price" => "$price",
    "dg" => "$dg", );
    return $color;
}
?>
<p>
<table>
<thead>
<tr>
<th>Device sort</th>
</th>Prod sort</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<form action="" method="get">
<?php
if(isset($_GET['ab_sort'])) {
    echo "<input type=hidden name=\"ab_sort\" value=\"" . $_GET['ab_sort'] . "\">\n";
}
?>
<select name="dev_sort" style="width: 20em;" onchange="this.form.submit()">
  <option value="">Show all</option>
<?php
while($row = mysqli_fetch_array($telesort)) {
$id = $row['id'];
$prod = $row['prod'];
$model = $row['model'];
echo "  <option value=\"$id\">$prod $model</option>\n";
}
?>
</select>
<input type=submit value="Update"></form></td>
<td>
<form action="" method="get">
<?php
if(isset($_GET['tel_sort'])) {
    echo "<input type=hidden name=\"tel_sort\" value=\"" . $_GET['tel_sort'] . "\">\n";
}
if(isset($_GET['prod_sort'])) {
    echo "<input type=hidden name=\"prod_sort\" value=\"" . $_GET['prod_sort'] . "\">\n";
}
?>
<input type=submit value="Oppdater"></form></td>
</tr>
<tr>
<td>
<form action="" method="get">
<?php
if(isset($_GET['ab_sort'])) {
    echo "<input type=hidden name=\"ab_sort\" value=\"" . $_GET['ab_sort'] . "\">\n";
}
?>
<select name="prod_sort" style="width: 20em;" onchange="this.form.submit()">
  <option value="">Show all</option>
<?php
while($row = mysqli_fetch_array($prodsort)) {
$id = $row['id'];
$prod = $row['prod'];
echo "  <option value=\"$prod\">$prod</option>\n";
}
?>
</select>
<input type=submit value="Update"></form></td>
</tr>
</tbody>
</table>
</p>

<?php
if (is_array($ab_vis)) {
    $count = count($ab_vis);
} else {
    $count = 1;
}

$width = $count*130;
$width = $width+400;
echo "<table border='1' width=\"" . $width . " px\" id=\"mytable\">
<thead>
<tr>
<th><div title=\"$db_table_naming\">prod</th>
<th>model</th>\n";
if($count > 1) {
    foreach($ab_vis as $ab) {
        echo "<th>" . $ab . "</th>\n";
    }
} else {
echo "<th>$ab_vis</th>\n";
}
echo "</tr>
</thead>\n";

$mndpr = "";

while($row = mysqli_fetch_array($result)) {
  echo "<tbody>\n<tr class=\"mark\">";
  $device = $row['id'];
  $ab = ab($device);
  $kost = $row['kost'];
  $ts = ts($device);
  $ukon = $row['ukon'];
  if($ukon=="1") { $tdcolor = "#0099FF"; } else { $tdcolor = "#FFFFFF"; }
  echo "<td bgcolor=\"" . $tdcolor . "\">" . $row['prod'] . "</td>\n";
  echo "<td bgcolor=\"" . $tdcolor . "\"><div title=\"model: " . $row['model'] . " (id:" . $device . ")\">" . $row['model'] . "</a></div></td>\n";
  foreach($abo_vis as $abs) {
        $ab = "$abs";
        $if_model_one = if_model_one($ab, $device);
        if(isset($if_model_one[1])) {
            $name = $if_model_one[0];
            $mndpr = $if_model_one[1];
        } else {
            $pro = $if_model_one[0];
        }
        $price = $abo[$ab];
        if($ts[$ab] == 1) {
            $ts_sum = $ts["ts_sum"];
        } else {
            $ts_sum = 0;
        }
        $totpr = $price;
        $color = color($price, $kost, $ukon, $ts_sum, $ab);

        $kategori_sjekk = kategori_pris($ab);
        if (is_numeric($kategori_sjekk)) {
            $tellKat = count($kategori_sjekk);
        } else {
            $tellKat = 0;
        }
        if( $tellKat > 0 ) {
            $deviceAboVis = "";
            for ($abo_row = 0; $abo_row < $kat_count; $abo_row++) {
                $abo_navn = $kategori_abo_sjekk[$abo_row][0];
                $grupris = $kategori_abo_sjekk[$abo_row][1];
                $mndpris = $kategori_abo_sjekk[$abo_row][2];
                $totalpris = $mndpris*12+$color["pris"];
                $mnduten = $grunnpris*12;
                $devicepris = $totalpris-$mnduten;
                if($color["pris"] == "n/a") {
                    $totalpris = 0;
                    $devicepris = 0;
                } else {
                    $deviceAboVis .= "Abo: " . $abo_navn . "&#13;";
                    $deviceAboVis .= "Mndpris: " . $mndpris . " (" . $grupris . ")&#13;";
                    $deviceAboVis .= "Totalpris: " . $totalpris . "&#13;";
                    $deviceAboVis .= "devicepris: " . $devicepris . "&#13;&#13;";
                }
            }
            echo "<td bgcolor=\"" . $color['color'] . "\"><div title=\"" . $ab . "&#13;model: " . $row['model'] . "&#13;&#13;Utpris: " . $color['pris'] . "&#13;Kost: " . $kost . "&#13;TS: " . $color['ts'] . "&#13;DB: " . $color['db'] . "&#13;DG: " . $color['dg'] . "&#13;&#13;" . $deviceAboVis . "\">" . $color['pris'] . "</div></td>\n";
        } else {
        if (!isset($mnduten)) { $mnduten = $prov['grunnpris']*12; $devicepris = $totalpris - $mnduten; }
        $abo_short = strtolower(str_replace(' ', '', $ab));
        echo "<td bgcolor=\"" . $color['color'] . "\"><!-- t" . $color['tXt'] . " --><div title=\"" . $ab . "&#13;model: " . $row['model'] . "&#13;&#13;Utpris: " . $color['pris'] . "&#13;Kost: " . $kost . "&#13;Prov: " . $color['prov'] . "&#13;IMEI: " . $color['imei'] . "&#13;TS: " . $color['ts'] . "&#13;DB: " . $color['db'] . "&#13;DG: " . $color['dg'] . "&#13;&#13;M&aring;nedspris: " . $prov['mndpris'] . "&#13;Totalpris: " . $totalpris . "\">" . $color['pris'] . "</div></td>\n";
        }
    }
  echo "</tr>\n</tbody>";
}

echo "</table>\n";

function kategori_sjekk($kat) {
    global $conn;
    global $pro;
    $telque = mysqli_query($conn,"SELECT * FROM $pro WHERE abonnement = '$kat'");
    $telres = mysqli_fetch_assoc($telque);
    return $telres;
}

function if_model_one($ab, $device) {
    $pro = pro($ab);
    if(isset($navn)) {
        $return = array($pro, $navn, $mndpris);
    } else {
        $return = array($pro);
    }
    return $return;
}

mysqli_close($conn);

MYSQL数据库将如下所示:

CREATE TABLE `device_kost` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `prod` varchar(64) NOT NULL,
  `model` text NOT NULL,
  `kost` decimal(10,2) NOT NULL,
  `ukon` int(10) NOT NULL,
  PRIMARY KEY (`id`)
);

INSERT INTO `device_kost` VALUES 
(1,'Samsung','Galaxy',1361.00,0),
(2,'Apple','iPhone',2510.00,0);

CREATE TABLE `device_ts` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `device` int(10) DEFAULT NULL,
  `nick` varchar(32) DEFAULT NULL,
  `ts_sum` int(10) DEFAULT NULL,
  `date_f` date NOT NULL,
  `date_t` date NOT NULL,
  PRIMARY KEY (`id`)
);
INSERT INTO `device_ts` VALUES 
(1,1,'gal',200,'2019-01-01','2019-02-28'),
(2,2,'ip',200,'2019-01-01','2019-02-28');

“ device_ts”表还有其他3个类似表。而且我尝试将他们内心联系在一起,但是没有运气。首先,我必须将similair列名称设置为“ as”。

SELECT *
FROM
(
select a.`id` , a.`prod` , a.`model` , a.`kost` , a.`ukon` , 
b.`device` as `device_ts` , b.`ts_sum` , b.`date_f` , b.`date_t` , b.`Ub` as `ubt_ts` ,
c.`device` , c.`Ub` as `ubt_ma` 
 from 
device_kost a 
inner join device_ts b on a.`id` = b.`device`
inner join device_max c on a.`id` = c.`device`
) as T
order by T.id Desc limit 10 

页面/脚本效果很好。但由于设备过多,大约需要30-40秒。我想,如果我可以创建一个更好的内部联接函数,然后在删除很多不必要的sql查询之后对信息进行排序。

0 个答案:

没有答案