mysql语句如何优化 4表连查,原来使用多视图太慢

kingskyboy 2013-11-07 05:11:27
--rm_customer_user 用户表 字段 CustomerId 客户ID,UserName 用户名,Mobile 手机号码,Status 状态
--rm_customer_company 用户公司表 CustomerId 客户ID(FK) Company 公司名称 ProjectId 产品类型
--rm_agent_user 代理商表 AgentId 代理ID(FK) Company 代理商公司名称
--rm_agent_customer 代理商绑定客户表 AgentId 代理ID(FK) CustomerId客户ID(FK) ProjectType产品类型


SELECT u.CustomerId,u.UserName AS UserName,u.Mobile AS Mobile,company.Company AS Company,u.`Status`,z.Company AS agentCompany FROM rm_customer_user AS u
LEFT JOIN rm_customer_company AS company ON u.CustomerId=company.CustomerId AND company.ProjectId=3
LEFT JOIN
(SELECT `t`.`CustomerId` AS `CustomerId`,`a`.`Company` AS `Company` from `rm_agent_customer` `t` LEFT JOIN `rm_agent_user` `a` ON `a`.`AgentId` = `t`.`AgentId` WHERE `t`.`ProjectType` = 3
) AS z
ON z.CustomerId=u.CustomerId;


查询3号产品用户基本信息以及对应3号产品公司名称,外加3号产品代理商公司名称




语句执行时间看效率 第一次
7S
第二次
1.272s
第三次
0.912s

和mysql缓存有一定的关系


...全文
346 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Spring89 2013-11-07
  • 打赏
  • 举报
回复
rm_customer_user 使用了全表扫描,所以这里要加索引! <deriverd2>这里也是全表扫描,需要建索引! <deriverd2>,它是从第二个select中出来的,都是在select from子句中的select 这么复杂的SQL,建议你拆开,这样查询真的很慢,也不好优化!我以前有条SQL也很SQL,结果也是全表扫描,加索引,强制使用索引,结果还是一样!一但拆开速度就快了! 这一步都没做好,加缓存干嘛呢!越往后查询越慢。
kingskyboy 2013-11-07
  • 打赏
  • 举报
回复
引用 4 楼 rucypli 的回复:
我看你给的建表语句没有rm_agent_customer的ProjectType索引
索引没贴上来,agentId customerId 都有索引 3000多的数据就这样了
rucypli 2013-11-07
  • 打赏
  • 举报
回复
我看你给的建表语句没有rm_agent_customer的ProjectType索引
kingskyboy 2013-11-07
  • 打赏
  • 举报
回复
引用 2 楼 rucypli 的回复:
rm_agent_customer的ProjectType上加索引
已经加过了
rucypli 2013-11-07
  • 打赏
  • 举报
回复
rm_agent_customer的ProjectType上加索引
kingskyboy 2013-11-07
  • 打赏
  • 举报
回复

DROP TABLE IF EXISTS `rm_agent_customer`;
CREATE TABLE `rm_agent_customer` (
  `AgentCusId` int(11) NOT NULL AUTO_INCREMENT,
  `AgentId` int(11) DEFAULT '0',
  `CustomerId` int(11) DEFAULT '0',
  `InsertTime` int(10) DEFAULT '0',
  `ProjectType` int(2) DEFAULT '0',
  `UpdateTime` int(10) DEFAULT '0',
  `Status` int(1) DEFAULT '0',
  PRIMARY KEY (`AgentCusId`),
  KEY `i_agentId` (`AgentId`) USING BTREE
) ENGINE=MyISAM AUTO_INCREMENT=1814 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of rm_agent_customer
-- ----------------------------
INSERT INTO `rm_agent_customer` VALUES ('73', '73', '240', '1378803198', '1', '0', '1');
INSERT INTO `rm_agent_customer` VALUES ('72', '73', '295', '1378798681', '1', '0', '1');
INSERT INTO `rm_agent_customer` VALUES ('71', '55', '294', '1378797505', '1', '0', '1');



