Mysql在1中进行3次查询并提高性能

时间:2018-01-06 16:55:52

标签: mysql performance join

我尝试创建报告并运行4个查询,但性能非常糟糕。

我正在使用2张桌子

这个有2500件物品

CREATE TABLE `bolt_accounts` (
  `id` int(11) NOT NULL,
  `slug` varchar(128) COLLATE utf8_unicode_ci NOT NULL,
  `datecreated` datetime NOT NULL,
  `datechanged` datetime NOT NULL,
  `datepublish` datetime DEFAULT NULL,
  `datedepublish` datetime DEFAULT NULL,
  `username` varchar(32) COLLATE utf8_unicode_ci DEFAULT '',
  `ownerid` int(11) DEFAULT NULL,
  `status` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
  `templatefields` longtext COLLATE utf8_unicode_ci COMMENT '(DC2Type:json_array)',
  `managerid` varchar(128) COLLATE utf8_unicode_ci DEFAULT '',
  `parentid` varchar(256) COLLATE utf8_unicode_ci DEFAULT '',
  `name` varchar(256) COLLATE utf8_unicode_ci DEFAULT '',
  `qualify` varchar(256) COLLATE utf8_unicode_ci DEFAULT '',
  `regdate` date DEFAULT NULL,
  `city` varchar(256) COLLATE utf8_unicode_ci DEFAULT '',
  `phone` varchar(256) COLLATE utf8_unicode_ci DEFAULT '',
  `passhash` varchar(256) COLLATE utf8_unicode_ci DEFAULT '',
  `cookie` varchar(256) COLLATE utf8_unicode_ci DEFAULT '',
  `resettoken` varchar(256) COLLATE utf8_unicode_ci DEFAULT '',
  `block` tinyint(1) NOT NULL DEFAULT '0',
  `blocksms` tinyint(1) NOT NULL DEFAULT '0',
  `birthday` date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;


ALTER TABLE `bolt_accounts`
  ADD PRIMARY KEY (`id`),
  ADD KEY `IDX_9C703491989D9B62` (`slug`),
  ADD KEY `IDX_9C703491AFBA6FD8` (`datecreated`),
  ADD KEY `IDX_9C703491BE74E59A` (`datechanged`),
  ADD KEY `IDX_9C703491A5131421` (`datepublish`),
  ADD KEY `IDX_9C703491B7805520` (`datedepublish`),
  ADD KEY `IDX_9C7034917B00651C` (`status`),
  ADD KEY `IDX_9C703491C13A5CC2` (`managerid`),
  ADD KEY `IDX_9C703491856A684C` (`parentid`(255)),
  ADD KEY `IDX_9C7034911E6AC3AE` (`regdate`),
  ADD KEY `IDX_9C7034914709B432` (`birthday`);

另一个包含所有统计数据,其中有超过1 400 000个项目

CREATE TABLE `bolt_statistics` (
  `id` int(11) NOT NULL,
  `slug` varchar(128) COLLATE utf8_unicode_ci NOT NULL,
  `datecreated` datetime NOT NULL,
  `datechanged` datetime NOT NULL,
  `datepublish` datetime DEFAULT NULL,
  `datedepublish` datetime DEFAULT NULL,
  `username` varchar(32) COLLATE utf8_unicode_ci DEFAULT '',
  `ownerid` int(11) DEFAULT NULL,
  `status` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
  `templatefields` longtext COLLATE utf8_unicode_ci COMMENT '(DC2Type:json_array)',
  `managerid` varchar(256) COLLATE utf8_unicode_ci DEFAULT '',
  `statdate` datetime DEFAULT NULL,
  `lopv` double NOT NULL DEFAULT '0',
  `gope` double NOT NULL DEFAULT '0',
  `gopv` double NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

ALTER TABLE `bolt_statistics`
  ADD PRIMARY KEY (`id`),
  ADD KEY `IDX_BE38DFD2989D9B62` (`slug`),
  ADD KEY `IDX_BE38DFD2AFBA6FD8` (`datecreated`),
  ADD KEY `IDX_BE38DFD2BE74E59A` (`datechanged`),
  ADD KEY `IDX_BE38DFD2A5131421` (`datepublish`),
  ADD KEY `IDX_BE38DFD2B7805520` (`datedepublish`),
  ADD KEY `IDX_BE38DFD27B00651C` (`status`),
  ADD KEY `IDX_BE38DFD2C13A5CC2` (`managerid`(255));

所以问题是,当我把这些表连在一起时,性能变得很低......

SELECT ba.managerid,name,replace(phone,'+','') as phone,passhash, date_format(ba.datepublish,'%d.%m.%Y %H:%i') as datepublish, max(bs.lopv) as lopv, max(bs.gopv) as gopv 
FROM bolt_accounts ba 
LEFT JOIN bolt_statistics bs ON ba.managerid=bs.managerid 
WHERE (parentid='007-645930') 
AND (date(ba.datechanged)=('2018-01-06')) 
AND (date(bs.datecreated)=('2018-01-06')) 
GROUP BY ba.managerid 
ORDER BY gopv desc 

此查询将运行360-450ms~0,3秒 它将返回所有已经parentid = 007-645930的经理人 有点像这样:

managerid
007-663360
007-677590
007-697191
007-1526400
007-1155884
007-1842169
077-1564660
007-1883072
007-777143
007-1865946
007-1875083
007-1753407
007-1322124
007-1100631
007-1603795
007-1171656
007-1890892
007-1166247
007-1564611
007-1882959
007-1145375
007-1878383
007-1128857
007-1762655
007-1346877
007-1714252
007-1709538
007-1319044
007-1698517
007-1316756
007-1679094
007-1298984
007-1905146
007-1675451
007-1287166
007-1899632
007-1629224
007-1190862
007-1894824
007-1616741
007-1171665
007-1894330

我从该列表中获取1个id,并运行3个查询

SELECT max(s.lopv) as lopv, max(s.gopv) as gopv 
FROM bolt_statistics s WHERE (managerid='007-663360') 
AND (datecreated between DATE_FORMAT('2018-01-06' - INTERVAL 1 MONTH,'%Y-%m-28 23:00:00') and DATE_FORMAT(LAST_DAY('2018-01-06' - INTERVAL 1 MONTH),'%Y-%m-%d 23:59:59'))

执行时间20-25ms

SELECT max(s.lopv) as lopv, max(s.gopv) as gopv 
FROM bolt_statistics s 
WHERE (managerid='007-663360') 
AND (date(datecreated) = date('2018-01-06' -INTERVAL 1 day))

执行时间15-20ms

SELECT max(s.lopv) as lopv, max(s.gopv) as gopv 
FROM bolt_statistics s 
WHERE (managerid='007-663360') 
AND (date(datecreated) = date('2018-01-06' -INTERVAL 2 day))

执行时间15-20ms

当所有执行结束时,渲染php报告需要1.5秒(1500毫秒)。

我知道,我不太擅长mysql查询;))但我想知道,我怎样才能提高查询的性能?

如果我将所有这些查询合并在1中会更快吗?

1 个答案:

答案 0 :(得分:1)

那些字段真的需要一个完整的 256 字符吗?将它们更改为合理的数字,然后删除ADD KEY IDX_BE38DFD2C13A5CC2 (managerid(255))上的前缀等(前缀索引通常无用。)

不要将列隐藏在函数内(date(ba.datechanged))。代替:

AND ba.datechanged >= '2018-01-06' - INTERVAL 2 DAY
AND ba.datechanged  < '2018-01-06' - INTERVAL 3 DAY

注意:无论数据类型datechanged是什么,DATEDATETIMEDATETIME(6)TIMESTAMP,上述模式都能正常运行。优化器可以使用诸如......之类的索引。

之后,使用复合INDEX(managerid, datecreated)来显着提升性能。

使用派生表而不是LEFT JOINGROUP BY。这可能会提高速度。

什么是status?为什么VARCHAR(32)?如果它是一个简单的,低基数的值,不要自己索引;索引将不会被使用。

(可能会有更多提示,但这应该让你开始。)