473,406 Members | 2,451 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.

indexing fields in the where clause

To gain performance, do I need to index ALL the fields in the where
clause.

Say we have a query like:
select stuff from table where field1=.. and field2=...

If field1 selection substantially reduces the number of records
selected, do we really need to index field2 as well.

In other words, does the query first subselect from the first argumet,
and then select further from the subset, or does the query do two
independent subselects and then merge them together?

--
http://www.dbForumz.com/ This article was posted by author's request
Articles individually checked for conformance to usenet standards
Topic URL: http://www.dbForumz.com/mySQL-indexi...ict156808.html
Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=524834
Jul 20 '05 #1
2 2120
steve wrote:
To gain performance, do I need to index ALL the fields in the where
clause.

Say we have a query like:
select stuff from table where field1=.. and field2=...

If field1 selection substantially reduces the number of records
selected, do we really need to index field2 as well.

In other words, does the query first subselect from the first argumet,
and then select further from the subset, or does the query do two
independent subselects and then merge them together?


MySQL is clever enough to work out which columns have indexes on them and
will use those columns first when selecting the data, and the columns that
do not have indexes afterwards, even if you put the where clause in an
order where the first column listed does not have an index.

You are correct in assuming that if the indexed column does most of the work
then you don't really need to index the second column as well, otherwise
you might well end up with every column in the table indexed! You would
probably only need to have the second column indexed as well if a select
query for the first column returned several hundred or thousands of
records.

--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Jul 20 '05 #2
"Chris Hope" wrote:
steve wrote:
To gain performance, do I need to index ALL the fields in the where clause.

Say we have a query like:
select stuff from table where field1=.. and field2=...

If field1 selection substantially reduces the number of records
selected, do we really need to index field2 as well.

In other words, does the query first subselect from the first argumet,
and then select further from the subset, or does the query do two
independent subselects and then merge them together?


MySQL is clever enough to work out which columns have indexes on

them and
will use those columns first when selecting the data, and the columns that
do not have indexes afterwards, even if you put the where clause in an order where the first column listed does not have an index.

You are correct in assuming that if the indexed column does most of
the work
then you don’t really need to index the second column as well,
otherwise
you might well end up with every column in the table indexed! You
would
probably only need to have the second column indexed as well if a
select
query for the first column returned several hundred or thousands of
records.


Chris, thanks for the quick reply. That is exactly the info I was
looking for. Good to hear that the order of fields in the where
clause does not matter, and mysql can figure out how to optimize.

--
http://www.dbForumz.com/ This article was posted by author's request
Articles individually checked for conformance to usenet standards
Topic URL: http://www.dbForumz.com/mySQL-indexi...ict156808.html
Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=524872
Jul 20 '05 #3

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

Similar topics

1
by: Danny | last post by:
sorry for posting a question that has probably been posted hundreds of times, but i can't seem to find the answer i need...We're using a Sql Server 7.0 database and recently started getting timeout...
1
by: Thomas Bartkus | last post by:
If we have a date/time field and are doing frequent queries WHERE {date/time field} BETWEEN TimeA AND TimeB Does it make sense, query speed wise, to create an index on the date/time field? ...
2
by: Kent.Brooke | last post by:
Can someone set me straight I know indexing is a "try & see" art. However I am at a loss if it's better to use the INCLUDE switch on a unique index and tag on the columns be used to avoid a lookup...
9
by: John Sidney-Woollett | last post by:
Hi I'm building a web app where changes to customer orders are logged in the following table, and I worried about the time that it will take to locate records that need further...
6
by: Hennie7863 | last post by:
Hi everybody, After days reading stuff about indexing, extents, pages, 8KB, 64 KB, IGNORE_DUP_KEY, CREATE INDEX, bla bla, lalalala, lllllll, grrrrrrr and testing with different kinds of queries,...
6
by: Bethany Holliday | last post by:
Hi All, I'm hoping someone can help me. I think I'm missing something very basic. I'm trying to put a clustered index on a view that I have created. I keep getting the error: Server: Msg...
3
by: Chung Leong | last post by:
Here's the rest of the tutorial I started earlier: Aside from text within a document, Indexing Service let you search on meta information stored in the files. For example, MusicArtist and...
4
by: Amar | last post by:
Hi All, I need to select data from a database table containing huge amount of data. Now I am storing data using one primary key and I am just using simple select statement, and this process...
3
by: Icemokka | last post by:
Hi, I've got a table in MsAccess with 100+ fields. If I fill a tabletable with this table , change some values , get the update-command via commandbuilder , the update fails. This because the...
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
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
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,...
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.