簡體   English   中英

Mysql 查詢在存儲過程中花費了太多時間

[英]Mysql query taking too much time in stored procedure

我正在使用存儲過程來獲取一些數據,但是我的 sql 查詢需要很長時間:

   CREATE DEFINER=``@`%` PROCEDURE `test_sp`(in _orderIds varchar(255))
     BEGIN
     drop temporary table if exists temp;
     create temporary table temp( val int );
     set @sql = concat("insert into temp (val) values ('", 
     replace((_orderIds), ",", "'),('"),"');");
     prepare stmt1 from @sql;
     execute stmt1;

      SELECT  p.productid,p.HSNNo,oi.orderid,cast(IFNULL(oi.dispatched,0) as 
      decimal(10,2)) 
        FROM orderitem oi
        inner JOIN producttax pt on pt.ProductId=oi.productid
        inner JOIN product p on p.ProductId=pt.ProductId
        WHERE oi.orderid in(select val from temp)
        GROUP BY oi.productid,p.HSNNo,oi.orderid,oi.orderitemid;
         END

如果我將在沒有臨時表的情況下使用相同的,那么它的工作良好:

CREATE DEFINER=``@`%` PROCEDURE `test_sp`(in _orderIds varchar(255))
    BEGIN
  drop temporary table if exists temp;
    create temporary table temp( val int );
    set @sql = concat("insert into temp (val) values ('", 
    replace((_orderIds), ",", "'),('"),"');");
    prepare stmt1 from @sql;
    execute stmt1;
    SELECT  p.productid,p.HSNNo,oi.orderid,cast(IFNULL(oi.dispatched,0) as 
     decimal(10,2)) 

        FROM orderitem oi
        inner JOIN producttax pt on pt.ProductId=oi.productid
        inner JOIN product p on p.ProductId=pt.ProductId
        WHERE oi.orderid in(5465,7687,876)
        GROUP BY oi.productid,p.HSNNo,oi.orderid,oi.orderitemid;
        END

讓我們在加入其他表之前過濾第一個orderitem

create DEFINER=``@`%` procedure `test_sp`(in _orderIds varchar(255))
begin
    drop temporary table if exists temp;
    create temporary table temp(val int );
    set @sql = concat("insert into temp (val) values ('", 
    replace((_orderIds), ",", "'),('"),"');");
    prepare stmt1 from @sql;
    execute stmt1;

    select  p.productid,p.HSNNo,oi.orderid,cast(IFNULL(oi.dispatched,0) as 
        decimal(10,2)) 
    from 
        (select orderid, dispatched, productid from orderitem where orderid in (select val from temp)) oi
    inner join producttax pt on pt.ProductId=oi.productid
    inner join product p on p.ProductId=pt.ProductId    
    group by oi.productid,p.HSNNo,oi.orderid,oi.orderitemid;
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM