By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,660 Members | 1,945 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,660 IT Pros & Developers. It's quick & easy.

Multi column index

P: n/a
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
Share this Question
Share on Google+
8 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.