Bluehost.com Web Hosting $6.95

MySQL group indexes

This is a discussion on MySQL group indexes within the MySQL Database forums, part of the Database Forums category; I have a query like... SELECT ... ORDER BY A DESC, B DESC I have created both index(A), index(A,...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 12-08-2006
howa
 
Posts: n/a
Default MySQL group indexes

I have a query like...

SELECT ... ORDER BY A DESC, B DESC


I have created both index(A), index(A,B)

but mysql always explained that index(A) is used, although possible
keys include (A,B)

any reason to find out why?

p.s.the numbers of rows is around 3M, (200MB in size)

Reply With Quote
  #2 (permalink)  
Old 12-08-2006
lain
 
Posts: n/a
Default Re: MySQL group indexes


howa napisal(a):
> I have a query like...
>
> SELECT ... ORDER BY A DESC, B DESC
>
>
> I have created both index(A), index(A,B)
>
> but mysql always explained that index(A) is used, although possible
> keys include (A,B)
>
> any reason to find out why?
>
> p.s.the numbers of rows is around 3M, (200MB in size)


this is because MySql uses only one (best in his opinion) index in
GROUP BY - so if you want a double index on 2 columns (A, B) you must
create it and group columns in order given in your index

Reply With Quote
  #3 (permalink)  
Old 12-08-2006
howa
 
Posts: n/a
Default Re: MySQL group indexes


lain ¼g¹D¡G

> howa napisal(a):
> > I have a query like...
> >
> > SELECT ... ORDER BY A DESC, B DESC
> >
> >
> > I have created both index(A), index(A,B)
> >
> > but mysql always explained that index(A) is used, although possible
> > keys include (A,B)
> >
> > any reason to find out why?
> >
> > p.s.the numbers of rows is around 3M, (200MB in size)

>
> this is because MySql uses only one (best in his opinion) index in
> GROUP BY - so if you want a double index on 2 columns (A, B) you must
> create it and group columns in order given in your index


okay, i know the reason now, thx anyway.

Reply With Quote
  #4 (permalink)  
Old 12-09-2006
Michael Austin
 
Posts: n/a
Default Re: MySQL group indexes

lain wrote:

> howa napisal(a):
>
>>I have a query like...
>>
>>SELECT ... ORDER BY A DESC, B DESC
>>
>>
>>I have created both index(A), index(A,B)
>>
>>but mysql always explained that index(A) is used, although possible
>>keys include (A,B)
>>
>>any reason to find out why?
>>
>>p.s.the numbers of rows is around 3M, (200MB in size)

>
>
> this is because MySql uses only one (best in his opinion) index in
> GROUP BY - so if you want a double index on 2 columns (A, B) you must
> create it and group columns in order given in your index
>


If you have a where clause, the index is chosen by the columns in your "where"
clause and not the group by, the group by is done in the sort phase after the
data has been retrieved.

--
Michael Austin.
Database Consultant
)
Reply With Quote
  #5 (permalink)  
Old 12-09-2006
howa
 
Posts: n/a
Default Re: MySQL group indexes


Michael Austin ¼g¹D¡G

> lain wrote:
>
> > howa napisal(a):
> >
> >>I have a query like...
> >>
> >>SELECT ... ORDER BY A DESC, B DESC
> >>
> >>
> >>I have created both index(A), index(A,B)
> >>
> >>but mysql always explained that index(A) is used, although possible
> >>keys include (A,B)
> >>
> >>any reason to find out why?
> >>
> >>p.s.the numbers of rows is around 3M, (200MB in size)

> >
> >
> > this is because MySql uses only one (best in his opinion) index in
> > GROUP BY - so if you want a double index on 2 columns (A, B) you must
> > create it and group columns in order given in your index
> >

>
> If you have a where clause, the index is chosen by the columns in your "where"
> clause and not the group by, the group by is done in the sort phase afterthe
> data has been retrieved.
>
> --
> Michael Austin.
> Database Consultant
> )


yes, i know this now, thanks.

in fact, are there any best practice to improve theese kind of query?
since we are only selecting a small subset of the rows(i.e. paging)?

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 03:02 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0