Webmaster Forum

Go Back   Webmaster Forum > Web Development > Coding Forum

Coding Forum Problems with your code? Discuss coding issues, including JavaScript, PHP & MySQL, HTML & CSS, Flash & ActionScript, and more.


Reply
 
LinkBack Thread Tools Display Modes
Old 08-04-2009, 03:57 AM   #1 (permalink)
Junior Member
 
Join Date: 06-29-09
Posts: 19
iTrader: 0 / 0%
Latest Blog:
None

gilbertsavier in the red
Query Optimization

Hello,
This is my first article and so please provide comments on it (good or bad).

The design of the database is one of the most important factors in the performance of the database and with a good database design you also need optimal queries to perform optimally. Everyone wants the performance of their database to be optimal but does not concentrate on designing a query. They just write the query depending on the only major factor. What I want. They don’t consider that even the same thing can be achieved with some alternate queries and in more efficient manner.

Let’s take a very simple 3 table example, Employee, Department and Sub-Department. Schema of

Employee table
1. Employee

1. EmpId
2. SDId
3. EmpName
4. EmpAdd
5. EmpPhone


Sub-Department table
2. SubDepartment

1. SDId
2. DeptId
3. SDName


Department table
3. Department

1. DeptId
2. DeptName


The relationship between the Employee and Sub-Department table is one or more sub-departments have zero or more employees or in other words zero or more employees work in one or more Sub-Departments. The relationship between Department and Sub-Department is one or more Sub-Department is within one and only one Department. The Entity Relationship diagram of the above scenario is


ER Diagram

Now with the above Database schema lets have a very basic queries which can give us the same results and we will analyze as to which is faster and better performing than the other one. Let’s take a very common example with the above database schema, to know all the Employees in a particular department. The common solution that comes to our mind is.

Code: sql

SELECT e.EmpId, e.SDId, e.EmpName, e.EmpAdd, e.EmpPhone
FROM Employee e, SubDepartment sd, Department d
WHERE e.SDId = sd.SDId
AND sd.DeptId = d.DeptId
AND d.DeptId = ‘MyDepartment’

The above solution of joining the 3 tables to give us the output for the MyDepartment as the id of the department is a very common but is a very expensive one. Any organization have an employee to department ratio quite high and joining an Employee to a Department table can be quite a undesirable operation.

If we analyze the above solution then we are getting the output for one and only one Department i.e. MyDepartment. So our aim should be to find the Sub-Department’s in our MyDepartment.
Code: sql

SELECT SDId
FROM SubDepartment
WHERE DeptId = ‘MyDepartment’

The above query gives us the Sub-Department ID’s of all the Sub department present in our concerned department ‘MyDepartment’. Now if we can have the all the employee details present in each subdepartment returned by the above query we get the results that we had with the join from 3 tables.

Code: sql

SELECT e.EmpId, e.SDId, e.EmpName, e.EmpAdd, e.EmpPhone
FROM Employee e
WHERE e.SDId
IN (SELECT SDId
FROM SubDepartment
WHERE DeptId = ‘MyDepartment’)

With the above solution we have avoided 3 table join and also a query to the department table with introduction of a sub query. By avoiding the joins of a tables what we have achieved is no Department table in the query to retrieve the results but managed to provide the same output. Now we have some limitations with the above solutions.

If you are a web developer and if your client is using MySQL and that also there are chances that he might not be using version 5 then you are in trouble as you cannot use Sub queries. Then you can even write the above query using any programming language and avoid even the sub-query. Let’s take the simple example of PHP and MySQL.

Code: PHP

//SQL to give the sub-departments of the queries DeptId
$sql_sub_dept = "SELECT SDId FROM SubDepartment WHERE DeptId = $DeptID";
//Main SQL to give us the Employee of a particular department here $DeptID
$sql_main_dept = " SELECT e.EmpId, e.SDId, e.EmpName, e.EmpAdd, e.EmpPhone FROM Employee e WHERE Employee. SDId IN (";

//Execute the SQL and concatenate each subdepartment ID
$result = mysql_query ($sql_sub_dept);
while($rec = mysql_fetch_array($result))
{
$ sql_main_dept.= $rec["SDId"] ;
$ sql_main_dept.= ',';
}
//Remove the last “,” and end the bracket to complete the SQL.
$sql_main_dept = trim($sql_main_dept,",");
$sql_main_dept.= ")";
__________________
Thanks & regards,
Lokananth
<a href="http://www.mioot.com">Live Chat Software</a> By miOOt
gilbertsavier is offline  
Add Post to del.icio.us
Reply With Quote
Old 08-04-2009, 08:06 AM   #2 (permalink)
v7n Mentor
 
Izzmo's Avatar
 
Join Date: 11-01-03
Location: Kansas City
Posts: 1,338
iTrader: 0 / 0%
Latest Blog:
Starting p90x today

Izzmo is a highly respected web proIzzmo is a highly respected web proIzzmo is a highly respected web proIzzmo is a highly respected web proIzzmo is a highly respected web proIzzmo is a highly respected web proIzzmo is a highly respected web proIzzmo is a highly respected web proIzzmo is a highly respected web proIzzmo is a highly respected web proIzzmo is a highly respected web pro
Send a message via ICQ to Izzmo Send a message via AIM to Izzmo Send a message via MSN to Izzmo Send a message via Yahoo to Izzmo
Sorry, I didn't fully read the article. The forum is mainly for solving issues/problems one might be having with their script, not really for articles on how to do something.

You might try posting this on a website such as http://www.coderprofile.com.
__________________
Izzmo
Coding Guru Extraordinaire
ZeroWeb Hosting & Design - Customizable hosting for every type of user!
Izzmo is offline  
Add Post to del.icio.us
Reply With Quote
Old 08-04-2009, 11:24 PM   #3 (permalink)
v7n Mentor
 
Join Date: 07-24-06
Posts: 691
iTrader: 1 / 100%
Latest Blog:
None

nasty.web is just really nicenasty.web is just really nicenasty.web is just really nicenasty.web is just really nicenasty.web is just really nicenasty.web is just really nicenasty.web is just really nicenasty.web is just really nicenasty.web is just really nicenasty.web is just really nicenasty.web is just really nice
Oh and the article is quite misleading. E.g.

Instead of

Code:
... AND sd.DeptId = d.DeptId AND d.DeptId = ‘MyDepartment’
you could simply write
Code:
... AND sd.DeptId = ‘MyDepartment’
because you aren't using department data in your query anyway. Also left out join might be sometimes faster than cross join.
nasty.web is offline  
Add Post to del.icio.us
Reply With Quote
Go Back   Webmaster Forum > Web Development > Coding Forum

Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sql Query itHighway Web Design Lobby 1 03-17-2008 01:09 PM
SQL Query Sketch Coding Forum 9 12-13-2006 12:26 PM


Sponsor Links
Get exposure! Contextual Links V7N SEO Blog V7N Directory


All times are GMT -7. The time now is 03:46 AM.
© Copyright 2008 V7 Inc
Powered by vBulletin
Copyright © 2000-2009 Jelsoft Enterprises Limited.


Search Engine Optimization by vBSEO 3.3.0 ©2009, Crawlability, Inc.