473,513 Members | 2,677 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to speed up this SQL statement

I want to execute the following SQL statement for a single emp.emp_id.

Just adding and emp.emp_id=256 to the end of the statement results in
a SQL statement that on average takes 5.5 seconds.

I've tried various permutations of the statement where I add "and
emp.emp_id=256" or "em.emp_id=256" but fundamentally, I don't know how
to approach optimizing this statement for a single row.

Your tips would be of help.

Thanks.
SELECT emp.emp_id,
emp.first_name,
emp.middle,
emp.last_name,
emp.login,
emp.email,
emp.emp_number,
emp.cube,
emp.phone,
emp.workstation,
coalesce(departments.corp_department, '') corp_department,
coalesce(groups.corp_group, '') corp_group,
building.building ,
emp_type.emp_type,
user_type.user_type,
emp.office,
emp.my_favourites,
emp.flag,
emp.last_visited_date,
emp.no_of_visits FROM bobnet.employee emp
LEFT OUTER JOIN (SELECT em.emp_id, gl.group_name corp_department FROM
employee em
LEFT OUTER JOIN emp_group eg ON em.emp_id=eg.emp_id
LEFT OUTER JOIN bobnet.group_lookup gl ON eg.group_id
=gl.group_id
WHERE gl.attribute='Corp Department' ) departments
ON emp.emp_id = departments.emp_id
LEFT OUTER JOIN (SELECT em.emp_id, gl.group_name corp_group FROM
employee em
LEFT OUTER JOIN bobnet.emp_group eg
ON em.emp_id=eg.emp_id
LEFT OUTER JOIN group_lookup gl
ON eg.group_id =gl.group_id
WHERE gl.attribute='Corp Group' ) groups
ON emp.emp_id=groups.emp_id
LEFT OUTER JOIN (SELECT em.emp_id, gl.group_name building FROM
employee em
LEFT OUTER JOIN bobnet.emp_group eg
ON em.emp_id=eg.emp_id
LEFT OUTER JOIN group_lookup gl
ON eg.group_id =gl.group_id
WHERE gl.attribute='Building' ) building
ON emp.emp_id=building.emp_id
LEFT OUTER JOIN (SELECT em.emp_id, gl.group_name emp_type FROM
employee em
LEFT OUTER JOIN bobnet.emp_group eg
ON em.emp_id=eg.emp_id
LEFT OUTER JOIN group_lookup gl
ON eg.group_id =gl.group_id
WHERE gl.attribute='Corp EmpType' ) emp_type
ON emp.emp_id=emp_type.emp_id
LEFT OUTER JOIN (SELECT em.emp_id, gl.group_name user_type FROM
employee em
LEFT OUTER JOIN bobnet.emp_group eg
ON em.emp_id=eg.emp_id
LEFT OUTER JOIN group_lookup gl
ON eg.group_id =gl.group_id
WHERE gl.attribute='UserType' ) user_type
ON emp.emp_id=user_type.emp_id
where emp.phone <'111-111-1111' and user_type.user_type='Corporate'

Jul 7 '07 #1
8 3266
gi*******************@yahoo.com wrote:
I want to execute the following SQL statement for a single emp.emp_id.

Just adding and emp.emp_id=256 to the end of the statement results in
a SQL statement that on average takes 5.5 seconds.

I've tried various permutations of the statement where I add "and
emp.emp_id=256" or "em.emp_id=256" but fundamentally, I don't know how
to approach optimizing this statement for a single row.

Your tips would be of help.
First, do you have sufficient indexes and updated statistics? You can
use db2advis to see what db2 thinks of indexes (dont take it's word for
granted, but it is a good help).

Second, dont do outer joins unless you really need them. Do you really
need to do outer joins in the subselects? Example:

LEFT OUTER JOIN
(SELECT em.emp_id, gl.group_name corp_department
FROM employee em
LEFT OUTER JOIN emp_group eg
ON em.emp_id=eg.emp_id
LEFT OUTER JOIN bobnet.group_lookup gl
ON eg.group_id=gl.group_id
WHERE gl.attribute='Corp Department' ) departments
....

Couldnt you do this instead?

LEFT OUTER JOIN
(SELECT em.emp_id, gl.group_name corp_department
FROM employee em
INNER JOIN emp_group eg
ON em.emp_id=eg.emp_id
INNER JOIN bobnet.group_lookup gl
ON eg.group_id=gl.group_id
WHERE gl.attribute='Corp Department' ) departments
....

