简体   繁体   中英

How to store results from a query to us IN multiple queries?

Using PHP and Sqlite3, I have a query such as:

$firstquery = "SELECT id FROM mytable WHERE critera=........";

This can give me anywhere from 1 to many 1000's of id 's depending on the criteria.

I then use this found set in up to 40 other SQL queries on my page. All are variations such as:

$secondquery = "SELECT name FROM nametable WHERE nametable.id IN (".$firstquery.")";
$thirdquery = "SELECT car FROM cartable WHERE cartable.id IN (".$firstquery.")";

Now, when I have lots and lost of these $second, $third...$thirtiethquery the page starts getting slow and I think that constantly needing to run $firstquery over and over is not helping.

Is there a way to avoid needing to run $firstquery over and over again? Maybe storing the result as some way? (I tried storing the result as a string and had it in the WHERE, but if there are thousands of id 's then the clause has too many parameters).

Thanks for any thoughts! MacKniven

If the first query is so slow that it noticeably affects your page load time, you should optimize it.

If it is still too slow, you have to temporarily store the result somewhere.

If there can be too many results, you can simply use a temporary table (a temporary table is not visible in other connections):

CREATE TEMPORARY TABLE FirstQueryResults AS
SELECT id FROM mytable WHERE critera=........;

SELECT name FROM nametable WHERE nametable.id IN FirstQueryResults;
...

DROP TABLE FirstQueryResults;

And if there can be many results, it might be worth it to create an index on the temporary table.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM