0

我创建了下表,其中包含有关产品的信息:

CREATE TABLE `AllProducts` (
 `insertionTime` datetime NOT NULL,
 `shop` varchar(40) CHARACTER SET utf8 NOT NULL,
 `name` varchar(100) CHARACTER SET utf8 NOT NULL,
 `category` varchar(40) CHARACTER SET utf8 NOT NULL,
 `description` text CHARACTER SET utf8 NOT NULL,
 `price` decimal(10,2) NOT NULL,
 `url` varchar(350) COLLATE latin1_german1_ci NOT NULL,
 `image` varchar(450) CHARACTER SET utf8 NOT NULL,
 `fromPrice` text CHARACTER SET utf8 NOT NULL,
 `deliveryCosts` decimal(10,2) NOT NULL,
 `stock` text CHARACTER SET utf8 NOT NULL,
 `deliveryTime` varchar(80) CHARACTER SET utf8 NOT NULL,
 `ean` varchar(32) CHARACTER SET utf8 NOT NULL,
 `color` varchar(40) CHARACTER SET utf8 NOT NULL,
 `size` varchar(40) CHARACTER SET utf8 NOT NULL,
 `brand` varchar(40) CHARACTER SET utf8 NOT NULL,
 KEY `category` (`category`),
 KEY `nameShopInsertiontime` (`name`,`shop`,`insertionTime`),
 KEY `name` (`name`),
 KEY `catNameShopInsert` (`category`,`name`,`shop`,`insertionTime`),
 KEY `eanName` (`ean`,`name`),
 KEY `eanInsertionTime` (`ean`,`insertionTime`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_german1_c

我想创建一个从该表中选择所有独特产品的视图(“原型产品”视图)。我表中的一些产品有 EAN 编号,但不是全部。如果有可用的 EAN 编号,我想将其用作 productId。如果它不可用,我想散列名称(小写)并将其用作产品 ID。

我创建了以下视图,它使用联合所有来区分上述情况。它可以工作,但需要 4.3 秒才能完成(在 200.000 行上)。这太慢了。

select 
max(`ap`.`name`) AS `name`,
max(`ap`.`description`) AS `description`,
min(`ap`.`price`) AS `price`,
max(`ap`.`image`) AS `image`,
`ap`.`ean` AS `ean`,
max(`ap`.`color`) AS `color`,
max(`ap`.`size`) AS `size`,
max(`ap`.`brand`) AS `brand`,
`ap`.`ean` AS `productId` 
from `AllProducts` `ap` 
where (not((`ap`.`ean` like ''))) 
group by `ap`.`ean` 

union all 

select 
`ap`.`name` AS `name`,
max(`ap`.`description`) AS `description`,
min(`ap`.`price`) AS `price`,
max(`ap`.`image`) AS `image`,
'' AS `ean`,
max(`ap`.`color`) AS `color`,
max(`ap`.`size`) AS `size`,
max(`ap`.`brand`) AS `brand`,
md5(lcase(`ap`.`name`)) AS `productId` 
from `AllProducts` `ap` 
where (`ap`.`ean` = '') 
group by `ap`.`name` order by NULL

如果有多种产品可用,则需要最长/最短的可用描述(或颜色/尺寸/其他)。我不一定需要最长或最短的值,而只需要任何值。

我的问题:我能做些什么来提高这个视图的性能?


使用 Mani 的评论,我切换到以下情况,这确实很快。

select 
`ap`.`name` AS `name`,
`ap`.`description` AS `description`,
a.category,
`ap`.`price` AS `price`,
`ap`.`image` AS `image`,
`ap`.`ean` AS `ean`,
`ap`.`color` AS `color`,
`ap`.`size` AS `size`,
`ap`.`brand` AS `brand`,
case when ap.ean not like '' then ap.ean else md5(lcase(ap.name)) end as productId
from `AllProducts` `ap`

这个视图是相当快的。但是,我仍然需要从这个新视图中选择唯一的 productId——包括名称、描述、类别等(视图称为 AllProductsView)。我尝试了以下方法,这导致查询比原始查询慢(6.5 秒):

select max(`apv`.`name`) AS `name`,
max(`apv`.`description`) AS  `description`,
max(`apv`.`category`) AS `category`,
min(`apv`.`price`) AS `price`,
max(`apv`.`image`) AS `image`,
max(`apv`.`color`) AS `color`,
max(`apv`.`size`) AS `size`,
max(`apv`.`brand`) AS `brand`,
`apv`.`productId` AS `productId` 
from `AllProductsView` `apv` 
group by `apv`.`productId`

任何人有建议如何加快这个查询?

4

1 回答 1

0

力击桌子两次。使用 ifnull 函数。

ifnull( ap. ean, md5(lcase( ap. name))) 作为产品 ID

于 2013-09-10T07:22:05.490 回答