-- ----------------------------
-- Table structure for `rm_agent_user`
-- ----------------------------
DROP TABLE IF EXISTS `rm_agent_user`;
CREATE TABLE `rm_agent_user` (
  `AgentId` int(11) NOT NULL AUTO_INCREMENT,
  `RealName` varchar(50) DEFAULT '',
  `UserName` varchar(100) DEFAULT '',
  `Password` varchar(100) DEFAULT '',
  `Fax` varchar(50) DEFAULT '',
  `Email` varchar(100) DEFAULT '',
  `Company` varchar(255) DEFAULT '',
  `Country` varchar(100) DEFAULT '',
  `Province` varchar(50) DEFAULT '',
  `City` varchar(50) DEFAULT '',
  `Town` varchar(100) DEFAULT '',
  `Area` varchar(50) DEFAULT '',
  `SubArea` varchar(50) DEFAULT '',
  `Street` varchar(255) DEFAULT '',
  `Address` varchar(500) DEFAULT '',
  `Mobile` varchar(50) DEFAULT '',
  `Qicq` varchar(20) DEFAULT '',
  `Tele` varchar(20) DEFAULT '',
  `IdCard` varchar(20) DEFAULT '',
  `InsertTime` int(10) DEFAULT '0',
  `Level` int(1) DEFAULT '0',
  `ParentId` int(11) DEFAULT '1',
  `UpdateTime` int(10) DEFAULT '0',
  `IsYz` int(1) DEFAULT '0',
  `Salesman` varchar(255) DEFAULT '',
  `Agent` varchar(255) DEFAULT '',
  `IsUse` int(1) DEFAULT '0',
  `Status` int(11) DEFAULT '0',
  `Money` decimal(11,3) DEFAULT '0.000',
  `YzId` int(11) DEFAULT '0',
  `StatusOne` int(1) DEFAULT '0',
  `StatusTwo` int(1) DEFAULT '0',
  `TypeOne` varchar(255) DEFAULT '',
  `TypeTwo` varchar(255) DEFAULT '',
  `TypeThree` varchar(255) DEFAULT '',
  `StatusThree` int(1) DEFAULT '0',
  `LastLoginTime` int(10) DEFAULT '0',
  `UserType` int(1) DEFAULT '1' COMMENT '1 代理商 2雇员',
  PRIMARY KEY (`AgentId`)
) ENGINE=MyISAM AUTO_INCREMENT=2430 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of rm_agent_user
-- ----------------------------



INSERT INTO `rm_agent_user` VALUES ('42', '', 'lxyd', 'c4af508c6ef1619e755d14fc6d29e1d4', '', '测试@126.com', '红花生公司', '', '001029', '001029008', '001029008001', '', '', '', 'cc', '13850665428', '', '5250997', '', '1377743076', '1', '1', '1377843114', '0', '', '', '0', '1', '0.000', '0', '0', '0', 'http://www.cg.com', 'cc', 'cc', '0', '1378198488', '1');
INSERT INTO `rm_agent_user` VALUES ('46', '深圳地虎科技有限公司', 'test001', 'e10adc3949ba59abbe56e057f20f883e', '', '383508147@qq.com', '深圳地虎科技有限公司', '001', '001012', '001012001', '', '', '', '', '广东省', '15171002199', '', '0755-28855992', '', '1377786794', '1', '1', '1378100465', '0', '', '', '0', '1', '0.000', '0', '0', '0', 'http://www.baidu.com', '六点半', '游戏人生', '0', '1382931569', '1');
INSERT INTO `rm_agent_user` VALUES ('47', '测试公司', 'test2', '96e79218965eb72c92a549dd5a330112', '', '', '测试公司', '001', '001018', '001018014', '001018014012', '', '', '', '', '', '', '', '', '1377787623', '1', '1', '1377797019', '0', '', '', '0', '1', '0.000', '0', '0', '0', '', '', '', '0', '0', '1');


