I tested so much things to get my Query the fastest as i can.
The table has over 2.9 Million Rows. And thats the Query
SELECT id, name, price, pricebefore, link, imagelink, updated, site, siteid 
    FROM items 
    WHERE (case when pricebefore3 is NULL then pricebefore else pricebefore3*1.5 end) >= pricebefore 
    AND price < pricebefore 
    AND isbn != -1 
    AND 1 = CASE
      WHEN (100-price/pricebefore*100) > 90 THEN updated < NOW() - INTERVAL ".$timeago." MINUTE
      ELSE (100-price/pricebefore*100) > ".mysqli_real_escape_string($link, $percentage)." 
    END
    ".preg_quote($merchant)."
    AND name LIKE '%".mysqli_real_escape_string($link, $search)."%' 
    ORDER BY updated DESC 
    LIMIT ".mysqli_real_escape_string($link, $perPage)." 
    OFFSET ".mysqli_real_escape_string($link, $site);
Explain the variables:
- $timeagocan be 0 or like 30 to get only items older than 30 minute
- $percentagecan be like 70 to show only items with more than 70 percentage on price difference
- $merchantis that- AND link REGEXP '".$merchant."'and the- $merchantcan look like that- amazon.de/gp/product|idealo.deto get only items with that in the link (so only items from amazon.de and idealo.de
- $searchcan be a keyword that includes in the name
- $perPageand- $siteis for pagination. so 20 items per page, site 1
I tried so much different things but doesnt find a good way.
There is also a way without $search line when $search is not set (so no one is searching specific item)
$merchant is also ony set when user has something in $merchant otherwise its that $merchant = '';
does anyone have an idea to make that faster?
the sql is running with mysqli_connect just simple php file
and one more thing...
if i have this in regex, it is faster than the second one. But why?!
AND link REGEXP 'amazon.de/gp/product|idealo.de'
AND link REGEXP 'idealo.de'
The second is 2 secs slower than the first?!
 
     
    