[英]PHP and SQL: code is really slow
$unique = array();
$sql = "SELECT ID, TitleName, ArtistDisplayName, Mix FROM values_to_insert as A
WHERE A.ID = ";
//Get a single row from our data that needs to be inserted...
while($result = $conn->query(($sql. $count)))
{
//Get the $data of the single row query for inserting.
$data = mysqli_fetch_row($result);
$count++;
//SQL to get a match of the single row of $data we just fetched...
$get_match = "SELECT TitleName_ti, Artist_ti, RemixName_ti from titles as B
Where B.TitleName_ti = '$data[1]'
and B.Artist_ti = '$data[2]'
and B.RemixName_ti = '$data[3]'
LIMIT 1";
//If this query returns a match, then push this data to our $unique value array.
if(!$result = $conn->query($get_match))
{
//If this data has been pushed already, (since our data includes repeats), then don't
//put a repeat of the data into our unique array. Else, push the data.
if(!in_array($unique, $data))
{
echo 'Pushed to array: ' . $data[0] . "---" . $data[1] . "</br>";
array_push($unique, $data);
}
else
echo'Nothing pushed... </br>';
}
}
这花费了5分钟以上的时间,甚至什么都没有打印到屏幕上。 我很想知道正在消耗那么多时间的东西,以及可能花费所有时间来替代的方法或功能。 我猜一些正确方向的指针会很棒。
这段代码基本上获取表'A'的所有行,一次一次。 检查表'B'中是否存在匹配项,如果存在,那么我不希望该$ data,但是如果没有,则然后检查数据本身是否是重复项,因为我的表' A'具有一些重复值。
表A有60,000行表B有200,000行
查询中的查询很少是一个好主意
但是您的脚本似乎存在多个问题。 只用SQL完成全部工作并将结果每次都推送到数组可能会更容易。 SQL可以删除重复项:
<?php
$unique = array();
$sql = "SELECT DISTINCT A.ID,
A.TitleName,
A.ArtistDisplayName,
A.Mix
FROM values_to_insert as A
LEFT OUTER JOIN titles as B
ON B.TitleName_ti = A.ID
and B.Artist_ti = A.TitleName
and B.RemixName_ti = A.ArtistDisplayName
WHERE B.TitleName_ti IS NULL
ORDER BY a.ID";
if($result = $conn->query(($sql)))
{
//Get the $data of the single row query for inserting.
while($data = mysqli_fetch_row($result))
{
array_push($unique, $data);
}
}
至于你原来的查询。
您有一个计数(我假设它被初始化为0,但是如果有一个字符,那么它将做奇怪的事情),并获得具有该值的记录。 如果第一个ID是1,000,000,000,则您已经进行了1b次查询,然后才找到要处理的记录。 您可以通过删除WHERE子句并按ID排序来获得ID顺序中的所有行。
然后,您可以从第二个查询中获得一条记录,其中详细信息匹配,但是只有在未找到记录的情况下才对它们进行处理。 您不使用任何返回的值。 您可以通过执行LEFT OUTER JOIN来获取匹配项,并检查WHERE子句中是否没有匹配项,以实现此目的。
编辑-正如您所指出的,您似乎用来匹配记录的字段似乎在逻辑上不匹配。 我已经像您一样使用了它们,但我希望您确实希望将B.TitleName_ti匹配为A.TitleName,将B.Artist_ti匹配为A.ArtistDisplayName,将B.RemixName_ti匹配为A.Mix
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.