问题
actually m using this temp table concept becoz i want the query to run fast. my code is as follows
$maketemp1 = "
CREATE TEMPORARY TABLE bin1 (
`ackNo` varchar(55),
`repairStatus` varchar(100),
`productFamily` varchar(100),
`location` varchar(100),
`binTo` varchar(100),
`binTime` datetime,
`age` int(50)
)
";
mysql_query($maketemp1) or die ("Sql error : ".mysql_error());
$inserttemp1 = "
INSERT INTO bin1
(`ackNo`, `repairStatus`, `productFamily`, `location`,`binTo`,`binTime`,`age`)
SELECT customerupdate.ackNo,tblRepairQueue.repairStatus, tblRepairQueue.productFamily,tblRepairQueue.location,customerupdate.binTo,customerupdate.binTime,
TIMESTAMPDIFF(HOUR , customerupdate.binTime , '".$value."') FROM `tblRepairQueue` , `customerupdate` WHERE
tblRepairQueue.ackNo=customerupdate.ackNo and tblRepairQueue.location='".$empLocationName."'
";
mysql_query($inserttemp1) or die ("Sql error : ".mysql_error());
But what is happening is when i run the above query it is taking too long time to copy ..the process is very slow when compared to normal query.. and also i have another doubt..will the concept of using temp table give results faster?
回答1:
Temporary table might not be the best option depending on what you are trying to achieve... It can become useful to create a table with some data consolidation to narrow the scope of the next couple requests but it is rarely efficient to do so.
Also, temporary tables cannot be accessed multiple times, so a real table would be needed.
The creation of a table requires disk writes, which is a lot less efficient than a pure select in in the RAM. Using caching methods, you can retrieve the result of a previous select very fast. You could use that select as the FROM part of your queries to make it faster than pooling in a huge database. But even then...
We are evolving on a dataset of approximately 1To, big queries are findings products according to the users' rights (category,store,...) we have tried to create a table for each user pointing to the products he has the rights on and then use that table to retrieve the data without requiring huge where conditions but it was still slow using SSD. So we just dumped huge RAM on the server and the SELECT that we would have used to fill the temp tables is ran for each user at login, and then used as FROM and everything's perfect.
来源:https://stackoverflow.com/questions/21424969/copying-to-temp-table-is-taking-long-time