473,657 Members | 2,283 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

which sql is better on perfomance in DB2 and how conditional where clause works?

1 New Member
Hello and thanks in advance.

I have two table in DB2 (ver 8). table details are

table one(Cust_det) has two fields (Cust_cin and cust_name).
table two(cust_add_de t) has cust_cin and cust_address fields.

To join above two table which sql is better on perfomance point of view?
and how conditional where clause works?

Expand|Select|Wrap|Line Numbers
  1. select
  2.   A.cust_cin, B.cust_address
  3. from
  4.   Cust_det A, cust_add_det B
  5. where
  6.   A.Cust_cin = ?
  7. and
  8.   A.cust_cin=B.cust_cin
or
Expand|Select|Wrap|Line Numbers
  1. select
  2.   A.cust_cin, B.cust_address
  3. from
  4.   Cust_det A, cust_add_det B
  5. where
  6.   A.cust_cin=B.cust_cin
  7. and
  8.   A.Cust_cin = ?
Jan 13 '08 #1
3 3248
KiranKGone
6 New Member
The first option should work better, coz, the first predicate is indexable, the second one is not.. the resultant set of the where clause is filtered by the first predicate and then the second predicate is applied to the resultant set of the first.. Hope this helps !!
Jan 14 '08 #2
docdiesel
297 Recognized Expert Contributor
Hi,

both statements will be executed by DB2 at the very same speed. Before execution, DB2's internal optimizer is analyzing the SQL and will weigh each condition before setting up an execution plan, so that the SQL is executed the fastest way (in its point of view).

Besides, if you're searching for the field cust_cin, to speed up the query forget about the table Cust_det and just rely on table cust_add_det:
Expand|Select|Wrap|Line Numbers
  1. select
  2.   C.Cust_CIN, C.Cust_Address
  3. from
  4.   Cust_Add_Det C
  5. where
  6.   C.Cust_cin = ?
  7.  
Furthermore, if you need to join those two tables in a query,
  • define Cust_CIN as primary key in both tables (this sets up indexes)
  • define a foreign key constraint between Cust_CIN in both tables
  • use an INNER JOIN within your SQL statement:
    Expand|Select|Wrap|Line Numbers
    1. SELECT ...
    2. FROM       tab1 A
    3. INNER JOIN tab2 B
    4. ON    A.cin=B.cin
    5. WHERE ...
Regards,

Bernd

P.S. I added some code tags to your post for better readability.
Jan 14 '08 #3
docdiesel
297 Recognized Expert Contributor
Btw., you may see how DB2 and its optimizer are working by using the EXPLAIN functionality. Open the control center, right-click the database and choose the EXPLAIN option. Enter your sql statement and click OK. You will be presented a graphical plan how DB2 would be retrieving the date, using table scans, index scans etc. On this plan, you may also see where the most time is needed to fetch the data (in DB2 internal units "timerons") , means, where the biggest resource for optimization by change of design lies (e.g. adding indexes, splitting tables).

Regards,

Bernd
Jan 14 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

7
3508
by: Guy Hocking | last post by:
Hi there, I have a problem in my ASP/SQL Server application i am developing, i hope you guys can help. I have a ASP form with list boxes populated by SQL tables. When a user selects a value in a list box and submits the form the value is put into a session variable and the relevant page is displayed (in accordance to one of the list boxes). The page is then displayed with the relevant SQL data. So far i have got the
1
12352
by: RobbieGotNeeds | last post by:
Are there any conditional aggregate functions, such as SUM()? An example would probably be the best way to describe what I'm trying to do... I have the following table, named Orders, with the following records: ItemNo qty_ord paid ------ ----------- ------ T101B 1 199.00
1
3734
by: Sid Gudes | last post by:
Hi All, I'm converting a CLI application from Teradata to DB2. In Teradata, there is a conditional rollback, e.g. rollback where customer='1234' and ID=123 This does the rollback only if the "Where" condition selects one or more rows. I didn't see a "where" clause in the syntax of DB2's Rollback statement. Is there some other way to achieve this result?
3
2308
by: Chuck Reed | last post by:
I am working on a sales report where I show weekly sales by category for each of the 52 weeks in the year. Each record in my table/report has the 52 weeks of sales in it. I want to highlight to top five weeks of sales for each category by using a conditonal format to highlight these top five weeks. I've been trying to find a way to do this but have not been sucessful looking through many books on Access and in reading the user groups. If...
1
942
by: John | last post by:
Hi I have created a simple data entry form by dragging a table onto a form and letting vs2005 generate all bindings etc. All works fine. Now I need to be able to view records selectively ie sometimes I need to view/edit only 'Current' accounts and other times I need to view/edit only 'Prospect' accounts. How do I implement this selective behaviour from this point? Thanks
15
1584
by: Peter Liu | last post by:
someting like return (a==b)?a:((b==c)?:..)................ thanks
5
2655
by: pwiegers | last post by:
Hi, I'm trying to use the result of a conditional statement in a where clause, but i'm getting 1)nowhere 2) desperate :-) The query is simple: -------- SELECT idUser, (@ageraw:=YEAR(CURRENT_DATE()) - YEAR(dateofbirth) -
2
1424
by: Abandoned | last post by:
Hi.. I want to do index in postgresql & python. My table: id(int) | id2(int) | w(int) | d(int) My query: select id, w where id=x and id2=y (sometimes and d=z) I have too many insert and select operation on this table. And which index type can i use ? Btree, Rtree, Gist or Hash ?
6
5029
by: Jared | last post by:
Consider the following two functionally identical example queries: Query 1: DECLARE @Name VARCHAR(32) SET @Name = 'Bob' SELECT * FROM Employees WHERE = CASE WHEN @Name IS NULL THEN ELSE @Name END Query 2:
0
8411
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8323
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8838
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8513
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8613
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7351
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6176
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2740
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.