I have a very slow query because using ORDER BY. Now i understand why it is slow but i have no idea how i make it faster.
The table got like 13,000,000 records. (the reason why it is slow)
Tables 30 Rows 13,591,548 Data 1.3 GiB Indexes 265.2 MiB Total 1.5 GiB Overhead 96 B
$sql3 = mysql_query("SELECT * FROM config LIMIT 1");
$row3 = mysql_fetch_assoc($sql3);
$log=$row3['con_log'];
$sql2 = mysql_query("SELECT sl_start,sl_id FROM scan_list2 WHERE sl_scanned='0' ORDER BY sl_id LIMIT 40");
while($row2 = mysql_fetch_assoc($sql2)){
echo "crk|".$row2['sl_start']."|".$log."-";
$sl_id = $row2['sl_id'];
mysql_query("UPDATE scan_list2 SET sl_scanned='1' WHERE sl_id='$sl_id'");
Is there any better solution for this problem?
First review your table indexes.
In php you don't need to perform one update for each 40 row, run your update
then select
and after, perform a loop to show information about rows from select
, like:
mysql_query("UPDATE scan_list2 SET sl_scanned = '1' WHERE sl_scanned = '0' ORDER BY sl_id LIMIT 40");
$sql3 = mysql_query("SELECT * FROM config LIMIT 1");
$row3 = mysql_fetch_assoc($sql3);
$log=$row3['con_log'];
$sql2 = mysql_query("SELECT sl_start,sl_id FROM scan_list2 WHERE sl_scanned = '1' ORDER BY sl_id DESC LIMIT 40");
while($row2 = mysql_fetch_assoc($sql2)){
echo "crk|".$row2['sl_start']."|".$log."-";
}
Try instead change your UPDATE query to update all the records in one update by adding JOIN
UPDATE scan_list2
INNER JOIN
(
SELECT sl_start,sl_id
FROM scan_list2
WHERE sl_scanned='0'
ORDER BY sl_id
LIMIT 40
) b ON scan_list2.sl_id = b.sl_id
SET sl_scanned='1'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.