-- ----------------------------
-- Table structure for `rm_customer_company`
-- ----------------------------
DROP TABLE IF EXISTS `rm_customer_company`;
CREATE TABLE `rm_customer_company` (
  `Id` int(11) NOT NULL AUTO_INCREMENT,
  `Company` varchar(255) DEFAULT NULL COMMENT '公司名称',
  `ProjectId` int(11) DEFAULT NULL,
  `InsertTime` int(10) DEFAULT NULL,
  `CustomerId` int(11) DEFAULT NULL,
  `SimpleName` varchar(255) DEFAULT NULL,
  `City` varchar(255) DEFAULT NULL,
  `Province` varchar(255) DEFAULT NULL,
  `Area` varchar(255) DEFAULT NULL,
  `Descn` varchar(1000) DEFAULT NULL,
  `Industry` varchar(255) DEFAULT NULL,
  `SubArea` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`Id`),
  KEY `i_projectId` (`ProjectId`) USING BTREE
) ENGINE=MyISAM AUTO_INCREMENT=22623 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of rm_customer_company
-- ----------------------------

INSERT INTO `rm_customer_company` VALUES ('11150', '发斯蒂芬', '1', '1383633899', '61', null, null, null, null, null, null, null);
INSERT INTO `rm_customer_company` VALUES ('11151', '', '1', '1111111111', '62', null, null, null, null, null, null, null);
INSERT INTO `rm_customer_company` VALUES ('22616', '林德集团', '1', '1383808213', '1938', null, null, null, null, null, null, null);
INSERT INTO `rm_customer_company` VALUES ('22617', '林德集团', '2', '1383808213', '1938', null, null, null, null, null, null, null);
INSERT INTO `rm_customer_company` VALUES ('22618', '林德集团', '3', '1383808213', '1938', null, null, null, null, null, null, null);
INSERT INTO `rm_customer_company` VALUES ('22619', '林德集团', '4', '1383808213', '1938', null, null, null, null, null, null, null);
INSERT INTO `rm_customer_company` VALUES ('22620', '林德集团', '5', '1383808213', '1938', null, null, null, null, null, null, null);
INSERT INTO `rm_customer_company` VALUES ('22621', '林德集团', '6', '1383808213', '1938', null, null, null, null, null, null, null);
INSERT INTO `rm_customer_company` VALUES ('22622', '林德集团', '7', '1383808213', '1938', null, null, null, null, null, null, null);

