473,394 Members | 1,703 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,394 software developers and data experts.

SQL index problem

INDEX index_A (coll_a, coll_b),
INDEX index_B (col_b, coll_a),

Are these two indexes redundant?
The two indexes above actually contain the same contents in different order.
I can change a query from
"SELECT * FROM table WHERE coll_b=b AND coll_a=a"
to
"SELECT * FROM table WHERE coll_a=a AND coll_b=b",
so that I can make use of index_A, and get rid of index_B.
Jul 20 '05 #1
1 1157
"jy2003" <jy****@sbcglobal.net> wrote in message news:YuTuc.62858
INDEX index_A (coll_a, coll_b),
INDEX index_B (col_b, coll_a),

Are these two indexes redundant?
No. First is useful if you want to sort by coll_a, and second is useful if
you want to sort by coll_b. But in your example below you fix coll_a and
coll_b to fixed values, so in that case the index is redundant.
The two indexes above actually contain the same contents in different order. I can change a query from
"SELECT * FROM table WHERE coll_b=b AND coll_a=a"
to
"SELECT * FROM table WHERE coll_a=a AND coll_b=b",
so that I can make use of index_A, and get rid of index_B.


You can drop one of the indexes and not bother rewriting your query. The
database engine is smart enough to realize to rearrange the where clause in
order to use the right index.

Now if you have lots of tables and a complicated where clause then the
database engine could get confused and may pick the wrong index. So after
careful analysis that tells you which is the correct index to use, use an
order by clause. Say we had only the index INDEX index_A (coll_a, coll_b).
Then the select statement would be

-- use only if explain shows database using the wrong index
-- and you know what the right index is
SELECT * FROM table WHERE coll_a=a AND coll_b=b
ORDER BY coll_a, coll_b;

MySql also has extensions use index, ignore index, and force index. See the
documentation for details, and which extensions work in which versions of
MySql (4.1 supports all of them). See my reply to the thread "Indexes with
OR queries" for an example.
Jul 20 '05 #2

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

Similar topics

0
by: Mike Coppinger | last post by:
I have a problem that has raised a couple of questions. SITUATION: I have a table called pr_persona that has a composite key comprising pr_persona_db CHAR(2) pr_persona_id INT(11) auto...
1
by: J. C. Clay | last post by:
We are having some troubles with corrupt indexes on our SQL 2000 dbase. It is only affecting 2 tables out of about 150 we have. These 2 tables are heavily used. We have run dbcc repairs;...
0
by: Guy Deprez | last post by:
Hi, i'm having a problem to create indexes. STEP 1 ----------- Connection is OK (you can find the string at the end of the message) Table ("Couleurs") creation is OK STEP 2. Index Creation
3
by: charley | last post by:
Hello, The appearance of the page should be for the main image, lpmap.jpg, to be on the bottom, (z-index: 1), with pictures, (image0.jpg - image9.jpg), and accompaning notes to appear on top,...
14
by: Sean C. | last post by:
Helpful folks, Most of my previous experience with DB2 was on s390 mainframe systems and the optimizer on this platform always seemed very predictable and consistent. Since moving to a WinNT/UDB...
24
by: Henrik Steffen | last post by:
hello all, on my master-db-server i'm running postgres 7.4.1, and I have got two slave-servers running postgres 7.4.2 running the following query on the master-server (7.4.1) delivers: ...
14
by: Rich | last post by:
Yes, I need to store some values in an array type collection object that can hold 3 or more parameters per index. I have looked at the collection object, hashtable object and would prefer not to...
85
by: Russ | last post by:
Every Python programmer gets this message occasionally: IndexError: list index out of range The message tells you where the error occurred, but it doesn't tell you what the range and the...
18
by: Dave | last post by:
Guys I am really stuck on this one. Any help or suggestions would be appreciated. We have a large table which seemed to just hit some kind of threshold. They query is somewhat responsive when...
5
by: lightgram | last post by:
Hi I have a problem, which after browsing through Google, seems to be fairly common. However having tried most suggestions I am still getting the problem. I have a menu bar across the top...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.