Solved

I'm looking for the best way to join two tables in MySql based on the matching of the words in a varchar field in one table against a varchar field on another table

Posted on 2009-06-30
4
157 Views
Last Modified: 2012-05-07
I have two tables with a description column in each one. The first table may have between 500,000 and 1,000,000 records, the second one may have between 1,000 and 10,000 records. I need to get the join of both tables based on the descriptions fields in each table but the problem is I should search the words in the descriptions fields in any order. For example "My house is red" should join with "Red is my house" or with "is red my house". So the words in the description field on the first table should be the same to the words in the description field on the second table without considering the order of those words.
I could create in both tables 7 or 8 varchar fields (the descriptions are never longer than 8 words) to store all the words from the descriptions fields of both tables if it could help.
How can I solve this having a good performance?


In table_b, word_1 has the first word of the description field for each record in this table, word_2 has the second word of the description field for each record in this table, and so on:
 

select a.*, b.*

from table_a a, table_b b

where a.descrip like b.word_1 

            and a.descrip like b.word_2 

            and a.descrip like b.word_3 

            and a.descrip like b.word_4 

            and a.descrip like b.word_5 

            and a.descrip like b.word_6

Open in new window

0
Comment
Question by:egrinblat
  • 2
4 Comments
 
LVL 41

Accepted Solution

by:
pcelba earned 500 total points
ID: 24750553
You don't need several more columns just one containing description with words sorted alphabetically (and converted to lowercase possibly). Then you may simply compare these new descriptions in join.
0
 
LVL 41

Expert Comment

by:pcelba
ID: 24750564
And you should create index on these new columns to ensure good performance.
0
 
LVL 14

Expert Comment

by:shru_0409
ID: 24752386
select *
from table_a a, table_b b
where a.column_name = b.column_name -- ref columns from both table
and a.descrip like '%'|| b.descrip || '%'

try this
0
 

Author Closing Comment

by:egrinblat
ID: 31598558
Thank you very much, that's an easy to implement and excellent idea.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now