MYSQL视图速度减慢

时间:2015-07-25 02:56:34

标签: mysql performance views

我有一个mysql视图,当它解释的数据库由于编程缺陷而变得过载时,它会减慢速度。我修复了这个漏洞,并将数据库清理成一个非常小的尺寸。

我在同一主机/服务器上有一个包含相同表和相同数据的数据库。它有相同的观点。该视图的查询在1秒内​​完成,但问题视图的查询需要20秒。

是否有缓存或其他我可能需要清除才能解决这个问题?

   select 
        `income`.`id` AS `id`
    from
        (`clientsWithIncome`
        left join `income` ON (`income`.`client` = `clientsWithIncome`.`client`)) 

以上是查询的最简化形式,它给我提出了问题。我可以在一个数据库上执行此操作,并在一秒钟内获得结果,然后在问题数据库上花费20秒。

delimiter $$

CREATE TABLE `income` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `index` varchar(45) DEFAULT NULL,
  `client` varchar(45) DEFAULT NULL,
  `entity_type` varchar(45) DEFAULT NULL,
  `date_received` varchar(45) DEFAULT NULL,
  `preparer` varchar(45) DEFAULT NULL,
  `date_began` varchar(45) DEFAULT NULL,
  `date_prepared` varchar(45) DEFAULT NULL,
  `date_assembled` varchar(45) DEFAULT NULL,
  `date_signed` varchar(45) DEFAULT NULL,
  `date_fed` varchar(45) DEFAULT NULL,
  `date_state` varchar(45) DEFAULT NULL,
  `notes` text,
  `status` varchar(45) DEFAULT NULL,
  `year` varchar(45) DEFAULT NULL,
  `highlight` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=93442 DEFAULT CHARSET=latin1$$

和VIEWs:

delimiter $$

CREATE ALGORITHM=UNDEFINED DEFINER=`xxxxxxxxxxx`@`localhost`
            SQL SECURITY DEFINER VIEW `clientsWithIncome` AS 
  select  `income`.`client` AS `client`
    from  `income`
    where  (`income`.`year` = (year((curdate() + interval 1 month)) - 1))
    union 
  select  `contacts`.`client` AS `client`
    from  `contacts`
    where  (`contacts`.`tax_returns` = 1)$$

delimiter $$

CREATE ALGORITHM=UNDEFINED DEFINER=`xxxxxxxxxxxx`@`localhost`
          SQL SECURITY DEFINER VIEW `currentIncome` AS
 select  `income`.`id` AS `id`,
       `income`.`index` AS `index`,
       `clientsWithIncome`.`client` AS `client`,
       `income`.`entity_type` AS `entity_type`,
       `income`.`date_received` AS `date_received`,
       `income`.`preparer` AS `preparer`,
       `income`.`date_began` AS `date_began`,
       `income`.`date_prepared` AS `date_prepared`,
       `income`.`date_assembled` AS `date_assembled`,
       `income`.`date_signed` AS `date_signed`,`income`.`date_fed` AS `date_fed`,
       `income`.`date_state` AS `date_state`,`income`.`notes` AS `notes`,
       `income`.`status` AS `status`,`income`.`year` AS `year`,
       `income`.`highlight` AS `highlight`
    from  (`clientsWithIncome`
            left join  `income`
                 on(((`income`.`client` = `clientsWithIncome`.`client`)
                and  (`income`.`year` = (year((curdate() + interval 1 month)) - 1))))
          )$$

2 个答案:

答案 0 :(得分:0)

我的答案结果证明是我的想法。某种缓存。

我从PHPmyAdmin执行了以下操作:

检查表 修理台 优化表 同花表

现在查询效果非常好。

答案 1 :(得分:0)

  

year varchar(45)DEFAULT NULL,

有YEAR数据类型;用它。

添加INDEX(year)

添加INDEX(client)

添加INDEX(tax_returns)

如果这些建议不够用,请回来......

请为另一张表提供SHOW CREATE TABLE

请提供EXPLAIN SELECT ...

对您将始终设置的列使用NOT NULL