chonglt chonglt - 1 month ago 8
MySQL Question

MySQL query is extremely slow

I have a complex SQL query that linked 6 tables and I found out that it takes about 2 minutes (which I think it shouldn't) to display the result. The result will return about 5400++ records. Is there any way to redesign the query so that it will be able to execute a bit faster? Thank you for help!

My current query is:

SELECT *,
(SELECT MasterDescription FROM sys_master_code WHERE MasterId = mktg_account.CategoryId) AS CustomerCategory,
(CASE WHEN mktg_account.BusinessSourceId = 97 THEN mktg_account.BusinessSourceOthers ELSE (SELECT MasterDescription FROM sys_master_code WHERE MasterId = mktg_account.BusinessSourceId) END) AS BusinessSource,
(SELECT TreeName FROM sys_tree WHERE TreeId = mktg_account.TreeId) AS PhaseName,
(SELECT SolicitorName FROM sys_solicitor WHERE sys_solicitor.SolicitorId =(SELECT SolicitorId FROM mktg_spa WHERE mktg_spa.AccountId = mktg_account.AccountId)) AS SolicitorName,
(SELECT MasterCode FROM sys_master_code WHERE sys_master_code.MasterId = mktg_account.AgentCompany) AS AgentCompanyText,
(SELECT MasterCode FROM sys_master_code WHERE sys_master_code.MasterId = mktg_account.FinancialTypeId) AS FinanceSource,
(SELECT UserName FROM sys_user WHERE UserId = mktg_account.CreatedBy) AS CreatedByName
FROM mktg_account
WHERE TreeId IN (SELECT TreeId FROM sys_user_access_right WHERE UserId = 4)
AND IsApproved = '1' AND IsDeleted <>'1'
ORDER BY AccountNo

CREATE TABLE IF NOT EXISTS mktg_account (
AccountId int(10) unsigned NOT NULL AUTO_INCREMENT,
AccountNo varchar(30) NOT NULL DEFAULT '',
AccountStatus varchar(1) DEFAULT NULL,
TreeId int(10) unsigned NOT NULL DEFAULT '0',
SalesDate datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
PurchasePrice double NOT NULL DEFAULT '0',
SalesPersonId int(10) unsigned DEFAULT NULL,
FinancialTypeId int(10) unsigned DEFAULT NULL,
BillingCustomerId int(10) unsigned DEFAULT NULL,
EventId int(11) NOT NULL DEFAULT '0',
CategoryId int(11) NOT NULL DEFAULT '0',
RealEstateAgentId int(11) NOT NULL DEFAULT '0',
BusinessSourceId int(10) unsigned DEFAULT NULL,
BusinessSourceOthers varchar(300) DEFAULT NULL,
SalesPromotionId int(10) unsigned DEFAULT NULL,
Remarks text,
AgentName varchar(100) NOT NULL DEFAULT '',
AgentCompany varchar(100) NOT NULL DEFAULT '',
AgentContact varchar(100) DEFAULT NULL,
AgentRemarks text,
CreatedDateTime datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
CreatedBy int(10) unsigned NOT NULL DEFAULT '0',
PurgedDateTime datetime DEFAULT NULL,
PurgedBy int(10) unsigned DEFAULT NULL,
PurgedIP varchar(20) DEFAULT NULL,
IsDeleted tinyint(3) unsigned NOT NULL DEFAULT '0',
IsApproved tinyint(3) unsigned NOT NULL DEFAULT '1',
IsPurge tinyint(3) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (AccountId)
)

CREATE TABLE IF NOT EXISTS sys_master_code (
MasterId int(10) unsigned NOT NULL AUTO_INCREMENT,
MasterType varchar(20) NOT NULL DEFAULT '',
MasterCode varchar(100) NOT NULL DEFAULT '',
MasterDescription varchar(500) NOT NULL DEFAULT '',
CreatedDateTime datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
CreatedBy int(10) unsigned NOT NULL DEFAULT '0',
CreatedIP varchar(20) NOT NULL DEFAULT '',
IsDeleted tinyint(3) unsigned NOT NULL DEFAULT '0',
IsApproved tinyint(3) unsigned NOT NULL DEFAULT '1',
IsActive varchar(1) NOT NULL DEFAULT 'Y',
PRIMARY KEY (MasterId)
)

CREATE TABLE IF NOT EXISTS sys_tree (
TreeId int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Tree Running Id',
TreeName varchar(150) NOT NULL DEFAULT '',
IsDeleted tinyint(3) unsigned DEFAULT NULL COMMENT '1 = deleted',
PRIMARY KEY (TreeId)
)

CREATE TABLE IF NOT EXISTS sys_solicitor (
SolicitorId int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Running id number',
SolicitorCode varchar(50) NOT NULL DEFAULT '',
SolicitorName varchar(150) NOT NULL DEFAULT '',
SolicitorType varchar(50) NOT NULL DEFAULT '',
ContactPerson varchar(50) NOT NULL DEFAULT '',
PhoneNo varchar(50) NOT NULL DEFAULT '' COMMENT 'phone number',
Email varchar(40) NOT NULL DEFAULT '' COMMENT 'email address',
CreatedDateTime datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
CreatedBy int(10) unsigned NOT NULL DEFAULT '0',
IsDeleted tinyint(3) unsigned DEFAULT NULL COMMENT '1 = deleted',
IsApproved tinyint(3) unsigned DEFAULT NULL COMMENT '0 = waiting approval',
PRIMARY KEY (SolicitorId)
)

CREATE TABLE IF NOT EXISTS sys_user (
UserId int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'User Running Id',
UserLoginId varchar(25) NOT NULL DEFAULT '' COMMENT 'User Name For Log In',
UserName varchar(150) NOT NULL DEFAULT '' COMMENT 'User Real Name',
Password varchar(50) NOT NULL DEFAULT '' COMMENT 'Password For Log in',
EmailAddress varchar(100) NOT NULL DEFAULT '',
IsDeleted tinyint(3) unsigned DEFAULT NULL COMMENT '1 = deleted',
CreatedDateTime datetime NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT 'Date and time this record created',
CreatedBy int(10) unsigned NOT NULL DEFAULT '0',
IsApproved tinyint(3) unsigned DEFAULT NULL COMMENT '0 = waiting approval',
IsActive varchar(1) NOT NULL DEFAULT 'Y',
PRIMARY KEY (UserId)
)

CREATE TABLE IF NOT EXISTS sys_user_access_right (
UserId int(10) unsigned NOT NULL AUTO_INCREMENT,
TreeId int(10) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (UserId,TreeId),
KEY FK_SysUserAccessRight_SysTree (TreeId)
)

CREATE TABLE IF NOT EXISTS mktg_spa (
SpaId int(10) unsigned NOT NULL AUTO_INCREMENT,
AccountId int(10) unsigned NOT NULL DEFAULT '0',
SPADate datetime DEFAULT NULL,
AgreementNo varchar(50) DEFAULT NULL,
AgreementStatus varchar(10) DEFAULT NULL,
AgreementDate datetime DEFAULT NULL,
SPAReference varchar(100) DEFAULT NULL,
SolicitorId int(10) unsigned DEFAULT NULL,
SolicitorRefNo varchar(50) DEFAULT NULL,
TitleDetailReason varchar(200) DEFAULT NULL,
Remark text,
CreatedDateTime datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
CreatedBy int(10) unsigned NOT NULL DEFAULT '0',
IsDeleted tinyint(3) unsigned NOT NULL DEFAULT '0',
IsApproved tinyint(3) unsigned NOT NULL DEFAULT '1',
PRIMARY KEY (SpaId)
)


I run EXPLAIN and it show the table :Query Analysis

Answer

You should use join instead of subqueries. Written with join, your query would look like

SELECT mktg_account.*,
  tbCustomerCategory.MasterDescription as CustomerCategory, 
  (CASE WHEN mktg_account.BusinessSourceId = 97 
   THEN mktg_account.BusinessSourceOthers 
   ELSE tbBusinessSource.MasterDescription 
   END) AS BusinessSource, 
  sys_tree.TreeName as PhaseName, 
  sys_solicitor.SolicitorName,
  tbAgentCompanyText.MasterCode as AgentCompanyText,
  tbFinanceSource.MasterCode as FinanceSource,
  sys_user.UserName as CreatedByName 
from mktg_account
left join sys_master_code as tbCustomerCategory on tbCustomerCategory.MasterId = mktg_account.CategoryId
left join sys_master_code as tbBusinessSource on tbBusinessSource.MasterId = mktg_account.BusinessSourceId
left join sys_tree on sys_tree.TreeId = mktg_account.TreeId
left join mktg_spa on mktg_spa.AccountId = mktg_account.AccountId
left join sys_solicitor on sys_solicitor.SolicitorId = mktg_spa.SolicitorId
left join sys_master_code as tbAgentCompanyText on tbAgentCompanyText.MasterId = mktg_account.AgentCompany
left join sys_master_code as tbFinanceSource on tbFinanceSource.MasterId = mktg_account.FinancialTypeId
left join sys_user on sys_user.UserId = mktg_account.CreatedBy
join sys_user_access_right on sys_user_access_right.TreeId = mktg_account.TreeId 
WHERE sys_user_access_right.UserId = 4
AND mktg_account.IsApproved = '1' AND mktg_account.IsDeleted <> '1'
ORDER BY mktg_account.AccountNo;

And you are missing an index on mktg_spa, so add it:

create index idx_mktg_spa_AccountId on mktg_spa (AccountId);

(You might want to try this first, it might already improve your query enough without converting it to join).

There might be some potential for an index on mktg_account, but it really depends on your data. If most of your rows are "deleted" or "not approved", you can try e.g.

create index idx_mktg_account_IsApprovedAccountNoIsDeleted 
on mktg_account (IsApproved, AccountNo, IsDeleted);

But if your query returns more than just a small percentage of your rows, this index might actually slow down your query, so test without it first.