简体   繁体   English

如何减少视图的执行时间?

[英]How can I reduce the execution time of a view?

Here is my query: 这是我的查询:

select count(1) AS `number`,
       `pronexo_pronexo`.`unique_products`.`product_id` AS `product_id`,
       `pronexo_pronexo`.`products`.`name` AS `name`
from ( `pronexo_pronexo`.`unique_products`
join `pronexo_pronexo`.`products` 
on((`pronexo_pronexo`.`unique_products`.`product_id` = `pronexo_pronexo`.`products`.`id`)))
where isnull(`pronexo_pronexo`.`unique_products`.`exit_time`)
group by `pronexo_pronexo`.`unique_products`.`product_id`

And here is the result of EXPLAIN : 这是EXPLAIN的结果:

在此处输入图片说明

Any idea should I create an index on which column(s) to make it faster? 我知道应该在哪个列上创建索引以使其更快吗? It takes about 3 sec to be executed now. 现在大约需要3秒钟才能执行。 By the way the data isn't huge by now. 顺便说一下,目前数据还不是很大。

select  count(1) AS `number`,
        u.`product_id`,
        p.`name`
    from  `pronexo_pronexo`.`unique_products` AS u
    join  `pronexo_pronexo`.`products` AS p
      on  u.`product_id` =  p.`id`
     where  isnull(u.`exit_time`)
    group by  u.`product_id`

Change isnull(u.exit_time) to u.exit_time IS NULL . u.exit_time IS NULL isnull(u.exit_time)更改为u.exit_time IS NULL

Add INDEX(exit_time, product_id) to unique_products . INDEX(exit_time, product_id)添加到unique_products

In products , if id is not indexed, index it. products ,如果未对id进行索引,请对其进行索引。

For further discussion, please provide SHOW CREATE TABLE . 为了进一步讨论,请提供SHOW CREATE TABLE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM