473,406 Members | 2,769 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,406 software developers and data experts.

Indexes

Is there some manual on how to make indexes?

for example:
table is
create mytable
(
a varchar,
b varchar,
c int,
d int
);

the data fields are (a,b,c,d) and my select statements can be
1. select a,b,sum(c)-sum(d) from mytable;
2. select a, sum(c) from mytable;
3. select b, sum(d) from mytable;

Which of the following structure makes a better index:
one index:
create index index1 on mytable (a,b);

or

two indexes:
create index index1 on mytable (a);
create index index2 on mytable (b);

Jan 10 '06 #1
5 1492
On Mon, 09 Jan 2006 23:01:01 -0800, Shiraz wrote:
the data fields are (a,b,c,d) and my select statements can be 1. select
a,b,sum(c)-sum(d) from mytable; 2. select a, sum(c) from mytable;
3. select b, sum(d) from mytable;

Which of the following structure makes a better index: one index:
create index index1 on mytable (a,b);

or

two indexes:
create index index1 on mytable (a);
create index index2 on mytable (b);


Have you tried create all three indexes (they can co-exist), then run
"EXPLAIN SELECT a,b,sum(c)-sum(d)" and the other queries on your tables?

This is the way to find your answer, depending on data distribution
sometimes MySQL will use different queries for the same table and query
structure.

Also, as I understood it the indexes were only used when you had a WHERE
clause (as otherwise you're operating on the whole dataset so there's no
point working out which order to iterate through the whole lot on).

Anyway, I may be wrong on that last point - but you should create the
indexes and test which indexes are used (MySQL will *generally* use the
fastest/best index).

Cheers,
Andy

--
Andy Jeffries | gPHPEdit Lead Developer
http://www.gphpedit.org | PHP editor for Gnome 2
http://www.andyjeffries.co.uk | Personal site and photos

Jan 10 '06 #2
>Is there some manual on how to make indexes?

I often make them with:

alter table mytable add index (foo);
or
alter table mytable add unique (foo);
or
alter table mytable add primary key (foo);

for example:
table is
create mytable
(
a varchar,
b varchar, You need a length on varchar fields. c int,
d int
);

the data fields are (a,b,c,d) and my select statements can be
1. select a,b,sum(c)-sum(d) from mytable;
2. select a, sum(c) from mytable;
3. select b, sum(d) from mytable;
All of these select the entire table. And they don't use ORDER BY.
Chances are use of an index would not be very productive here.

Usually, the index you want relates to conditions in the WHERE
clause or what you ORDER BY. Also, if you want something to be
unique, you want a UNIQUE index or PRIMARY KEY on it.
Which of the following structure makes a better index:
one index:
create index index1 on mytable (a,b);

or

two indexes:
create index index1 on mytable (a);
create index index2 on mytable (b);


You can't use two separate indexes for the same query. (Depending
on the types of queries you make, it could be a good idea to have
them for different queries).

For example, in a phone book, it's generally ordered by (lastname,
firstname). You can do a lookup if it were ordered by (lastname)
or by (firstname), it's just slower. But a second phone book ordered
by (firstname) is no help if you have one ordered by (lastname)
only.

Gordon L. Burditt
Jan 10 '06 #3
I understand what you are saying here. Different queries can use
different indexes, so as defined below i have three queries and there
can be an index defined for all of them specifically that works best
for that query.
alter table mytable add index (a);
alter table mytable add index (b);
alter table mytable add index (a,b);
You can't use two separate indexes for the same query. (Depending
on the types of queries you make, it could be a good idea to have
them for different queries).
Let me add to the problem: for the table defined above (mytable), with
fields a,b,c,d in need 3 basic queries:

select a, sum(c)
where a > '2005'
group by a;

and

select b, sum(d)
where a > '2005'
group by a;

and

select a, b, sum(c) - sum(d)
where a > '2005'
and b > '-1'
group by a,b;
Also, as I understood it the indexes were only used when you had a WHERE
clause (as otherwise you're operating on the whole dataset so there's no
point working out which order to iterate through the whole lot on).
and given that the data set is going to be thousands of rows, does it
make sense to make 2 indexes
alter table mytable add index (a);
alter table mytable add index (b);

or just one;
alter table mytable add index (a,b);

OR can i have all three in there? considering space in not an issue?
For example, in a phone book, it's generally ordered by (lastname,
firstname). You can do a lookup if it were ordered by (lastname)
or by (firstname), it's just slower. But a second phone book ordered
by (firstname) is no help if you have one ordered by (lastname)
only.


Jan 10 '06 #4
>I understand what you are saying here. Different queries can use
different indexes, so as defined below i have three queries and there
can be an index defined for all of them specifically that works best
for that query.
alter table mytable add index (a);
alter table mytable add index (b);
alter table mytable add index (a,b);
Having many indexes slows down record insertion and deletion (and updating
the key fields). It can also dramatically speed up selects.
You might not want to add an index appropriate for a query used once
a year at tax time. You might well want to add an index appropriate
for a query used a dozen times a second.
You can't use two separate indexes for the same query. (Depending
on the types of queries you make, it could be a good idea to have
them for different queries).


