Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old August 13th, 2008, 10:14 PM
Newbie
 
Join Date: Aug 2008
Posts: 1
Default Group by not using index

Hello,

I have 3 tables

tableA has info regarding name and clientid
tableB has info regarding the orderids of the clients
tableC has info regarding the tradeids of the clients
And all the tables have session column

primary key for tableA is (session,clientid, name)
tableB has primary key(session, orderid) and index(session, clientid)
tableC has index(session, orderid)

and here is my query
Expand|Select|Wrap|Line Numbers
  1. select a.name, count(distinct a.clientid), sum(c.price)
  2. from tableA a
  3. STRAIGHT_JOIN tableB b on a.session=b.session and a.clientid=b.clientid
  4. STRAIGHT_JOIN tableC c use index (primary) on b.session=c.session and b.orderid=c.orderid
  5. group by a.name
  6. ;
  7.  
the query is taking 45 seconds to execute
and extra field of the explain plan show using filesort for tableA

My question is how I have to modify the indexes to make the tableA to use the index so that query executes faster.

Thanks in advance

Last edited by Atli; August 16th, 2008 at 06:09 AM. Reason: Added [code] tags
Reply
  #2  
Old August 16th, 2008, 06:28 AM
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Age: 22
Posts: 2,777
Default

Hi.

What is the purpose of the "session" columns?

My guess would be that your Primary Keys are causing the delays.
Why do created a PK based on all three columns in table A?

As a general rule, you should never use multiple columns as a PK unless it is absolutely necessary.

A Primary Key should ideally be a row counter of sorts. 99% of the time that will do fine, and that will usually produce the fastest search results, especially when joining multiple tables.

Simply changing that in your tables, as well as adding Indexes for your "name", "session" and "price" columns should reduce the query times.
Reply
  #3  
Old August 19th, 2008, 08:35 AM
Needs Regular Fix
 
Join Date: Mar 2008
Posts: 305
Default

Just to add a little to the previous reply:

what is the least common unique denominator in your table A? You say your primary key is composed of three columns: session, clientID and name, but why all three? Is the session column needed? And if so, is the name column needed?

In your query, you have the "on" condition for the table joins using the two columns "session" and "clientID", but not the "name" column. Because of this, mysql may not be able to use your primary key for your query. Therefore you may be able to increase speed by taking the "name" column out of your primary key in Table A. You may want to try that, and then see what the EXPLAIN results come up with.

Here I repeat your query:
Expand|Select|Wrap|Line Numbers
  1. select a.name, count(distinct a.clientid), sum(c.price)
  2. from tableA a
  3. STRAIGHT_JOIN tableB b on a.session=b.session and a.clientid=b.clientid
  4. STRAIGHT_JOIN tableC c use index (primary) on b.session=c.session and b.orderid=c.orderid
  5. group by a.name
  6. ;
  7.  
I see another funny thing in the above: You have a "group by name", but could this also not be substituted by "group by clientid"? You have a count(distinct a.clientid), and looking at this I am becoming quite suspicious and curious about what you are really trying to do here. Your query and table structure may be a little bit "funny" for mysql to figure out. Try to post your "show create table" outputs here and also give a few words of explanation as to what session, clientid and name are supposed to mean here.
Reply
Reply

Bookmarks

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles