473,396 Members | 2,158 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.

MySQL table intersections

1
Hi all,

I am trying to figure out how to simulate an intersection in MySQL 4.1. I'm going to make a toy example to illustrate the problem.

I have 3 tables,
WordTable

| word id | word |
| 1 | rabbit |
| 2 | venus |
| ...
| 2600000 | apple |

SentenceTable
| sentence id | data |
| 1 | some data |
| 2 | some data |
| ....
| 2600000 | some data |


Mapping Table (SentenceWordTable)
Basically like an index
| Word Id | Sentence Id |
| 1 | 34 |
| 2 | 343 |
| 1 | 343 |
| 2 | 394 |
...
| 448 | 48535 |


What happens is I might want to search for sentences that are mapped to word ids (1, 4, 304, 399). How do I do it without writing a sql statement with 3 inner joins? The problem is if I go to 15 words, it would become a huge statement and it will run very slowly.

E.g. something like this might run very slowly + ugly
SELECT Distinct a.SentenceId
FROM SentenceWordTable a
INNER JOIN SentenceWordTable b
INNER JOIN SentenceWordTable c
...
INNER JOIN SentenceWordTable z
WHERE a.SentenceId = b.SentenceId ... AND y.SentenceId=z.SentenceId
AND a.WordId = 3 AND b.WordId = 5 ... AND z.WordId = 49

I hope to do something like

SELECT Distinct a.SentenceId FROM SentenceWordTable a WHERE a.WordId = 3
INTERSECT
SELECT Distinct b.SentenceId FROM SentenceWordTable b WHERE b.WordId = 53
...

This is for some biological data, but I'm using the word/sentence problem as an analogy.


Thanks!
Feb 18 '06 #1
1 7608
Banfa
9,065 Expert Mod 8TB
It appears to me that once SentenceWordTable is set up this is the only you are searching which should be quite fast.

You are using a notation that I am not quite familiar with I would have expected to see

SELECT Distinct SentenceId FROM SentenceWordTable WHERE WordId = 3
INTERSECT
SELECT Distinct SentenceId FROM SentenceWordTable WHERE WordId = 53

notice lack of a's and b's.

I take it what you want is to select a sentence that has all of a given number of words. Since MySql doesn't do INTERSECTions I think I would probably use a simple search to get all the data that I required and then post process that data in whatever language you are using.

SELECT Distinct SentenceId,WordId FROM SentenceWordTable WHERE WordId = 3 OR WordId = 53

Alternitively use a better database.
Feb 20 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Paul Graham | last post by:
Hi All, New to Java2D and am trying to do something I know should be relatively simple but isn't turning out to be. I want to draw intersecting rectangles across an arbitrary shape that can...
0
by: Gordon | last post by:
I have 2 tables t and t1. In this case, t1 is a copy of t. I want to delete rows from t1 based on criteria on the t table and a relationship between t ad t1 (in this case the id column). In the...
0
by: Lenz Grimmer | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, MySQL 4.0.14, a new version of the popular Open Source/Free Software Database, has been released. It is now available in source and binary...
6
by: jacob nikom | last post by:
I would like to create data model for a group of stores. All stores in this group are very similar to each other, so it is natural to allocate one MySQL database per store. Each database is going...
0
by: Mike Chirico | last post by:
Interesting Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Copyright (GPU Free Documentation License) 2004 Last Updated: Mon Jun 7 10:37:28 EDT 2004 The latest...
39
by: Mairhtin O'Feannag | last post by:
Hello, I have a client (customer) who asked the question : "Why would I buy and use UDB, when MySql is free?" I had to say I was stunned. I have no experience with MySql, so I was left sort...
1
by: jlee | last post by:
I'm pretty much a newbie on mysql, and I need some help. I am running mysql Ver 12.22 Distrib 4.0.24, for portbld-freebsd5.4 (i386) on a server hosting an active website. The site's developer...
1
by: Good Man | last post by:
Hi there I've noticed some very weird things happening with my current MySQL setup on my XP Laptop, a development machine. For a while, I have been trying to get the MySQL cache to work....
6
Atli
by: Atli | last post by:
This is an easy to digest 12 step guide on basics of using MySQL. It's a great refresher for those who need it and it work's great for first time MySQL users. Anyone should be able to get...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
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,...

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.