We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Timing Out - Compare 2 tables

I've got 2 tables, basically this weeks stock list and last weeks. Each has around 25,000 SKU's. I'm trying to run a comparison programme to see where there are stock-outs i.e. SKU missing from the latest table. Table structures identical. Both about 20mb in size.

Script is timing out so wondered if there is a way to avoid this

<?php {foreach (glob('test.txt') as $fn) {unlink($fn);}}?>
<?php 
include 'db_dave.php';
$query = "SELECT con_inv_old.SKU
FROM con_inv_old
LEFT JOIN con_inv
ON con_inv_old.SKU = con_inv.SKU
WHERE con_inv.SKU IS NULL";
$result = mysql_query($query) or die (mysql_error() . "<br />Couldn't execute query: $query");
$num = mysql_num_rows($result);
$i = 0;
while ($i < $num) { 
$EAN = mysql_result($result, $i, "con_inv_old.SKU");
$File = "test.txt";
$Handle = fopen($File, 'a');

$Data = $EAN;
fwrite($Handle, "$Data\r\n");

fclose($Handle);
 $i++;
}
mysql_close();
?>
3
Contributors
5
Replies
1 Week
Discussion Span
5 Months Ago
Last Updated
6
Views
Question
Answered
eman neercs
Newbie Poster
18 posts since Nov 2009
Reputation Points: 9
Solved Threads: 0
Skill Endorsements: 0

SKU is indexed I assume.

You can try this to see if it makes a difference (it avoids a costly join):

SELECT `SKU`
FROM `con_inv_old`
WHERE `SKU` NOT IN (
    SELECT `SKU`
    FROM `con_inv`
    WHERE `SKU` IS NOT NULL
)
pritaeas
Posting Prodigy
Moderator
9,317 posts since Jul 2006
Reputation Points: 1,178
Solved Threads: 1,467
Skill Endorsements: 86

ahouldn't even need the where on the inner query

jstfsklh211
Junior Poster
100 posts since Apr 2011
Reputation Points: 34
Solved Threads: 27
Skill Endorsements: 1

No, but it reduces the result set.

pritaeas
Posting Prodigy
Moderator
9,317 posts since Jul 2006
Reputation Points: 1,178
Solved Threads: 1,467
Skill Endorsements: 86

Thanks for the code.

This works after I had reduced the table size - not enough processing power.

SELECT b.SKU
FROM con_inv_old b
WHERE b.SKU NOT IN (
    SELECT a.SKU
    FROM a.con_inv
    WHERE b.SKU IS NOT NULL
)

The list gives me those in "b" (old data) that are not in "a" (new data). I now want to use the list of those not in "a" to select data from "b" but it is timing out again.

SELECT b.SKU, a.SKU, a.quantity
    FROM con_inv_old b
    WHERE b.SKU NOT IN (
        SELECT a.SKU
        FROM a.con_inv
        WHERE b.SKU IS NOT NULL
    )

Or is my code wrong.

eman neercs
Newbie Poster
18 posts since Nov 2009
Reputation Points: 9
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 5 Months Ago by pritaeas and jstfsklh211

what data are you trying to select
probably what you want is

SELECT SKU, quantity
FROM con_inv_old
WHERE SKU NOT IN (
    SELECT SKU FROM con_inv
)

this should give you the sku and quantity for each item missing from con_inv

@pritaeas
the only the only null sku's would have been the ones from the outer join
without the outer join the where is not null should be extra

jstfsklh211
Junior Poster
100 posts since Apr 2011
Reputation Points: 34
Solved Threads: 27
Skill Endorsements: 1

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0763 seconds using 2.73MB