This question already has answers here:
Which is more efficient: Multiple MySQL tables or one large table?
(9 answers)
Closed 5 years ago.
which is much better, multiple tables join with one main table, or each table join with one another.
example: 5 tables join with one main table
main table == 0
table 5 join table 0, table 4 join table 0, table 3 join table 0, table 2 join table 0, table 1 join table 0
each id's of 5 tables is in main table 0
OR
table 5 join table 4, table 4 join table 3, table 3 join table 2, table 2 join table 1, and table 1 join main table 0.
over all you cant say which one is the better version. The more you normalize, the more you have to join at some point it could lead into performance issues. If you need those joins often, i would prefere less normalization ;)
Related
I want to join 2 columns base on this :
A.Contactnumber and the 2 column B.old_contact, B.recent_contact
I want to retain the old contact and at the same time include recent if they're not alike
select a.*, b.old_contact, b.recent_contact
from table a
left join table b
on a.contactnumber = b.old_contact
SELECT a.contactnumber, b.old_contact, b.recent_contact
FROM a INNER JOIN b ON a.contactnumber=b.old_contact
WHERE b.old_contact!=b.recent_contact;
as the result you will get the values which are present in table "a" and in table "b" and which have different values in columns "old_contact" and "recent_contact" in table "b". Is that what you need?
sorry if this question has been asked heaps before, but I didn't know how to word it in a way that google would be able to understand.
Basically if for example you have 3 tables:
Table A
Table B1
Table B2
and the data from all 3 tables are connected in 1 of 2 ways either:
Table A & Table B1
OR
Table A & Table B2
Which would be best practice to connect them in a table and why?
1 table such as:
Joined table
|Table A |Table B1 |Table B2 |
|tableA_ID|tableB1_ID|null |
|tableA_ID|null |tableB2_ID|
or have 2 seperate tables for each join
Table A and B1 joined
Table A and B2 joined
Or is there another better way?
Joining table depends upon the Fields and Relationship among the tables.
It also depends on the output you are looking - based on this you will need to join the tables
I think you want a left join, two in fact:
select a.tableA_ID, b1.tableB1_ID, b2.tableB2_ID
from a left join
b1
on a.tableA_ID = b1.tableA_ID left join
b2
on a.tableA_ID = b2.tableA_ID ;
It's a bit unclear what you're trying to do, but given your expected results, union all might work:
select a.tableA_ID,
b.tableB1_ID as TableB1,
null as TableB2
from a join b on a.tableA_ID = b.tableA_ID
union all
select a.tableA_ID,
null,
b2.tableB2_ID
from a join b2 on a.tableA_ID = b2.tableA_ID
This question already has answers here:
How can I do a FULL OUTER JOIN in MySQL?
(15 answers)
Closed 8 years ago.
I have 3 tables Category,subcategory and product
how can I do full outer join between these tables but on version 5.1.69 (mysql)
[category.cat_id] => category table l
[subcategory.subcat_id] => subcategory tab1e 2
[product.subcat_id] => product table 3
Have cat_id as a foreign key in Subcategory table and subcat_id as a foreign key in product table.
here is an example inner join:
Select product_name,cat_name,subcat_name
from Product p,Category c,Subcategory s
where c.cat_id=s.cat_id and p.subcat_id=s.subcat_id and product_id=121
I have two tables sharing the same attribute 'attr'. The domain of values in attribute 'attr' in Table cust are a subset of domain of values in attribute 'attr' in table sales.
E.g Table cust containing 940, 8575, 454, 86869, 856869, 9686 as values in attribute 'attr', while Table sales contains 454, 86869, 856869, 8756, 5324, 946707, 9779. Also table cust contains 1 billion rows while Table sale contains 1 trillion rows. In order to perform the join I tried the following command:
select * from cust where cust.attr in(select distinct attr from table sales)
However, this turns out to be very slow..is there some efficient way to perform the join
1 billion rows !! ... 1 trillion rows !!
Create index on attr column in both the tables
ALTER TABLE cust ADD INDEX (attr);
ALTER TABLE sales ADD INDEX (attr);
Then do join using below syntax.
select * from cust c
inner join sales s
on c.attr=s.attr
SELECT cust.*
FROM cust
INNER JOIN sales attr
ON sales.attr = cust.attr
WHERE [query conditions here]
Make sure cust.attr and sales.attr are indexed, too
This post on JOINs will be helpful: http://webduos.com/mysql-join-syntax/
I am trying to build a query for later inserting it in a table I'm building. I want the query to return 4 columns, which are 4 ids from different entities which relate with each other. I've got one table with the relations, but with varchars, and four tables where I already put the different occurences with an ID each:
TABLE RELATIONS:
A B C D
Sony Bravia 32" 1200€
JVC Whatever 15cm 200€
Samsung Galaxy 13" 500€
TABLE A:
id name
1 Sony
2 JVC
3 Samsung
TABLE B:
id name
1 Whatever
2 Galaxy
3 Bravia
TABLE C:
id name
1 13"
2 15cm
3 32"
TABLE D:
id name
1 200€
2 1200€
3 500€
Now, what I want to get with my query is:
QUERY RESULT:
A B C D
1 3 3 2
2 1 2 1
3 2 1 3
Firstly, I constructed this one:
SELECT DISTINCT A.id as A, B.id as B, C.id as C, D.id as D
FROM relations
INNER JOIN A ON A.name = relations.A
INNER JOIN B ON B.name = relations.B
INNER JOIN C ON C.name = relations.C
INNER JOIN D ON D.name = relations.D
It seems correct, but it takes sooooo long (maybe hours) to complete. The table sizes are (80,65000,1900,15) for the 4 entities, and 65000 for the relations table.
If I perform just one of the joins it takes 15ms, if I perform two of them 6-7 seconds, and with 3 o 4 the time increases exponentially. I think maybe the JOIN solution might be overkill for my situation, as I only need to "translate" the strings...
I already created an index for every "name" field in the four entity tables, as well as an index for relations.a,.b,.c,.d.
Curiously, it takes almost nothing if what I do is duplicate the a,b,c,d columns in the relations table and perform 4 UPDATE queries inserting the id into the duplicate field, matching its "parent"...but I'm sure there's gotta be a better way to do that...anyone has an idea?
Many thanks!
EXPLAIN result http://www.redlanemedia.com/explain.png