Hi guys,
I have a table currently set up like this:
[video] <- [city_video_link] -> [city]
video
------
[p]video_id
video_name
etc.
city_video_link
---------------
[f]video_id
[f]city_id
city
----
[p]city_id
city_name
Where one video can linked to many cities.
I would like to be able to do a free text search on many fields and
other linked tables including cities. So far the best query I've come up
with is
--
SELECT DISTINCT videos.*
FROM video, city_video_link INNER JOIN city ON
city.city_id=city_video_link.city_id
WHERE
(
videos.video_id=city_video_link.video_id
AND
city.city_name REGEXP 'london'
)
--
However as soon as more many to many tables are added to the query then
it soon becomes very slow. eg.
--
SELECT DISTINCT videos.*
FROM video, city_video_link INNER JOIN city ON
city.city_id=city_video_link.city_id,
country_video_link, INNER JOIN country ON
country.country_id=country_video_link.country_id
WHERE
(
videos.video_id=city_video_link.video_id
AND
city.city_name REGEXP 'london'
)
OR
(
videos.video_id=country_video_link.video_id
AND
country.country_name REGEXP 'london'
)
--
I can speed it up by changing it to
--
WHERE
(
videos.video_id=city_video_link.video_id AND
videos.video_id=country_video_link.video_id
)
AND
(
city.city_name REGEXP 'london'
OR
country.country_name REGEXP 'london'
)
--
But if there isn't a corresponding link in one of the tables then the
record isn't found even if the other tables match.
Is there any way I can get this query running at a practical speed, or
will I have to re-think the way the database works?
Thanks
Andrew