473,782 Members | 2,454 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Query regarding Index

Suppose i have a table which holds thousands of records with the
following structure

CREATE TABLE "test "."T_CNTRY" (
"CNTRY_CDE" CHAR(2) NOT NULL ,
"CNTRY_NAME " VARCHAR(50) )
and i have Created an index like below ::

CREATE UNIQUE INDEX "testI "."Uindex1" ON "test "."T_CNTRY"
("CNTRY_CDE" ASC);
1.) Do i have to create an Index like this to make queries which make
use of this run faster

CREATE INDEX "testI "."Iindex1" ON "test "."T_CNTRY" ("CNTRY_CDE"
ASC);

OR

2.) Creating the unique index/primary key index will serve the purpose
of making queries run faster

thanks in advance

Feb 28 '06 #1
15 6482
rAinDeEr wrote:
Suppose i have a table which holds thousands of records with the
following structure

CREATE TABLE "test "."T_CNTRY" (
"CNTRY_CDE" CHAR(2) NOT NULL ,
"CNTRY_NAME " VARCHAR(50) )
and i have Created an index like below ::

CREATE UNIQUE INDEX "testI "."Uindex1" ON "test "."T_CNTRY"
("CNTRY_CDE" ASC);
1.) Do i have to create an Index like this to make queries which make
use of this run faster

CREATE INDEX "testI "."Iindex1" ON "test "."T_CNTRY" ("CNTRY_CDE"
ASC);

OR

2.) Creating the unique index/primary key index will serve the purpose
of making queries run faster

A UNIQUE index is an INDEX + UNIQUEness.
DB2 knows that and will tell you:
db2 => create table t(c1 int not null);
DB20000I The SQL command completed successfully.
db2 => create unique index i1 on t(c1);
DB20000I The SQL command completed successfully.
db2 => create index i2 on t(c1);
SQL0605W The index was not created because an index "SRIELAU.I1 " with a
matching definition already exists. SQLSTATE=01550

As you see, DB2 figured that the second index is not needed.

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Feb 28 '06 #2
hhmmmm...

1.)this means i need not create a primary key when I have already
defined a UNIQUE + INDEX like what i have tested before.

CREATE UNIQUE INDEX "test"."IAWD_TY P01" ON "DB2GRA "."T_TYP"
("AWD_TYP_CD E" ASC) CLUSTER
DB20000I The SQL command completed successfully.

ALTER TABLE "test"."T__ TYP" ADD CONSTRAINT "PK_AWD_TYP " PRIMARY KEY
("AWD_TYP_CD E")
SQL0598W Existing index "DB2GRA.IAWD_TY P01" is used as the index for
the
primary key or a unique key. SQLSTATE=01550

2.) Does that mean that I can ask the Data Modeler not to make unique
index togther with a primary key
coz cerating unique indexes serves the purpose of both.

~ Thanks for the information

Feb 28 '06 #3
A primary key is a unique index + NOT NULL constraints on the key
column(s) ... ie , a unique key columns can have null values but
primary key columns cannot...

The primary key is also needed when you want to define a refrential
constraint ...
From the data model perspecitive, defining a primary key is a good

practise ... Other unique keys are defined as alternate keys ...
therefor, why not define a primary key and avoid defining Unique index
on the PK columns ?

Going back to your original question, make sure you do RUNSTATS on the
tables and indexes ... Otherwise, the optimizer might not use the index
HTH

Sathyaram

Feb 28 '06 #4
s.*********@goo glemail.com wrote:
A primary key is a unique index + NOT NULL constraints on the key
column(s) ... ie , a unique key columns can have null values but
primary key columns cannot...

The primary key is also needed when you want to define a refrential
constraint ...
From the data model perspecitive, defining a primary key is a good

practise ... Other unique keys are defined as alternate keys ...
therefor, why not define a primary key and avoid defining Unique index
on the PK columns ?

Going back to your original question, make sure you do RUNSTATS on the
tables and indexes ... Otherwise, the optimizer might not use the index

Further:
By defining the unique index explicitly before the primary key two
things are achieved:
1. You are in control of the index name
2. You can INCLUDE additional columns with the index.

Example for 2:
CREATE TABLE T(pk INT NOT NULL, c1 INT);
CREATE UNIQUE INDEX I_FOR_PK ON T(pk) INCLUDE(c1);
ALTER TABLE T ADD CONSTRAINTS PK PRIMARY KEY (pk);

DB2 will pick I_FOR_PK to enforce the primary key,
but now
SELECT c1 FROM T WHERE pk = ?
will be able to use an index only access.

