473,809 Members | 2,931 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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=ci ty_video_link.c ity_id

WHERE

(
videos.video_id =city_video_lin k.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=ci ty_video_link.c ity_id,
country_video_l ink, INNER JOIN country ON
country.country _id=country_vid eo_link.country _id

WHERE

(
videos.video_id =city_video_lin k.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_lin k.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 1928
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_l ink 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
2615
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 want to search within my mysql database. But using: - several columns (title, description) - several tables (the product table is linked with another table which
0
1312
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 created a view which joined the three tables and i attempted to perform a full-text search on that view, but that resulted in an error indicating that a "unique column" is required for that view.
0
3940
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 into the needed value in the text file usind linked server insert command..... Till here its working fine.... now with time the text file is growing and i only needed the latest value from the text file..... so i want to truncate the values from...
0
1356
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 --------------------------------------------- WidthID---int (primary key) Width------decimal
2
3391
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 table today, it is linked as a text(255) field, ditto for the first table if I link it today if I link the 2nd table using access2003 (and the same odbc data source) it is linked as a memo field
9
2534
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 using search criteria", "Delete the current record" and so on. After a user has used the search criteria to find a specific record, I would like to use the "delete" function on the form to not only delete the record, but also update another table. ...
1
1749
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 linked by the CompanyID. I have a master form displaying company info and then a subform showing all the contacts at that company. I know how to search the master form for a particular company and display that record, and I presume it would be...
2
4656
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 there be a comma at the last row?
0
2731
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, cannot serch by combine 3 table) Example I have the query table below, how do I make the code to seach based on the query from this: SELECT Product.ID, Product.Description, Quantity.Quantity, Quantity.SeialNo, Quantity.SupplierID,...
0
9600
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10633
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10376
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10375
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10114
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9198
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6880
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5686
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3860
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.