1

我有两个表结构如下,这些表有很多数据但不能改变表结构

表“售后”

CREATE TABLE IF NOT EXISTS `postsale` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `group_id` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `season` varchar(25) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `sale_no` int(5) NOT NULL,
  `auction_date` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `season_time` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `lot_no` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `invoice_no` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `origin` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `tea_type` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `sub_tea_type` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `category` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `mark` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `grade` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `no_of_packages` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `gross_wt` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `net_wt` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `auction_valuation` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `lsp_sp` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `package_type` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `package_no` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `quantity` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `auctioneer` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `auction_price` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `buyer` text CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `area` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `broker_code` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `csv` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `session` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=29623 ;

和表压轴

CREATE TABLE IF NOT EXISTS `finalsale` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `group_id` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `season` varchar(25) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `sale_no` int(5) NOT NULL,
  `auction_date` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `season_time` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `lot_no` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `invoice_no` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `origin` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `tea_type` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `sub_tea_type` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `category` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `mark` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `grade` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `no_of_packages` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `gross_wt` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `net_wt` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `auction_valuation` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `lsp_sp` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `package_type` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `package_no` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `quantity` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `auctioneer` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `auction_price` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `buyer` text CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `area` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `broker_code` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `csv` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `session` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=41365 ;

基于上面两个表,这个查询的执行时间是巨大的,所以我需要优化下面的查询

UPDATE `finalsale`,`postsale` 
SET 
  `finalsale`.`auction_price`=`postsale`.`auction_price`,
  `finalsale`.`csv`=`postsale`.`csv`,
  `finalsale`.`session`=`postsale`.`session` 
WHERE `finalsale`.`lot_no`=`postsale`.`lot_no` 
      AND `finalsale`.`group_id`=`postsale`.`group_id` 
      AND `finalsale`.`group_id`='201217CLGuwahatiJT'

请帮忙

4

3 回答 3

0

通过在表定义中选择正确的数据类型,您将获得显着的性能改进。

有几列似乎只包含数字信息,最好将其存储在某些int列中。

此外,某些日期的列auction_date应转换为日期数据类型。

还要考虑 varchar 列的长度。大多数时候你不需要那里的 255 个字符。

仔细查看Mysql Docu for datatypes

如果之后您遇到性能问题,您可能会考虑在某些列上创建索引。但我强烈建议您在重新设计表定义考虑这一点!

于 2012-04-25T13:03:37.223 回答
0

假设您不可能改进架构(正如 OP 评论中提到的那样......臃肿),您可能会通过在每个上添加索引来显着提升此特定查询的性能的表。lot_nogroup_id

由于您使用的是 InnoDB,因此您可能会考虑使用外键约束(这也包括索引),尽管它取决于我不知道的表的语义和生命周期。

于 2012-04-25T13:04:11.107 回答
0

添加以下索引:

单列:

finalsale: (`group_id`)

多栏:

postsale: (`group_id, lot_no`)

这些索引允许 MySQL 首先通过将 WHERE 子句中的常量应用于 finalsale 表来限制结果集,然后使用最小的结果集执行与 postale 表的连接,充分利用索引。

于 2012-04-25T13:54:07.623 回答