Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

Here is my query:

SELECT BAL,NAME,
   (SELECT SUM(DR) FROM transaction WHERE NAME=TNAME AND DATE < '2000-05-01') AS DR ,
   (SELECT SUM(CR) FROM transaction WHERE NAME=TNAME AND DATE < '2000-05-01') AS CR
FROM main
GROUP BY MARK,NAME WITH ROLLUP

This query takes at least 20 seconds and sometimes even up to 45 seconds when ‘DATE’ increases(example:2012-05-01).

There are about 500,000 records in the transaction table. After hours of testing I think the problem might be with the 2 sub queries:

(SELECT SUM(DR) FROM transaction WHERE NAME=TNAME AND DATE < '2000-05-01') AS DR ,
(SELECT SUM(CR) FROM transaction WHERE NAME=TNAME AND DATE < '2000-05-01') AS CR

I tried adding an index to DATE and TNAME, it speeded things up a little bit, but still not enough.Would be great if I could atleast shave off atleast 10 more seconds Is there anything I could do to make it faster?

Table details:

 CREATE TABLE ` transaction` (
  `DATE` DATE NULL DEFAULT NULL,
  `DOCNO` VARCHAR(10) NULL DEFAULT NULL,
  `TCODE` VARCHAR(6) NULL DEFAULT NULL,
  `TNAME` VARCHAR(50) NULL DEFAULT NULL,
  `CR` DECIMAL(12,2) NULL DEFAULT NULL,
  `DR` DECIMAL(12,2) NULL DEFAULT NULL,
   INDEX `TNAME` (`TNAME`),
   INDEX `DATE` (`DATE`),
   INDEX `CR` (`CR`),
   INDEX `DR` (`DR`)
  )
  COLLATE='latin1_swedish_ci'
  ENGINE=InnoDB;


  CREATE TABLE `main` (
    `id` INT(11) NOT NULL AUTO_INCREMENT,
    `CODE` VARCHAR(50) NOT NULL,
    `NAME` VARCHAR(70) NULL DEFAULT NULL,
    `BAL` DECIMAL(12,2) NULL DEFAULT '0.00',
    `MARK` VARCHAR(35) NULL DEFAULT NULL,
    PRIMARY KEY (`CODE`),
    UNIQUE INDEX `NAME` (`NAME`),
    PRIMARY KEY (`id`),
    INDEX `MARK` (`MARK`)
   )
   COLLATE='utf8_general_ci'
   ENGINE=InnoDB
   ROW_FORMAT=COMPACT
   AUTO_INCREMENT=545451;
share|improve this question

1 Answer 1

up vote 0 down vote accepted

Of course it is the subqueries' fault. For each row of table main, this query goes through the whole transactions table twice (once for each subquery). That's a lot of load.

How about a simple join? This should have the same results and take much less than 50% of your processing time even with no indexes.

SELECT main.BAL,main.NAME,SUM(transaction.DR),SUM(transaction.CR)
FROM main
LEFT JOIN transaction on transaction.TNAME = main.NAME and transaction.DATE < '2000-05-01' 
GROUP BY main.MARK,main.NAME WITH ROLLUP
share|improve this answer
    
I'm testing it,but your query seems to return much lesser rows(not the same result).Is there any mistake that you've probably made? –  phpcoderx Jan 20 at 13:10
    
Yeah, probably not all rows of main have corresponding transactions. I just changed INNER JOIN to LEFT JOIN. How does it look now? –  foibs Jan 20 at 13:16
    
It is 10-15 seconds more faster now :) .In the future should I consider upgrading to those commercial ones like Oracle,MS SQL or should I change my table structure? because data keeps pouring in. –  phpcoderx Jan 20 at 13:32
    
I don't think you need to change DBMS. MySQL is up for the job. You could however change your queries and maybe table structure. E.g. make smaller queries with date ranges, or use a cron to update a stats table once per day or something, then select just from the stats table. Generally, when you go through many rows to get a result, you will get slow times, no matter if you're using MySQL or Oracle. –  foibs Jan 20 at 13:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.