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
162 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 42

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 42

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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…

726 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