I have two tables
rb_content : A simple table with movies and series (title/description/category)
rb_exclude : A table with a list of titles/descriptions to exclude from rb_content for a certain category.
I'm no SQL expert, so I'm probably using the wrong approach here to accomplish this.
SELECT title as rb_title, description as rb_description
FROM rb_content
WHERE title NOT IN (
SELECT title
FROM rb_content
INNER JOIN rb_exclude
ON ((
(rb_content.title ILIKE '%' || rb_exclude.txt || '%') AND
(rb_content.category = rb_exclude.category) AND
(rb_exclude.field = 0)
) OR (
(rb_content.description ILIKE '%' || rb_exclude.txt || '%') AND
(rb_content.category = rb_exclude.category) AND
(rb_exclude.field = 1)
)) AND (rb_exclude.active = 1)
);
Thanks!
The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:
CREATE INDEX rb_content_idx_title ON "rb_content" ("title");
CREATE INDEX rb_exclude_idx_field_active_category ON "rb_exclude" ("field","active","category");
SELECT
rb_content.title AS rb_title,
rb_content.description AS rb_description
FROM
rb_content
WHERE
rb_content.title NOT IN (
SELECT
title
FROM
((SELECT
title AS title
FROM
rb_content
INNER JOIN
rb_exclude
ON (
(
(
rb_content.description ILIKE '%' || rb_exclude.txt || '%'
)
AND (
rb_content.category = rb_exclude.category
)
AND (
rb_exclude.field = 1
)
)
)
AND (
rb_exclude.active = 1
))
UNION
DISTINCT (SELECT
title AS title
FROM
rb_content
INNER JOIN
rb_exclude
ON (((rb_content.title ILIKE '%' || rb_exclude.txt || '%')
AND (rb_content.category = rb_exclude.category)
AND (rb_exclude.field = 0)))
AND (rb_exclude.active = 1))
) AS union1
)