473,382 Members | 1,357 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,382 software developers and data experts.

complex mysql 3.23 LEFT JOIN question

Here's one for pathological SQL programmers.

I've got a table of things called elements. They're components, sort of
like amino acids, which come together to form complex web pages -- as
nodes in trees which form parent-child relationships, sort of like
newsgroups. For example, the parent_id field points to another element.
Indent_level is there for denormalization purposes, to avoid costly
recursive issues in querying. The element_page_id allows pages to link
to other pages via elements (pretty complex, I daresay, but don't worry
about that part).

Each row must have certain information, but may only have one value (a
numeric foreign key reference) among the following: resource, location,
service, staff, subject, and element_page_id. The others are NULL.
I've marked them below betweens rows of asterisks. In this example,
only service_id actually needs to perform a LEFT JOIN to fetch
information about it:

For example:
element_id: 6563
page_id: 207
parent_id: 6561
*********************
resource_id: NULL
location_id: NULL
service_id: 22
staff_id: NULL
subject_id: NULL
element_page_id: NULL
*********************

label: NULL
label_url: NULL
element_descr: NULL
element_size: NULL
element_order: 22
indent_level: 1

What I do is assemble a query to display the elements in element_order
for a particular page_id. At one point in the query I do this:

LEFT JOIN resource r using (resource_id)
LEFT JOIN service v on e.service_id = v.service_id
LEFT JOIN location l on e.location_id = l.location_id
LEFT JOIN staff s on e.staff_id = s.staff_id
LEFT JOIN subject b on e.subject_id = b.subject_id
LEFT JOIN page pe on e.element_page_id = pe.page_id

I need to get values unique to each sort of (heterogenous) element in
order to display the assembled page. For example, the name and e-mail
of a staffperson, the title and URL of a resource, the hours of a
location, etc.

My question is how to optimize this approach. Correct me if I'm
mistaken, but a LEFT JOIN where the left side is NULL will never result
in a full table scan of the right-hand side -- so what I'm doing here
isn't terribly intensive (doing a LEFT JOIN on everything, although only
one actually applies).

Lacking the CASE statement, is there any other way to optimize this?

I'm using mySQL 3.23. Any suggestions on optimization would be appreciated.

Paul Bramscher
br******@umn.edu
Jul 20 '05 #1
1 3418
Paul Bramscher wrote:
LEFT JOIN resource r using (resource_id)
LEFT JOIN service v on e.service_id = v.service_id
LEFT JOIN location l on e.location_id = l.location_id
LEFT JOIN staff s on e.staff_id = s.staff_id
LEFT JOIN subject b on e.subject_id = b.subject_id
LEFT JOIN page pe on e.element_page_id = pe.page_id

I need to get values unique to each sort of (heterogenous) element in
order to display the assembled page. For example, the name and e-mail
of a staffperson, the title and URL of a resource, the hours of a
location, etc.

My question is how to optimize this approach.


My first reaction is that this is one of the many situations where it's
more trouble than it's worth to try to combine many data operations into
a single SQL query. People try to do that too often, and create very
expensive code; expensive for the programmer to develop and maintain,
and computationally expensive for MySQL to execute.

If you drop the requirement to do this in a single query containing six
outer joins, I predict it will execute a lot faster, even though it
takes multiple queries to execute it.

Instead, do six separate queries, each an inner join.
For instance:

SELECT ... FROM elements INNER JOIN resource USING (resource_id)
WHERE elements.resource_id IS NOT NULL;
SELECT ... FROM elements INNER JOIN service USING (service_id)
WHERE elements.service_id IS NOT NULL;
SELECT ... FROM elements INNER JOIN location USING (location_id)
WHERE elements.location_id IS NOT NULL;
SELECT ... FROM elements INNER JOIN staff USING (staff_id)
WHERE elements.staff_id IS NOT NULL;
SELECT ... FROM elements INNER JOIN subject USING (subject_id)
WHERE elements.subject_id IS NOT NULL;
SELECT ... FROM elements INNER JOIN page USING (page_id)
WHERE elements.page_id IS NOT NULL;

You might also consider redesigning your database so that the keys go
the other direction:

CREATE TABLE element_to_page (
element_id INTEGER NOT NULL REFERENCES element,
page_id INTEGER NOT NULL REFERENCES page,
PRIMARY KEY (element_id, page_id),
);

Also create mapping tables for element_to_resource, element_to_service, etc.

You can use these mapping tables to create zero, one, or more references
between a given element and one of the other tables. Then you can take
those six foreign keys out of your elements table.

Regards,
Bill K.
Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: aaron | last post by:
I have a question about (i think) joining. If I have a table in a database that has this info: key - name - favorite 1 - john - 2 2 - judy - 3 3 - joe - 1 the favorite icecream...
5
by: Adam i Agnieszka Gasiorowski FNORD | last post by:
I need help width formulating the most effective (in terms of processing time) SQL query to count all the "new" documents in the repository, where "new" is defined as "from 00:00:01 up to...
6
by: Xenophobe | last post by:
I know this isn't a MySQL forum, but my question is related to a PHP project. I have two tables. table1 table2 "table1" contains 2 columns, ID and FirstName:
4
by: NotGiven | last post by:
I have two tables, Client and Project, related by clientID. I want to display a table with total CURRENT projects and total COMPLETED projects per client. I'd like for it to show like this: ...
0
by: Soefara | last post by:
Dear Sirs, I am experiencing strange results when trying to optimize a LEFT JOIN on 3 tables using MySQL. Given 3 tables A, B, C such as the following: create table A ( uniqueId int not...
0
by: Phil Powell | last post by:
I have a rather complicated query with a combination of LEFT JOINs and two MATCHES where the first match is non-boolean to get the accurate score, the second to search as boolean: SELECT...
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...
1
by: Sam | last post by:
For this example I have eliminated (most of) the unreferenced columns from the table definitions. My problem is: how does one formulate a select statement or create a view in SQL (I'm using...
5
by: Bob Bedford | last post by:
Hello all, I'm creating an invoice process for my website. I'm trying to show the invoice processing on a simple way. Let's explain how: I'm creating a table with the first column having...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.