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

Multi column index

Help!

Probably it is too easy, but I am not so experienced in MySQL up to now.

I have two tables table1&table2 with the following columns: col1 double,
col2 int(11), col3 date

I want to query:

INSERT INTO table2 SELECT col1,col2,col3 FROM table1
WHERE col2= ( SELECT MAX(col2)
FROM table1
WHERE table1.col1=table2.col1 AND
table1.col3=table2.col3
);

It is working fine, but not very fast. Do you know the best solution,
probably a multi-column index (INDEX indexname(col1,col3))? Or do I need
an index also for col2?

Thanks,

Matthias

Jul 19 '05 #1
8 4923
On Sun, 30 Nov 2003 02:14:47 +0100, Matthias Braun <ma*******@web.de>
wrote:
It is working fine, but not very fast. Do you know the best solution,
probably a multi-column index (INDEX indexname(col1,col3))? Or do I need
an index also for col2?


Mysql can only use one index per table. So, a multi-column index
would be the right way to go.

If you built your index with Col1,Col3, then any "where" with Col1 and
Col3 can be optimized with that index. And any "where" with just
col1 can be optimized, but a "where" with just col3 would NOT be able
to use the index.

Chuck Gadd
http://www.csd.net/~cgadd/aqua
Jul 19 '05 #2
On Sun, 30 Nov 2003 02:14:47 +0100, Matthias Braun <ma*******@web.de>
wrote:
It is working fine, but not very fast. Do you know the best solution,
probably a multi-column index (INDEX indexname(col1,col3))? Or do I need
an index also for col2?


Mysql can only use one index per table. So, a multi-column index
would be the right way to go.

If you built your index with Col1,Col3, then any "where" with Col1 and
Col3 can be optimized with that index. And any "where" with just
col1 can be optimized, but a "where" with just col3 would NOT be able
to use the index.

Chuck Gadd
http://www.csd.net/~cgadd/aqua
Jul 19 '05 #3
On Sat, 29 Nov 2003 18:21:27 -0700, I wrote:
Mysql can only use one index per table.


Mysql can only use one index per table PER QUERY.
Chuck Gadd
http://www.csd.net/~cgadd/aqua
Jul 19 '05 #4
On Sat, 29 Nov 2003 18:21:27 -0700, I wrote:
Mysql can only use one index per table.


Mysql can only use one index per table PER QUERY.
Chuck Gadd
http://www.csd.net/~cgadd/aqua
Jul 19 '05 #5
Hi Chuck,

I am using MySQL 4.1.0-alpha. Only one index is used for that query,
but the index is a multi-column index, see also

http://www.mysql.com/doc/en/Multiple...n_indexes.html

Matthias
Chuck Gadd schrieb:
On Sat, 29 Nov 2003 18:21:27 -0700, I wrote:
Mysql can only use one index per table.


Mysql can only use one index per table PER QUERY.
Chuck Gadd
http://www.csd.net/~cgadd/aqua


Jul 19 '05 #6
Hi Chuck,

I am using MySQL 4.1.0-alpha. Only one index is used for that query,
but the index is a multi-column index, see also

http://www.mysql.com/doc/en/Multiple...n_indexes.html

Matthias
Chuck Gadd schrieb:
On Sat, 29 Nov 2003 18:21:27 -0700, I wrote:
Mysql can only use one index per table.


Mysql can only use one index per table PER QUERY.
Chuck Gadd
http://www.csd.net/~cgadd/aqua


Jul 19 '05 #7
On Sun, 30 Nov 2003 02:51:07 +0100, Matthias Braun <ma*******@web.de>
wrote:

I am using MySQL 4.1.0-alpha. Only one index is used for that query,
but the index is a multi-column index, see also


I know about multi-colum indexes. I was basically just saying that
you need to use a multi-column index. You could not just create three
seperate single-field indexes and have mysql use them all on the same
query.

But I did mis-read your original posted query.

Lets see, it does:

select ... from table1 where col2=x

So you would need an index on table1.col2 by itself.

And it does

select max(col2) from table1 where table1.col1=table2.col1
and table1.col3=table2.col3

So you'd need an index on table1.col1+table1.col3 and
table2.col1+table2.col3

I am GUESSING that mysql would be able to use two different
indexes, since they really are seperate queries. I haven't played
with sub-selects much in mysql, so I'm not sure what the optimizer
will do. And in fact, the optimizer might not do a great job yet.
Chuck Gadd
http://www.csd.net/~cgadd/aqua
Jul 19 '05 #8
On Sun, 30 Nov 2003 02:51:07 +0100, Matthias Braun <ma*******@web.de>
wrote:

I am using MySQL 4.1.0-alpha. Only one index is used for that query,
but the index is a multi-column index, see also


I know about multi-colum indexes. I was basically just saying that
you need to use a multi-column index. You could not just create three
seperate single-field indexes and have mysql use them all on the same
query.

But I did mis-read your original posted query.

Lets see, it does:

select ... from table1 where col2=x

So you would need an index on table1.col2 by itself.

And it does

select max(col2) from table1 where table1.col1=table2.col1
and table1.col3=table2.col3

So you'd need an index on table1.col1+table1.col3 and
table2.col1+table2.col3

I am GUESSING that mysql would be able to use two different
indexes, since they really are seperate queries. I haven't played
with sub-selects much in mysql, so I'm not sure what the optimizer
will do. And in fact, the optimizer might not do a great job yet.
Chuck Gadd
http://www.csd.net/~cgadd/aqua
Jul 19 '05 #9

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

Similar topics

1
by: mudge | last post by:
Hello, I have a mysql table. Two columns in the table are used to pull out stories from the table. One of the columns stands for categories of the stories, and the other column is the primary...
1
by: Kamil | last post by:
I want to sum elements of a multi-dimensional vector, across each vector, or across each dimension... this is my old code: #define v_ldouble vector<ldouble> #define vv_ldouble...
2
by: Ross Klatte | last post by:
If I have a key comprised of Column-1 plus Column-2, and if I Select X where Column-2 = "yyy" then I suppose that the retrieval takes place on a pure sequential basis just as if I were selecting...
6
by: CindyH | last post by:
Hi Does anyone know how to create a multi column combo box from the same table? Thanks, Cindy
0
by: awebguynow | last post by:
If I use a multi-column index, is it ineffecient ? how much of a performance hit ? I'm creating an order table, that will have a city code as one of the columns. index will be ( orderID,...
7
by: Dave Hammond | last post by:
Hi All, I'd like to have indexed search capability on column A, column B, or columns (A,B) for a given table. According to the MySQL manual, a multi-column index of (A,B) will provide "leftmost...
8
by: kd | last post by:
Newbie question here. It's been a while since I've done C programming, and I hit a wall last night. Let's say I have a three dimensional array, like so: int p = {{{0,0,0}, {1,1,1},...
4
by: Steve | last post by:
Hi all, I don't want to use the datagrid if I don't have to. Is there a way to setup a ListBox to have more than one checkbox column? I need something like this | Include || Set as...
1
by: Sonny | last post by:
Hi, Would like to know the performance differenece between Multi-column Index vs Single Column Indexes. Let's say I have a table with col1, col2, col3 along with a primary key column and...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.