【问题标题】:Mysql Query taking timeMysql 查询耗时
【发布时间】:2010-11-26 12:27:26
【问题描述】:

Qe : 1)

select 
  a.finishproductid 
from 
  tblt_invoiceorderitems a, tblm_invoiceorder b 
where 
  b.invoiceorderdate <= 'Current Date' and 
  a.invoiceorderid = b.invoiceorderid 

--- 它有超过 16k 条记录。

Qe : 2)

select jobcardid, stockcode 
from tblm_finishproduct 
where productionentrydate <= 'Current Date'

--- 它也有超过 16k 的记录。

现在我希望第二个查询不在第一个查询中。

select jobcardid, stockcode 
from 
  tblm_finishproduct 
where 
  productionentrydate <= 'CurrrntDate' and 
  finishproductid not in 
  (
    select 
      a.finishproductid 
    from 
      tblt_invoiceorderitems a, tblm_invoiceorder b 
    where 
      b.invoiceorderdate <= 'CurrrntDate' and 
      a.invoiceorderid = b.invoiceorderid
  );

现在需要一些时间

【问题讨论】:

  • 请使用 SQL 代码块。

标签: optimization mysql subquery


【解决方案1】:

这不能解决您的问题,但您不应该在 where 子句中使用 a.invoiceorderid = b.invoiceorderid - 尝试使用此查询代替第一个查询。 Natural join 比对整个表进行叉积并只选择匹配的行更有效。

select a.finishproductid 
from tblt_invoiceorderitems a 
natural join tblm_invoiceorder b 
where b.invoiceorderdate <= 'Current Date'

【讨论】:

    【解决方案2】:

    从不在中删除这个:

    b.invoiceorderdate <= 'CurrrntDate' and
    

    【讨论】:

      【解决方案3】:

      "select Table1, Table2, Table3" 的结果可能是 16k^3 或 16k^2。您需要使用主键进行“内部连接”或“连接”来计算搜索。

      再见。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-11-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-14
        • 2011-02-07
        • 2013-04-08
        相关资源
        最近更新 更多