etc for the other subqueries.
/Lennart

[...]
Jul 7 '07 #2
gi*******************@yahoo.com wrote:
I want to execute the following SQL statement for a single emp.emp_id.

Just adding and emp.emp_id=256 to the end of the statement results in
a SQL statement that on average takes 5.5 seconds.

I've tried various permutations of the statement where I add "and
emp.emp_id=256" or "em.emp_id=256" but fundamentally, I don't know how
to approach optimizing this statement for a single row.

Your tips would be of help.
Is (for example) bobnet.employee <employee?
/Lennart
Jul 7 '07 #3
On Fri, 06 Jul 2007 17:26:27 -0700, "gi*******************@yahoo.com"
<gi*******************@yahoo.comwrote:
>I want to execute the following SQL statement for a single emp.emp_id.

Just adding and emp.emp_id=256 to the end of the statement results in
a SQL statement that on average takes 5.5 seconds.

I've tried various permutations of the statement where I add "and
emp.emp_id=256" or "em.emp_id=256" but fundamentally, I don't know how
to approach optimizing this statement for a single row.

Your tips would be of help.
That a lot of OUTER JOINs. Perhaps you can use sub-selects instead?

B.
Jul 9 '07 #4
On Jul 7, 5:09 pm, Lennart <erik.lennart.jons...@gmail.comwrote:
>
Is (for example) bobnet.employee <employee?

/Lennart
If it is true that bobnet.employee = employee and bobnet.emp_group =
emp_group, original query can be much simplified.

Comparing each subquery, all have same phrase(by ignoring schema is
specified or not) as following.
LEFT OUTER JOIN (SELECT em.emp_id, gl.group_name XXXXXXXXX FROM
bobnet.employee em
LEFT OUTER JOIN emp_group eg
ON em.emp_id=eg.emp_id
......
)
yyyyyyyy
ON emp.emp_id = yyyyyyyy.emp_id
I think those are redundant. Following query might be equivalent
original query.
SELECT emp.emp_id,
emp.first_name,
emp.middle,
emp.last_name,
emp.login,
emp.email,
emp.emp_number,
emp.cube,
emp.phone,
emp.workstation,
coalesce(departments.group_name, '') corp_department,
coalesce(groups.group_name, '') corp_group,
building.group_name,
emp_type.group_name,
user_type.group_name,
emp.office,
emp.my_favourites,
emp.flag,
emp.last_visited_date,
emp.no_of_visits
FROM bobnet.employee emp
LEFT OUTER JOIN
bobnet.emp_group eg
ON emp.emp_id=eg.emp_id
LEFT OUTER JOIN
group_lookup departments
ON eg.group_id = departments.group_id
AND departments.attribute='Corp Department'
LEFT OUTER JOIN
group_lookup groups
ON eg.group_id =groups.group_id
AND groups.attribute='Corp Group'
LEFT OUTER JOIN
group_lookup building
ON eg.group_id =building.group_id
AND building.attribute='Building'
LEFT OUTER JOIN
group_lookup emp_type
ON eg.group_id =emp_type.group_id
AND emp_type.attribute='Corp EmpType'
LEFT OUTER JOIN
group_lookup user_type
ON eg.group_id =user_type.group_id
AND user_type.attribute='UserType'
WHERE emp.phone <'111-111-1111'
AND user_type.group_name='Corporate'
;

And as Lennart already pointed out, it is worth to replace some LEFT
OUTER JOIN with INNER JOIN, if possible.
I thought some candidates to replace with INNER JOIN are
1) LEFT OUTER JOIN
bobnet.emp_group eg
ON emp.emp_id=eg.emp_id

2) LEFT OUTER JOIN
group_lookup building
ON eg.group_id =building.group_id
AND building.attribute='Building'

3) LEFT OUTER JOIN
group_lookup emp_type
ON eg.group_id =emp_type.group_id
AND emp_type.attribute='Corp EmpType'

4) LEFT OUTER JOIN
group_lookup user_type
ON eg.group_id =user_type.group_id
AND user_type.attribute='UserType'

Because, 1) is used in all subquery. So, it might be that there is
always matching row.
For 2), 3) and 4), COALESCE is not used in SELECT list, while for
departments.group_name and groups.group_name, COALESCE is used.

These guesses might be too over imaginative.
Only, gimme_this_gimme_that@yah*oo.com(originator of this thread)
would know truth.

