I am trying to extract a random article who has a picture from a database.
SELECT FLOOR(MAX(id) * RAND()) FROM `table` WHERE `picture` IS NOT NULL
My table is 33 MB big and has 1,006,394 articles but just 816 with pictures. My problem is this query takes 0.4640 sek
I need this to be much much more faster. Any idea is welcome.
P.S. 1. of course I have a index on id. 2. there is no index on the picture field. should I add one? 3. the product name is unique, also the product number, but thats out of question.
RESULT OF TESTING SESSION.
@cHao's Solution is faster when I use it to select one of the random entries with a picture.(les then 0.1 sec. But its slower if I try to do the opposite, to select a random article without picture. 2..3 sec.
@Kickstart's Solution is a bit slower when trying to find a entry with picture, but is almost same speed when trying to find a entry without picture. average 0,149 sec.
@bob-kruithof's Solution don't work for me. when trying to find a entry with picture, it selects a entry without picture.
and @ganesh-bora, yes you are right, in my case the speed difference is about 5..15 times.
I want to thank you all for your help, and I decided for @Kickstart.