473,396 Members | 2,059 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

text search on a many to many linked table

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
Jul 20 '05 #1
3 1901
Andrew Crowe wrote:
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?


I recommend you learn how to use an OUTER JOIN. It should be covered in
almost any book on SQL, or you can google for "sql outer join tutorial"
or some such query.

Regards,
Bill K.
Jul 20 '05 #2
Bill Karwin wrote:
Andrew Crowe wrote:
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?

I recommend you learn how to use an OUTER JOIN. It should be covered in
almost any book on SQL, or you can google for "sql outer join tutorial"
or some such query.


In MySQL OUTER JOIN is the same as doing a LEFT JOIN, which after a few
tables are added makes the query run very slowly.

Is there a way to make LEFT JOINs faster, or am I going to have to
approach this from a different angle such as using UNIONs?
Jul 20 '05 #3
Andrew Crowe wrote:
[If] MySQL OUTER JOIN is the same as doing a LEFT JOIN, which after a few
tables are added makes the query run very slowly.

Is there a way to make LEFT JOINs faster, or am I going to have to
approach this from a different angle such as using UNIONs?


I'm assuming you have created indexes on video_id, city_id, country_id,
etc. in all these tables. Try running the query with the EXPLAIN
command to get more information about how MySQL is utilizing indexes.

Also note that REGEXP _cannot_ use an index, because the pattern you are
comparing could be a substring within the city_name or country_name.

Another suggestion is to use a simpler comparison than REGEXP if you are
just looking for fixed strings. Try using LOCATE('london', city_name)
for instance. Read the MySQL docs on string functions for more
possibilities.

Here's one alternate query that should work, if you are using MySQL 4.1
for the subquery support:

SELECT DISTINCT V.*
FROM videos V
WHERE
V.video_id IN (SELECT CV1.video_id FROM city_video_link CV1 INNER
JOIN city C1 USING (city_id) WHERE LOCATE('london', C1.city_name))
OR
V.video_id IN (SELECT CV2.video_id FROM country_video_link CV2 INNER
JOIN country C2 USING (country_id) WHERE LOCATE('london', C2.country_name))
OR ... add other terms as needed ...

I hope MySQL knows how to factor out invariant subqueries. That is,
execute the subquery once and cache the result, instead of for every row
in videos.

Regards,
Bill K.
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Rod | last post by:
Hi, i am doing a ecommerce website and would like to implement a search engine to find products. All the serach engine I have found on the web are parsing html page! This is not what i want. i...
0
by: coosa | last post by:
Dear all, I'm using MS SQL Server 2005 and i have a full-text search issue; Let's say i have table1, table2 and table 3; for each, some full-text indices have been created. The issue is that i...
0
by: hellosibba | last post by:
Hi i am trying to use text file to write a value as soon as there is any update/insert into a table. for that i am using a text file and made a linked server relation with it..... and inserting...
0
by: sean worlock | last post by:
Hello all, I have googled like crazy for this with no sucess! I have a dataset containing a table with the following fields Table===Widths ---------------------------------------------...
2
by: Roger | last post by:
I've got two tables in sql2005 which have an 'ntext' field when I linked the first table in access97 last week using an odbc data source the access-field type was 'memo' when I link the 2nd...
9
by: hrreece | last post by:
I have an Access 2002 database that has a form that can be used to review individual records. At the bottom of the form are buttons that are linked to functions that allow the user to "Find a record...
1
by: nchin61 | last post by:
Hi everyone, I'm an access advanced-beginner, mainly self taught and I'm building a fairly simple parent/child database. I have a table of companies, and a table of contacts at those companies...
2
by: TG | last post by:
Hi! I am trying to export only the visible columns from a datagridview in my windows form in VB 2008. Should't it be no comma after the first row where the headers are? Also should...
0
by: JamesOo | last post by:
I have the code below, but I need to make it searchable in query table, below code only allowed seach the table which in show mdb only. (i.e. have 3 table, but only can search either one only,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.