In short I think it's a good thing to separate out the steps.
Don't mind the warnings.

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Feb 28 '06 #5
>By defining the unique index explicitly before the primary key two things are achieved:
1. You are in control of the index name
2. You can INCLUDE additional columns with the index


Can we add: 3. You can specify CLUSTER.

I am actually not familiar enough with CLUSTER to see if it makes sense
on the PRIMARY KEY.

B.

Feb 28 '06 #6
"Brian Tkatch" <Ma***********@ ThePentagon.com > wrote in message > Can we
add: 3. You can specify CLUSTER.

I am actually not familiar enough with CLUSTER to see if it makes sense
on the PRIMARY KEY.

B.


If your primary key has more than one column, and one or more of the columns
is a primary key on another parent table (there is a foreign key
relationship), then the primary key on the dependent table is a often a good
candidate for a clustering index.

Example:

ORDER table:
Order Number Integer PK
Customer Number (clustering index)
(There PK is a single column and not the clustering index)

ORDER DETAIL Table:
Order Number Integer PK (FK to ORDER Table)
Order Item Smallint PK
(the above PK with 2 columns should be the clustering index)
Feb 28 '06 #7
OK, i see.

Does CLUSTERing help BETWEENs?

That is, if the PK is a part number, part numbers are somewhat
sequential, and usually parts are grabbed with a BETWEEN, would
CLUSTERing put the part in order, helping a range scan quickly grab the
range from disk too?

B.

Feb 28 '06 #8
Yes. Cluster option directs DB2 to do the inserts using the index with
cluster option to determine the page in which the insert should go.
The idea is to keep inserted data clustered in the proper page.
When you use range delimiting predicates (between, <=,>=, like xx%, ..) on
the column(s) of the index then DB2 will likely use the index to grab the
pages with the rows you want.
After creating the index with cluster, if the data exists in the table, do
an offline reorg and db2 will sequence the rows following the index.
Inserts will try to keep the sequencing .
It would also be advisable to alter the table (or create it) with the
PCTFREE parm. This will give a percentage of free space in each page and
DB2 would likely find room in the proper page to place the insert.
db2 create table foo (col1 int,col2 char(xx),col3 .....) PCTFREE 20
db2 create unique index partno on foo (col1) cluster
db2 alter table foo primary key (col1) constraint pkcol1
Populate the table using a sorted file in col1 sequence or reorg aft er
the import/insert/load.
HTH, Pierre.

--
Pierre Saint-Jacques
SES Consultants Inc.
514-737-4515
"Brian Tkatch" <Ma***********@ ThePentagon.com > a écrit dans le message de
news: 11************* ********@i40g20 00...legro ups.com...
OK, i see.

Does CLUSTERing help BETWEENs?

That is, if the PK is a part number, part numbers are somewhat
sequential, and usually parts are grabbed with a BETWEEN, would
CLUSTERing put the part in order, helping a range scan quickly grab the
range from disk too?

B.


Feb 28 '06 #9
Hi,

Fraser McArthur, a consultant at the IBM Toronto Lab had wrote in an
article and it contained
· When queries are completing in a reasonable time, avoid adding
indexes as they can slow down update operations and consume extra
space. It is sometimes possible to have one larger index that will
cover several queries.
· Avoid using more than five columns in an index due to management
overhead.
· For multi-column indexes, place the column which is referenced most
in queries first in the definition.
· Avoid adding an index which is similar to a preexisting index. It
creates more work for the optimizer and will slow down update
operations. Instead, alter the preexisting index to contain additional
columns. For example, there is an existing index i1 on (c1,c2) of a
table. You notice that your query using "where c2=?", so you create an
additional index i2 on (c2). This similar index adds nothing, as it is
redundant to i1 and is now additional overhead.

The data model which we make use of has tables which have the similar
pattern.

My question is if we already have a Unique index(unique+in dex) and a
primary key
Do we really need a separate index on prod_typ_cde (because it is
redundant and is it an additional overhead) ??

CREATE TABLE "DB2TAR"."T KTG" (
"PROD_TYP_C DE" CHAR(6) NOT NULL ,
"REC_LOCTR_ NUM" CHAR(7) NOT NULL ,
"DEP_ALLW_I ND" CHAR(1) NOT NULL WITH DEFAULT 'N' ,
"BKNG_RNG_MIN_D AYS" SMALLINT NOT NULL ,
"BKNG_RNG_MAX_D AYS" SMALLINT NOT NULL ,
"AFT_BKNG_D AYS" SMALLINT ,
"BEF_DPTR_D AYS" SMALLINT ,
"TKTG_GRC_D AYS" SMALLINT ,
"GARP_RVW_ADVN_ DAYS" SMALLINT ,
"LST_UPDT_U SID" CHAR(6) NOT NULL ,
"LST_UPDT_T MS" TIMESTAMP NOT NULL WITH DEFAULT CURRENT TIMESTAMP )

