Question

I'd like to know what the purpose of a mapping table is, and how it speeds up MySQL queries. I am working on speeding up a slow MySQL query, and I ran across this stackoverflow question. Comments suggest that not only do mapping tables speed queries up that join both tables, but that they are the proper way to structure a database.

I have a table structure that is very similar, with a one-to-many relationship. As the table grows, the query execution time continues to climb to unacceptable levels. Since I haven't been able to find much on the web to help solve my problem, I though I'd try stackoverflow, since you guys have never let me down.

Was it helpful?

Solution

I'd like to know what the purpose of a mapping table is

A mapping table is used to represent a "many to many" relationship. For instance, in the question you quoted, there are two entities - books and students. A student may borrow many books; a book may be borrowed by many students.

The standard way to represent this in the relational model is through a "mapping table", which would have foreign keys to books and students, together with any information related to the loan - start and end date, for instance.

A mapping table is indeed a "proper" way to model many to many relationships.

and how it speeds up MySQL queries

It doesn't in most cases, though it usually performs better than other ways of representing "many to many" relationships. If performance is your concern, @Namphibian's answer is on the money as a starting point.

OTHER TIPS

The firs thing you need to answer is why the query is running so slow. Assuming you have the following query

SELECT * FROM Customer WHERE CustomerID= 1

It is running slow. So how do you determine why it is running slow? Type the following:

EXPLAIN ALL SELECT * FROM Customer WHERE CustomerID=1

This will return a result set indicating how MySQL is getting to the data. Here you can find clues about missing indexes etc which can help optimize your queries.

See the following links for more answers:

http://dev.mysql.com/doc/refman/5.0/en/explain.html

MySQL table index optimization

Even if you used a mapping table and your queries and indexing is not optimal you will have abysmal performance.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top