I have an SQL query that I think is not optimized enough. I timed it, and it sometimes take 1.5 seconds to complete, which seems a little high, no?
anyway this is the query:
SELECT id, link, feed, category, description, title, GROUP_CONCAT( tag ) as t, published
FROM items
LEFT JOIN tags ON items.id = tags.item_id
WHERE id NOT IN (
SELECT item_id
FROM tags, sinunim
WHERE tag = name
AND op = '1'
AND user = '$user_cookie'
) AND id NOT IN (
SELECT id
FROM sinunim
WHERE id <> 0
AND user = '$user_cookie'
) AND id NOT IN (
SELECT i.id
FROM sinunim s, items i
WHERE s.type = 'category'
AND s.name = i.category
AND s.op = '1'
AND s.user = '$user_cookie'
) AND id NOT IN (
SELECT i.id
FROM `sinunim` s, items i
WHERE s.name = i.feed
AND s.op = '1'
AND s.user = '$user_cookie'
)
GROUP BY items.title
ORDER BY items.published DESC
LIMIT 0 , 50
You can rewrite the 4
NOT INsub queries as one single sub query and since you’re grouping byitems.titleyou can even eliminate all sub queries and use joins instead.That way whatever you join will be joined only once and you can recreate the various comparisons by grouping them in one bigger logical expression
(x = y AND y = z) OR (k = l AND m = n).