Jul 9 '07 #5
Tonkuma wrote:
On Jul 7, 5:09 pm, Lennart <erik.lennart.jons...@gmail.comwrote:
>>
Is (for example) bobnet.employee <employee?

/Lennart
If it is true that bobnet.employee = employee and bobnet.emp_group =
emp_group, original query can be much simplified.
Yes, that is my thought exactly

I think those are redundant. Following query might be equivalent
original query.
SELECT emp.emp_id,
emp.first_name,
emp.middle,
emp.last_name,
emp.login,
emp.email,
emp.emp_number,
emp.cube,
emp.phone,
emp.workstation,
coalesce(departments.group_name, '') corp_department,
coalesce(groups.group_name, '') corp_group,
building.group_name,
emp_type.group_name,
user_type.group_name,
emp.office,
emp.my_favourites,
emp.flag,
emp.last_visited_date,
emp.no_of_visits
FROM bobnet.employee emp
LEFT OUTER JOIN
bobnet.emp_group eg
ON emp.emp_id=eg.emp_id
LEFT OUTER JOIN
group_lookup departments
ON eg.group_id = departments.group_id
AND departments.attribute='Corp Department'
LEFT OUTER JOIN
group_lookup groups
ON eg.group_id =groups.group_id
AND groups.attribute='Corp Group'
LEFT OUTER JOIN
group_lookup building
ON eg.group_id =building.group_id
AND building.attribute='Building'
LEFT OUTER JOIN
group_lookup emp_type
ON eg.group_id =emp_type.group_id
AND emp_type.attribute='Corp EmpType'
LEFT OUTER JOIN
group_lookup user_type
ON eg.group_id =user_type.group_id
AND user_type.attribute='UserType'
WHERE emp.phone <'111-111-1111'
AND user_type.group_name='Corporate'
;
It might even be simplified further (havent given it too much thought
though), by joining group_lookup only once and move the attribute='Corp
EmpType' etc to a case stmt in the select clause

SELECT emp.emp_id,
[...]
case group_lookup.attribute
when 'Building' then ... else ... end
case group_lookup.attribute
when ...

But as you point out below, it's hard to tell so we can only guess.

/Lennart


And as Lennart already pointed out, it is worth to replace some LEFT
OUTER JOIN with INNER JOIN, if possible.
I thought some candidates to replace with INNER JOIN are
1) LEFT OUTER JOIN
bobnet.emp_group eg
ON emp.emp_id=eg.emp_id

2) LEFT OUTER JOIN
group_lookup building
ON eg.group_id =building.group_id
AND building.attribute='Building'

3) LEFT OUTER JOIN
group_lookup emp_type
ON eg.group_id =emp_type.group_id
AND emp_type.attribute='Corp EmpType'

4) LEFT OUTER JOIN
group_lookup user_type
ON eg.group_id =user_type.group_id
AND user_type.attribute='UserType'

Because, 1) is used in all subquery. So, it might be that there is
always matching row.
For 2), 3) and 4), COALESCE is not used in SELECT list, while for
departments.group_name and groups.group_name, COALESCE is used.

These guesses might be too over imaginative.
Only, gimme_this_gimme_that@yah*oo.com(originator of this thread)
would know truth.
Jul 9 '07 #6
Thanks Lenart.

Yes the outer joins are necessary. There are many instances where a
department, building, emp_type, or user_type doesn't exist for the
employee.

Jul 12 '07 #7
The number of rows of this statement is not *exactly* the same as the
statement I posted.
It appears return nearly the same results and appears to return
something closer to what is actually needed.

Nice Tip!!!!

Thanks.
I think those are redundant. Following query might be equivalent
original query.
SELECT emp.emp_id,
emp.first_name,
emp.middle,
emp.last_name,
emp.login,
emp.email,
emp.emp_number,
emp.cube,
emp.phone,
emp.workstation,
coalesce(departments.group_name, '') corp_department,
coalesce(groups.group_name, '') corp_group,
building.group_name,
emp_type.group_name,
user_type.group_name,
emp.office,
emp.my_favourites,
emp.flag,
emp.last_visited_date,
emp.no_of_visits
FROM bobnet.employee emp
LEFT OUTER JOIN
bobnet.emp_group eg
ON emp.emp_id=eg.emp_id
LEFT OUTER JOIN
group_lookup departments
ON eg.group_id = departments.group_id
AND departments.attribute='Corp Department'
LEFT OUTER JOIN
group_lookup groups
ON eg.group_id =groups.group_id
AND groups.attribute='Corp Group'
LEFT OUTER JOIN
group_lookup building
ON eg.group_id =building.group_id
AND building.attribute='Building'
LEFT OUTER JOIN
group_lookup emp_type
ON eg.group_id =emp_type.group_id
AND emp_type.attribute='Corp EmpType'
LEFT OUTER JOIN
group_lookup user_type
ON eg.group_id =user_type.group_id
AND user_type.attribute='UserType'
WHERE emp.phone <'111-111-1111'
AND user_type.group_name='Corporate'
;
Jul 12 '07 #8
On Thu, 12 Jul 2007 13:43:32 -0700, "gi*******************@yahoo.com"
<gi*******************@yahoo.comwrote:
>Thanks Lenart.

