简体   繁体   中英

Mysql query performance issue with left join

I have 3 tables in database. First table has 150k records, 2nd table has 175k records and 3rd table has 2.1 million records. I need the output using above 3 tables. So I joined above 3 table using inner join and I got the output with 1.5k records, but it takes 8 hours of time to execute.

I used primary keys in joining tables. How to increase the performance?

I have tried to add indexes and below is the script & code

Below table has 150k records

 CREATE TABLE `us_input_opp_oppheader` (
    `accountId` varchar(100) NOT NULL,
    `accountName` varchar(200) DEFAULT NULL,
    `objectId` varchar(100) NOT NULL,
    `processType` varchar(100) DEFAULT NULL,
    `description` varchar(250) DEFAULT NULL,
    `noOfLines` int(11) DEFAULT NULL,
    PRIMARY KEY (`accountId`,`objectId`),
    KEY `objectId` 
    (`objectId`,`description`,`accountId`,`accountName`,`noOfLines`)
    )ENGINE=InnoDB DEFAULT CHARSET=latin1;

Below table has 175k Records

CREATE TABLE `us_input_opp_oppitem` (
  `objectId` bigint(20) NOT NULL,
  `createdBy` varchar(100) DEFAULT NULL,
  `changedBy` varchar(100) DEFAULT NULL,
  `numberInt` varchar(100) NOT NULL,
  PRIMARY KEY (`objectId`,`numberInt`),
  KEY `objectId` (`objectId`,`createdBy`,`changedBy`,`numberInt`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Below table has 2.1 million records

CREATE TABLE `us_input_opp_oppsalesteam` (
  `transactionNumber` varchar(100) NOT NULL,
  `item` varchar(100) NOT NULL,
  `partnerFunction` varchar(100) DEFAULT NULL,
  `partnerFunctionText` varchar(200) DEFAULT NULL,
  PRIMARY KEY (`transactionNumber`,`item`),
  KEY `item` (`item`,`partnerFunction`,`partnerFunctionText`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


SELECT oh.objectId, oh.description, oi.`createdBy`, oi.`changedBy`, oh.description, oh.processType, 
os.`item`, os.`partnerFunction`, os.`partnerFunctionText`     
FROM us_input_opp_oppheader oh 
LEFT join us_input_opp_oppitem oi ON oi.objectId = oh.objectId 
LEFT join us_input_opp_oppsalesteam os ON os.transactionNumber = oh.objectId 
WHERE (os.Item != 0 OR oh.noOfLines = 0) 
GROUP BY oh.objectId, oi.numberInt;

我认为如果您使用内部联接或联接而不是左联接,则将花费更短的时间

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM