Q:

Server performance issue - which query will be better solution

I have this simple question about server performance.
Let say that I generate $_SESSION['expired_products'] which is array of ids of all products that:
$query='select id from products 
where active="1" and available_qty > 0 and expired_date > now()';

this query will be executed on first visitor's visit
and than query in a loop will check:
$query='select * from products where id not in('.$_SESSION['expired_products'].')';

Or each time to check for: 
active="1" and available_qty > 0 and expired_date > now()
Thanks in advance !

A:

It would probably be better to do the check each time, because you don't know when quantity will become 0, could be 10 seconds, could be never.  Also expired_date could be soon, though this issue could be somewhat mitigated by adding some time to now().
Make sure active, available_qty, and expired_date have indexes in the database.  With indexes, performance most likely won't be an issue.
As far as performance with the queries,
$query='select id from products 
where active="1" and available_qty > 0 and expired_date > now()';

is considerabaly faster than
$query='select * from products where id not in('.$_SESSION['expired_products'].')';

if you have a lot of expired products, though IN is considerably faster than chaining OR's together.

