Click here to Skip to main content
15,850,360 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I have 10 tables. Each table depends on the other. I am using joins (more than 10 joins) in a single query to fetch result.
Problems are:

1. I find lots of repeated data for a single primary key column since for each primary key for eg. there are 3 matches in table 1 whereas 2 matches in table and so on. Thus data repeatation is 3*2*..*n times. Does this really matters in case I am fetching a single record?

2. So many joins create a bit of complexity(not an issue) but will it be better to use multiple queries instead of joins on comparing them on fetching speed?
PIEBALDconsult 9-Aug-15 3:12am    
It's not about speed. What does your application actually need?
Member 11040029 9-Aug-15 3:26am    
My app needs to fetch lots of data about a single person which is distributed among normalized tables. I am using joins to retrieve them.

I was just wondering, if will it be better to use multiple queries against joins?
PIEBALDconsult 9-Aug-15 12:27pm    
"normalized tables"
Unless you're writing a report, why flatten it?
Sergey Alexandrovich Kryukov 9-Aug-15 10:37am    
How can you compare incomparable things? It's not about speed, it's about the state if your database and your applications. Also, a set of separate queries is non-transactional.

In addition to solution 1 by Advy Pandya[^], i'd suggest to read this: Visual Representation of SQL Joins[^]
Share this answer
Advay Pandya 10-Aug-15 3:57am    
Nice article Maciej.... :)
Maciej Los 10-Aug-15 4:09am    
You can share your opinion via voting for this article.
Advay Pandya 10-Aug-15 5:00am    
yup. sure. It deserves....

In your query you are getting repeated data.The reason behind the repetition is the tables might be connected with one to many relation. So, you will need to put separate condition in the join to filter out the repeated data.The separate condition depends on the output that you are trying to get.

Multiple queries are never replacement of join. I am not sure about the result that you are trying to get, but multiple queries may not be your solution. and even if you will get output, of course the performance will get refused using multiple queries.

The best solution for your problem is to fix the one to many relation by filter out the repeated data. separate condition after "ON" will help you to filter out the repetition.

Please let me know if you have any concern or query or if you need more information on this.

Share this answer
Member 11040029 10-Aug-15 2:06am    
I got my answer in ur solution.. tnx!
Advay Pandya 10-Aug-15 2:37am    
Welcome :)
Maciej Los 10-Aug-15 4:09am    
Good advice, +5!
Advay Pandya 10-Aug-15 6:08am    
Thanks Maciej :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900