Jump to content

[SOLVED] Join on temporary table through php


wsantos

Recommended Posts

First I'd like to express my apologies to rjivgonzales ... this did work

 

mysql_query("DROP TEMPORARY TABLE IF EXISTS tmp_Main");
mysql_query("CREATE TEMPORARY TABLE tmp_Main SELECT src,uid FROM tblMain WHERE ... <my filters> ");

 

I did test it with

 

mysql_query("DROP TEMPORARY TABLE IF EXISTS tmp_Main");
mysql_query("CREATE TEMPORARY TABLE tmp_Main SELECT src,uid FROM tblMain WHERE ... <my filters> ");
$result = mysql_query("SELECT * FROM tmp_Main");
while($row=mysql_fetch_array($result,MYSQL_ASSOC))
  $daRes[] = $row;
print_r($daRes);

 

My bad for not testing by section.  It seemed I have the error on subsequent query which goes like

 

mysql_query("DROP TEMPORARY TABLE IF EXISTS tmp_Main");
mysql_query("CREATE TEMPORARY TABLE tmp_Main SELECT src,uid FROM tblMain WHERE ... <my filters> ");
mysql_query("DROP TEMPORARY TABLE IF EXISTS tmp_cid");
mysql_query("CREATE TEMPORARY TABLE tmp_Main SELECT src,uid FROM tblMain JOIN tmp_Main WHERE ... <my filters> ");
$result = mysql_query("SELECT * FROM tmp_cid");
while($row=mysql_fetch_array($result,MYSQL_ASSOC))
  $daRes[] = $row;
print_r($daRes);

 

Apparrently, I have an empty result on the join...which is not the case when I run the same query directly on mysql. Thanks for the help guys.

Link to comment
Share on other sites

I think you meant to name the second temp table name to be tmp_cid,but you named it tmp_main again

 

so it should be

mysql_query("DROP TEMPORARY TABLE IF EXISTS tmp_cid");
mysql_query("CREATE TEMPORARY TABLE tmp_cid SELECT src,uid FROM tblMain JOIN tmp_Main WHERE ... <my filters> ");

Link to comment
Share on other sites

Sorry on the typo...

 

This code still returned an empty result.

 

mysql_query("DROP TEMPORARY TABLE IF EXISTS tmp_Main");
mysql_query("CREATE TEMPORARY TABLE tmp_Main SELECT src,uid FROM tblMain WHERE ... <my filters> ");
mysql_query("DROP TEMPORARY TABLE IF EXISTS tmp_cid");
mysql_query("CREATE TEMPORARY TABLE tmp_cid SELECT src,uid FROM lookup_cid JOIN tmp_Main.uid=lookup_cid.uid ON tmp_Main WHERE ... <my filters> ");
$result = mysql_query("SELECT * FROM tmp_cid");
while($row=mysql_fetch_array($result,MYSQL_ASSOC))
  $daRes[] = $row;
print_r($daRes);

Link to comment
Share on other sites

Sorry it was a typo on my end that's why it didn't work.  However using temporary table is our chosen approach though a join is an immediate obvious approach for a couple of reasons:

 

1.  That db is 60G records in 1 month for the main table alone which yeilds at least 10 records on the second table.  For each record on the second table at least 60 records on the third. For each record on the third at least 500 records on the 4th.

2.  Though that db is properly indexed there are too many stored procedures not including the individual queries of each of the ~500 workstations that is querying approximately every 1 min.

3.  That db is currently not replicated hence I cannot work on the mirrored box.

 

Any technical suggestions on this is highly appreciated.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.