Yes the outer joins are necessary. There are many instances where a
department, building, emp_type, or user_type doesn't exist for the
employee.
Use sub-queries.

B.
Jul 13 '07 #9

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

Similar topics

4
1873
by: chiefprogramer | last post by:
How to build up a good frame for a mathematical system and it's ultra-high-speed? Althought In PHP 5 there is a new Object Model. But I think the new Object Model is still very slow >< Just like statement "f();" cost 1148 ns "fffffffffff();" cost 1273 ns (1 function char cost 13 ns) "$z->f();" cost 1865 ns
28
2564
by: Maboroshi | last post by:
Hi I am fairly new to programming but not as such that I am a total beginner From what I understand C and C++ are faster languages than Python. Is this because of Pythons ability to operate on almost any operating system? Or is there many other reasons why? I understand there is ansi/iso C and C++ and that ANSI/ISO Code will work on any...
52
3797
by: Neuruss | last post by:
It seems there are quite a few projects aimed to improve Python's speed and, therefore, eliminate its main limitation for mainstream acceptance. I just wonder what do you all think? Will Python (and dynamic languages in general) be someday close to compiled languages speed? What will be the future of Psyco, Pypy, Starkiller, Ironpython and...
5
2708
by: JENS CONSER | last post by:
Hello NG, We have a performance problem in using a client server solution based on MS SQL-Server 2000 through a VPN tunnel (via broadband internet connection). The SQL Server is running on a Windows 2003 Server which is configured as VPN server as well. We figured out that not the performance
60
10078
by: Neil | last post by:
I have a situation with an ODBC linked view in an Access 2000 MDB with a SQL 7 back end. The view is scrolling very slowly. However, if I open the view in an ADP file, it scrolls quickly. I needed to use an ODBC link for the view because it needs to be editable. Otherwise, I would have used a pass-through query. In previous discussions...
11
9169
by: Sezai YILMAZ | last post by:
Hello I need high throughput while inserting into PostgreSQL. Because of that I did some PostgreSQL insert performance tests. ------------------------------------------------------------ -- Test schema create table logs ( logid serial primary key, ctime integer not null,
16
1691
by: byteschreck | last post by:
I recently switched temporarily from C# to VB.NET to see what the differences are. To my surprise I am *much* faster with VB.NET. I don't know why, pressing the shift key to capitalize letters slows me down tremendously, among having to type a semicolon after each statement, not having intelligent auto-complete and auto-indentation and not...
1
1439
by: =?ISO-8859-15?Q?Ma=EBl_Benjamin_Mettler?= | last post by:
Hello Python-List I hope somebody can help me with this. I spent some time googling for an answer, but due to the nature of the problem lots of unrelevant stuff shows up. Anyway, I reimplemented parts of TigerSearch ( http://www.ims.uni-stuttgart.de/projekte/TIGER/TIGERSearch/ ) in Python. I am currently writing the paper that goes along...
8
6464
by: SaltyBoat | last post by:
Needing to import and parse data from a large PDF file into an Access 2002 table: I start by converted the PDF file to a html file. Then I read this html text file, line by line, into a table using a code loop and an INSERT INTO query. About 800,000 records of raw text. Later, I can then loop through and parse these 800,000 strings into...
47
1385
by: Mike Williams | last post by:
I'm trying to decide whether or not I need to move to a different development tool and I'm told that VB6 code, when well written, can be ten times faster than vb.net, but that if its badly written it can be ten times slower. Is that correct? I'm quite competent at writing code myself and so most of my code will be quite well written, and I...
0
7269
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7177
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7394
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7559
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7542
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5701
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5100
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3237
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
470
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.