If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Too many left joins? - newbie stuff, help needed.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-23-06, 16:35
Amn Amn is offline
Registered User
 
Join Date: Jul 2004
Posts: 8
Post Too many left joins? - newbie stuff, help needed.

Hi all.

I would like to get your opinion on how effective my query is, i am sure gurus and alike would see immediately if I am doing some newbie mistakes. I will try to explain my simple database.

basically a table of products exists, where each product has a name, price, qty, unit of qty measure, package, and distributor. The website which operates on this information is multi-lingual, so naturally all text has to have several versions - that includes product names, product package names, and product distributor names. A table for packages and one for distributors also exist.

Code:
create table multi_language_text
(
	id int not null,
	txt text character set utf8,
	language_id int not null,
	primary key (id, language_id)
);

create table product_makers
(
	id int not null,
	primary key (id),
	foreign key (id) references multi_language_text(id)
);

create table product_packages
(
	id int not null,
	primary key (id),
	foreign key (id) references multi_language_text(id)
) engine=InnoDB;

create table products
(
	id int not null,
	maker_id int not null,
	package_id int not null,
	qty real not null,
	unit char not null,
	price real not null,
	primary key (id, maker_id, package_id, qty, unit),
	foreign key (id) references multi_language_text(id),
	foreign key (package_id) references product_packages(id),
	foreign key (maker_id) references product_makers(id)

);
Now, to retrieve the list of all products, with names, package names, and distributor names instead of numbers, i use following query:

Code:
select
	p.id as id, pl.txt as name, pml.txt as maker, ppl.txt as package, p.qty as qty, p.unit as unit, p.price as price
from
	products as p
	left join
	multi_language_text as pl
	on p.id = pl.id
	left join
	multi_language_text as pml
	on p.maker_id = pml.id
	left join
	multi_language_text as ppl
	on p.package_id = ppl.id
where pl.language_id =1 and pml.language_id =1 and ppl.language_id =1;
Do you think this is over the top? I am fairly new to SQL (about 1-2 years of here-and-there experience making it maybe 3 months pure SQL time), but I did my reading, and i am also a programmer.

Should i replace the join thing with subqueries ? Also isn't it possible to eliminate text for other languages than the chosen (=1) more effectively, without doing it three times ?

The database is MySQL 4.1, so views are out of the questions..

Thanks.
Reply With Quote
  #2 (permalink)  
Old 11-23-06, 17:22
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
have you tried running those statements? because i think they will fail

this won't work --
Code:
foreign key (id) references multi_language_text(id)
since the multi_language_text table has a composite primary key, you cannot reference only part of it
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 11-27-06, 11:26
Amn Amn is offline
Registered User
 
Join Date: Jul 2004
Posts: 8
It does (MySQL?), although when writing the query i was sure that the standard would not allow it.

Anyways, apart from the constraints, do you think the left join style query is any effective ?
Reply With Quote
  #4 (permalink)  
Old 11-27-06, 11:47
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
the tables have to be InnoDB in order for the foreign keys to work

as far as your left join query is concerned, i cannot comment on it because i don't understand your table design
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 11-27-06, 13:10
Amn Amn is offline
Registered User
 
Join Date: Jul 2004
Posts: 8
All tables are supposed to be InnoDB. I dont know how it slipped out only product_packages specifies the InnoDB engine... In my sql files all tables specify InnoDB engine.

The table design is (poorly?) explained in the topic.

Thanks for the help. For now I left the query as is, because the practical speed implications are negliagible (for now).
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On