MySQL查询非常慢

时间:2016-10-26 06:00:40

标签: mysql

我有一个复杂的SQL查询链接了6个表,我发现显示结果需要大约2分钟(我认为它不应该)。结果将返回大约5400 ++个记录。有没有办法重新设计查询,以便它能够更快地执行?谢谢你的帮助!

我目前的查询是:

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)
)

我运行EXPLAIN并显示表格:Query Analysis

2 个答案:

答案 0 :(得分:1)

您应该使用join而不是子查询。用join编写,您的查询看起来像

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;

你错过了mktg_spa的索引,所以添加它:

create index idx_mktg_spa_AccountId on mktg_spa (AccountId);

(您可能希望首先尝试此操作,它可能已经足够改进您的查询而不将其转换为join)。

mktg_account上的索引可能存在一些潜力,但这实际上取决于您的数据。如果您的大多数行都被“删除”或“未批准”,您可以尝试例如。

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

但是,如果您的查询返回的行数不仅仅是一小部分,那么这个索引实际上可能会降低您的查询速度,因此请先测试它。

答案 1 :(得分:0)

我不打算为你重写整件事,但这是一个开始。适当编制索引后,此解决方案将比您目前的解决方案快几个数量级......

SELECT a.*
     , c.MasterDescription CustomerCategory
  FROM mktg_account a
  JOIN sys_master_code c
    ON c.MasterId = a.CategoryId
  JOIN sys_user_access_right r
    ON r.TreeId = a.TreeId 
 WHERE r.UserId = 4
   AND a.IsApproved = 1 
   AND IsDeleted <> 1
 ORDER 
    BY a.AccountNo