IN "SGRA005" ;
CREATE UNIQUE INDEX "DB2TAR"."ITKTG _OVRD_RULE01" ON "DB2GRA"."T KTG"
("PROD_TYP_C DE" ASC,
"REC_LOCTR_ NUM" ASC,
"DEP_ALLW_I ND" ASC,
"BKNG_RNG_MIN_D AYS" ASC,
"BKNG_RNG_MAX_D AYS" ASC)
CLUSTER ;

CREATE INDEX "DB2TAR "."ITKTG_OVRD_R ULE02" ON "DB2GRA"."T KTG"
("PROD_TYP_C DE" ASC);
ALTER TABLE "DB2TAR "."TKTG"
ADD CONSTRAINT "PK_TKTG_OVRD_R ULE" PRIMARY KEY
("PROD_TYP_CDE" ,
"REC_LOCTR_NUM" ,
"DEP_ALLW_I ND",
"BKNG_RNG_MIN_D AYS",
"BKNG_RNG_MAX_D AYS");

Mar 6 '06 #10

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

Similar topics

1
2522
by: knoak | last post by:
Hi there, I have a guestbook on my site, and it gets opened in a frame. Every message in the page has it's own anchor according to the message-number. Now i've made this thing that i can request the url in the frame by calling the frameset (index.php) like this --> index.php?interactive/guestbook/ I detect it with $_SERVER
7
682763
by: vnl | last post by:
I'm trying to run a SQL query but can't find any records when trying to select a certain date. Here's the sql: SELECT field 1, field2, date_and_time, FROM table1 WHERE date_and_time = '01-SEP-02' I'm getting no results. The date_and_time field is formatted like this: 2002-SEP-02 00:01:04
0
3073
by: B. Fongo | last post by:
I learned MySQL last year without putting it into action; that is why I face trouble in formulating my queries. Were it a test, then you would have passed it, because your queries did help me solve my problem. I'll turn to MySQL doc after getting through this pressing project. Thanks a lot Roger! Babale -----Urspr=FCngliche Nachricht-----
3
5225
by: Paul Mateer | last post by:
Hi, I have been running some queries against a table in a my database and have noted an odd (at least it seems odd to me) performance issue. The table has approximately 5 million rows and includes the following columns: DocID (INTEGER, PRIMARY KEY, CLUSTERED) IsRecord (INTEGER, NONCLUSTERED)
12
6204
by: WantedToBeDBA | last post by:
Hi all, db2 => create table emp(empno int not null primary key, \ db2 (cont.) => sex char(1) not null constraint s_check check \ db2 (cont.) => (sex in ('m','f')) \ db2 (cont.) => not enforced \ db2 (cont.) => enable query optimization) DB20000I The SQL command completed successfully. db2 => insert into emp values(1,'m')
6
2047
by: Jack Orenstein | last post by:
Suppose I have a table as follows: testdb=> \d person Table "public.person" Column | Type | Modifiers ------------+-------------------------+----------- id | integer | not null age | integer | other_info | character varying(1000) | Indexes: person_pkey primary key btree (id),
7
2215
by: stig | last post by:
hi. coming from postgresql, i am used to textual references to most of the things i do with the database. i feel a little lost with all the graphical. i have few questions regarding MS SQL 2000 1. what is the best (or easiest) way of getting a table definition in text? it could be either a CREATE TABLE sql-query or a just a definition, something like: TABLE thisTable id integer
17
2739
by: NeoAlchemy | last post by:
I am starting to find more web pages that are using a query parameters after the JavaScript file. Example can be found at www.opensourcefood.com. Within the source you'll see: <script src="/shared/scripts/common.js?revision=1.6" type="text/javascript">. I am trying to see if there is any big deal to this or a best practice that is starting to creep up in the JavaScript community. If this is used only as a way to distinguish what...
2
1255
by: gm000 | last post by:
hi thanks for your reply i was very helpful for me regarding this i have a problem which is this i m using this query it works SELECT AuthorId, UrlUserName, Urlid, UrlAddress, ChooseMedia, Title, Description, ChooseThumbnail, ChooseTopic, Datetime, (SELECT COUNT(CommentId) AS Expr1 FROM CommentTable WHERE (CommentUrlid =...
0
9639
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
9479
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
10311
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
10080
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
9942
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
8967
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...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5509
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3639
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.