MYSQL 查看速度变慢
MYSQL View Slowed Down
我有一个 mysql 视图,当它正在解释的数据库由于编程缺陷而过载时速度变慢。我修复了该缺陷并将数据库恢复到非常小的大小。
我有一个数据库,在同一个 host/server 上有相同的表和相同的数据。它有相同的观点。该视图的查询在 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$$
以及视图:
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))))
)$$
我的回答果然和我想的一样。某种缓存。
我从 PHPmyAdmin 执行了以下操作:
勾选table
修复table
优化 table
冲洗 table
现在查询的性能非常好。
year
varchar(45) DEFAULT NULL,
有一个 YEAR 数据类型;使用它。
添加INDEX(year)
添加INDEX(client)
添加INDEX(tax_returns)
如果这些建议不够有用,请返回...
请为另一个table提供SHOW CREATE TABLE
。
请提供EXPLAIN SELECT ...
在您将始终设置的列上使用 NOT NULL
。
我有一个 mysql 视图,当它正在解释的数据库由于编程缺陷而过载时速度变慢。我修复了该缺陷并将数据库恢复到非常小的大小。
我有一个数据库,在同一个 host/server 上有相同的表和相同的数据。它有相同的观点。该视图的查询在 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$$
以及视图:
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))))
)$$
我的回答果然和我想的一样。某种缓存。
我从 PHPmyAdmin 执行了以下操作:
勾选table 修复table 优化 table 冲洗 table
现在查询的性能非常好。
year
varchar(45) DEFAULT NULL,
有一个 YEAR 数据类型;使用它。
添加INDEX(year)
添加INDEX(client)
添加INDEX(tax_returns)
如果这些建议不够有用,请返回...
请为另一个table提供SHOW CREATE TABLE
。
请提供EXPLAIN SELECT ...
在您将始终设置的列上使用 NOT NULL
。