MySQL Forums
Forum List  »  Performance

Adding index kills query
Posted by: Robert Stafford
Date: April 23, 2014 06:10PM

I am trying to sum a variable, fPurchaseAmt, which is a float, grouped by iCompanyID, which is a BigInt. Adding an index on iCompanyID seems to have killed the performance. My table, Trans_smp, has 15 million rows, and about 10 other columns. The engine is InnoDB. In an effort to improve performance, I added an index on iCompanyID and another variable, iBrandID:

create index idxCompBrand on Trans_smp (iCompanyID, iBrandID);

No problems. I ran the following test code before and after creating the index:

select iCompanyID, count(fPurchaseAmt) FROM Trans_smp GROUP BY iCompanyID;

Before creating the index, it ran in about 34 seconds, after creating the index, it ran in about 14 seconds.

I then ran the code I want:

select iCompanyID, sum(fPurchaseAmt) FROM Trans_smp GROUP BY iCompanyID;

Before creating the index, it ran in about 33 seconds, roughly the same as the count. After creating the index, it either fails or it takes a long time. The most recent time I tried it, I waited 4 hours and it didn't finish. In an effort to get it to work, I tried creating a second index, limited just to iCompanyID:

create index idxComp on Trans_smp (iCompanyID);

After creating the second index, I tried again and it didn't finish in less than 4 hours, so I pulled the plug.

After creating the second index, I tried again with the following code:

select iCompanyID, sum(fPurchaseAmt)
FROM Trans_smp
ignore index idxCompBrand
ignore index idxComp
GROUP BY iCompanyID
;

Once again, ran without problems in about 33 seconds.

At someone's suggestion, I also tried creating an index on iCompanyID and fPurchaseAmt:

create index idxCompPurchAmt on Trans_smp (iCompanyID, fPurchaseAmt);

This also did not help - after several hours, I killed the sum query before it finished.

Anyone have any ideas about what my problem is, and how to avoid it? Obviously I can just use the "ignore index" approach, but I hope to start using this and similar steps on the full set, with has 350 million rows, so I would like to be able to use indexes.

Here's the full results for "show create table":

create table Trans_smp (
aTrans BigInt bigint(20) unsigned not null default '0'
, iCustID BigInt(20) unsigned default null
, iChainID SmallInt(5) unsigned default null
, iDeptID SmallInt(5) unsigned default null
, iCategoryID BigInt(20) unsigned default null
, iCompanyID BigInt(20) default null
, iBrandID BigInt(20) unsigned default null
, dTransDt date default null
, fProductSz float default null
, cProductMeasure char(8) default null
, iPurchaseQty MediumInt(9) default null
, fPurchaseAmt float default null
, key idxCompBrand (iCompanyID, iBrandID)
, key idxComp (iCompanyID)
, key idxCompPurchAmt (iCompanyID, fPurchaseAmt)
)
engine=InnoDB default charset=latin1;

Options: ReplyQuote


Subject
Views
Written By
Posted
Adding index kills query
2287
April 23, 2014 06:10PM
1020
April 23, 2014 06:14PM
1060
April 24, 2014 02:44AM
1205
April 24, 2014 09:07AM
1011
April 25, 2014 07:39AM
1142
April 25, 2014 11:54AM
1066
April 25, 2014 02:25PM
1110
April 28, 2014 06:06AM
924
April 24, 2014 09:37AM
983
April 24, 2014 06:31PM
1269
April 24, 2014 07:10PM
1088
April 24, 2014 07:19PM
1226
April 25, 2014 10:55AM
945
April 25, 2014 11:02PM
1901
April 27, 2014 07:53PM
1026
April 27, 2014 08:50PM
1011
April 27, 2014 09:00PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.