?
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
Medium Priority
?
164 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 2000 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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
In this article, we’ll look at how to deploy ProxySQL.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

741 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