-- ----------------------------
-- Table structure for `rm_customer_user`
-- ----------------------------
DROP TABLE IF EXISTS `rm_customer_user`;
CREATE TABLE `rm_customer_user` (
  `CustomerId` int(11) NOT NULL AUTO_INCREMENT,
  `RealName` varchar(50) DEFAULT '',
  `UserName` varchar(100) DEFAULT '',
  `Password` varchar(100) DEFAULT '',
  `Sex` int(1) DEFAULT '3',
  `Type` int(1) DEFAULT NULL,
  `InsertTime` int(10) DEFAULT '0',
  `UpdateTime` int(10) DEFAULT '0',
  `IsYz` int(1) DEFAULT '0',
  `YzId` int(11) DEFAULT '0',
  `Contury` varchar(50) DEFAULT '',
  `Province` varchar(50) DEFAULT '',
  `City` varchar(50) DEFAULT '',
  `Town` varchar(100) DEFAULT '',
  `Area` varchar(50) DEFAULT '',
  `SubArea` varchar(50) DEFAULT '',
  `Street` varchar(255) DEFAULT '',
  `Address` varchar(500) DEFAULT '',
  `Mobile` varchar(50) DEFAULT '',
  `Qicq` varchar(20) DEFAULT '',
  `Tele` varchar(20) DEFAULT '',
  `IdCard` varchar(20) DEFAULT '',
  `Status` int(1) DEFAULT '1',
  `AgentId` int(11) DEFAULT '0',
  `Active` int(1) DEFAULT '0',
  `Fax` varchar(100) DEFAULT '',
  `Email` varchar(100) DEFAULT '',
  `Company` varchar(255) DEFAULT '',
  `LastLoginTime` int(10) DEFAULT '0',
  `Level` int(10) DEFAULT '0',
  `Jifeng` int(11) DEFAULT '0',
  `Contacts` varchar(255) DEFAULT NULL COMMENT '联系人',
  `Industry` varchar(255) DEFAULT NULL COMMENT '行业',
  `IndustryName` varchar(255) DEFAULT NULL COMMENT '行业名称',
  PRIMARY KEY (`CustomerId`)
) ENGINE=MyISAM AUTO_INCREMENT=1939 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of rm_customer_user
-- ----------------------------
INSERT INTO `rm_customer_user` VALUES ('60', '斯卡拉', 'zhangyiping@shusheng.com', '1e10adcfdsbe56e057f20f883e', '1', '1', '1377176845', '1381214407', '0', '0', '', '', '', '', '', '', '', '厦门市软件园二期', '13587879799', '', '0592-2626888', '', '1', '0', '0', '0592-2585888', 'dd', '厦门书生集团', '1383813317', '0', '0', null, null, null);
INSERT INTO `rm_customer_user` VALUES ('61', '发顺丰', 'zzl001@ggl.com', '1e10adc3dsfds9abbe56e057f20f883e', '3', '2', '1377177550', '1377335402', '0', '0', '', '', '', '', '', '', '', '发顺丰', '13598745698', '', '05925487966', '', '1', '0', '0', '', 'zzl001@ggl.com', '发斯蒂芬', '1379058722', '0', '0', null, null, null);
INSERT INTO `rm_customer_user` VALUES ('62', '', 'bjjingsehong@163.com', 'ef7fdsfdsffc5774100f87fe2f437a435', '3', '1', '1377178070', '0', '0', '0', '', '', '', '', '', '', '', '', '', '', '', '', '1', '0', '0', '', 'bjjingsehong@163.com', '', '0', '0', '0', null, null, null);
INSERT INTO `rm_customer_user` VALUES ('63', '星星', 'c@a.com', '4297f44b13955235245b2497399d7a93', '0', '3', '1377178115', '1383632612', '0', '0', '', '', '', '', '', '', '', '厦门市湖里区湖里大道', '13399880099', '23478', '05928877665', '', '1', '0', '0', '', 'c@a.com', '书生天下', '1383810432', '0', '0', '古龙', null, null);
INSERT INTO `rm_customer_user` VALUES ('64', '', '2541972065@qq.com', 'b2401fdsfsd58c50c94ff3dab12d57356', '3', '2', '1377183805', '0', '0', '0', '', '', '', '', '', '', '', '', '', '', '', '', '1', '0', '0', '', '2541972065@qq.com', '', '1377504075', '0', '0', null, null, null);
INSERT INTO `rm_customer_user` VALUES ('65', '', 'zzl003@ggl.com', 'e10adcfdsfds9abbe56e057f20f883e', '3', '2', '1377185662', '0', '0', '0', '', '', '', '', '', '', '', '', '', '', '', '', '1', '0', '0', '', 'zzl003@ggl.com', '', '1379039397', '0', '0', null, null, null);
INSERT INTO `rm_customer_user` VALUES ('66', 'wumeilan', 'wumeilanss@126.com', 'e10dsfds49ba59abbe56e057f20f883e', '3', '2', '1377217731', '1378452978', '0', '0', '', '', '', '', '', '', '', '厦门东渡金鼎路', '18900000000', '1245215', '0529-2203000', '', '1', '0', '0', '', '2369720973@qq.com', '厦门小白兔公司', '1379487092', '0', '0', null, null, null);
INSERT INTO `rm_customer_user` VALUES ('67', '', '380448794@qq.com', 'e1f805874fsdfdsf54ae62e45afc0559', '3', '1', '1377218393', '0', '0', '0', '', '', '', '', '', '', '', '', '', '', '', '', '1', '0', '0', '', '380448794@qq.com', '', '1377826809', '0', '0', null, null, null);

56,679

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