Let me add to the problem: for the table defined above (mytable), with
fields a,b,c,d in need 3 basic queries:

select a, sum(c)
where a > '2005'
group by a;


This query could make good use of an index on (a) (for the WHERE
and GROUP BY).

and

select b, sum(d)
where a > '2005'
group by a;
This query could make good use of an index on (a) (for the WHERE
and GROUP BY).

and

select a, b, sum(c) - sum(d)
where a > '2005'
and b > '-1'
group by a,b;
This query could make good use of an index on (a) (for the WHERE)
or on (a,b) (for the WHERE and the GROUP BY) or on (b) (for the
WHERE) or on (b,a) (for the WHERE). Which one it will pick if all
are available, I'm not sure. It may well depend on things like whether
a or b have more unique values.
Also, as I understood it the indexes were only used when you had a WHERE
clause (as otherwise you're operating on the whole dataset so there's no
point working out which order to iterate through the whole lot on).
ORDER BY and GROUP BY may also take advantages of indexes if the index
matches what you're ordering by or grouping by.
and given that the data set is going to be thousands of rows, does it
make sense to make 2 indexes
alter table mytable add index (a);
alter table mytable add index (b);

or just one;
alter table mytable add index (a,b);

OR can i have all three in there? considering space in not an issue?


If space is not an issue, speed still might be. (How much are you
updating the data? Or is this 99.9% reads?) Yes, you can have all
three in there. And I believe an index for (a,b) can substitute
for one on (a) in some situations. However, an index for (b,a)
cannot substitute for an index on (a).
For example, in a phone book, it's generally ordered by (lastname,
firstname). You can do a lookup if it were ordered by (lastname)
or by (firstname), it's just slower. But a second phone book ordered
by (firstname) is no help if you have one ordered by (lastname)
only.


Gordon L. Burditt
Jan 10 '06 #5
I have about 2 million inserts, 2 million updates and about 10000
selects daily but I understand what you are saying.
Having many indexes slows down record insertion and deletion (and updating
the key fields). It can also dramatically speed up selects.
You might not want to add an index appropriate for a query used once
a year at tax time. You might well want to add an index appropriate
for a query used a dozen times a second.
This answers some of my concerns: its possible to have some benefit
from an index for (a,b) in lieu of an index (a)And I believe an index for (a,b) can substitute
for one on (a) in some situations. However, an index for (b,a)
cannot substitute for an index on (a).


Will try some tests on a data set and post the results. now that i
somewhat undestand what to look for and what are the variables.
Thanks for the help

SK

Jan 10 '06 #6

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

Similar topics

0
by: Paul | last post by:
I'm confused about something I did this week. I have an 8.1.7.4 database with tables partitioned by dates. I recently dropped my maxvalue tablespace, added a few more partitions for up-coming dates,...
2
by: Chris | last post by:
Any help would be appreciated. I am running a script that does the following in succession. 1-Drop existing database and create new database 2-Defines tables, stored procedures and functions...
1
by: Steve_CA | last post by:
Hi, The more I read, the more confused I'm getting ! (no wonder they say ignorance is bliss) I just got back from the bookstore and was flipping through some SQL Server Administration...
9
by: Igor | last post by:
Is there a way to force optimizer to use indexes without hints? (some server setting or index type...) I'll give an example to clarify : I have a table with fields Customer_Code char(10) not...
9
by: pheonix1t | last post by:
hello, I've been assigned to do performance tuning on an SQL2000 database (around 10GB in size, several instances). So far, I see a single RAID5 array, 4CPU (xeon 700MHZ), 4GB RAM. I see the...
4
by: mairhtin o'feannag | last post by:
Hello, I want to ask the question "how large is each of the indexes in a particular tablespace??" since I want to know which user-created indexes are taking up huge amounts of space. I used the...
14
by: Jeff | last post by:
This is the first time that I remember ever having too many indexes on a table, but it has happened. I have en employees table and store in many places, on tables, the id of the employee that...
17
by: boa | last post by:
I'm currently planning disk layouts and use for a new version of our database. The current version has all data and indexes in the default filegroup, placed on one big raid-5 array(6 drives) along...
10
by: lesperancer | last post by:
you start with a small application in access97, then you have more modules and more... and you reach the point where tables like 'item' and 'employee' reach the limit and you know there's more...
0
MMcCarthy
by: MMcCarthy | last post by:
The more data you include in your tables the more you will need to have indexes to search and sort that data. However, there is a balance between having enough indexes and too many. Too many...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
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,...
0
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...
0
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,...
0
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...
0
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...

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.