正确形成长时间查询执行myodbc语法

时间:2010-07-28 11:54:23

标签: sql mysql ms-access

我正在尝试为Access构建一个SQL查询,用myodbc连接链接表来从互联网上检索数据,但是完成查询的时间太长了大约五分钟,所以我认为问题在于查询:

SELECT COUNT([o].[orders_id]) AS howmany_orders, 
       (SELECT SUM([op1].[products_quantity]) FROM orders_total AS ot1, orders AS o1, orders_products AS op1
        WHERE [o1].[date_purchased] >=date()-30 and [o1].[orders_id] = [op1].[orders_id] and [ot1].[orders_id] = [op1].[orders_id] and [ot1].[class]="ot_total" and [o1].[orders_status] = 1 and [op1].[products_id]=[op].[products_id]
        GROUP BY [op1].[products_id]
        ) AS pendiente,
        [op].[products_model], 
        Round((((7+1)*(howmany_orders/90))+1)-(p.stock_real- IIF(pendiente>0,pendiente,0)), 0) AS pedir,
        p.ref_id

FROM orders_total AS ot, orders AS o, orders_products AS op INNER JOIN Productos AS p ON Mid([op].[products_model],4) LIKE p.ref_id

WHERE [o].[date_purchased] >=date()-90 and [o].[orders_id] = [op].[orders_id] and [ot].[orders_id] = [op].[orders_id] and [ot].[class]="ot_total" and [o].[orders_status] IN (7, 1) and ((p.fuera_de_stock)=False) and ((p.suspendido)=False) and  ((p.quitar_de_la_web)=False)

GROUP BY [op].[products_model], p.ref_id, p.stock_real, [op].[products_id];

我一眼就看到“ LIKE ”运算符可能是此处的问题之一:

 INNER JOIN Productos AS p ON Mid([op].[products_model],4) LIKE p.ref_id

但我无法替代 = 运算符

感谢您的帮助!

EDITING: 我已经将查询缩减到了但同时也是如此:

SELECT COUNT(o.orders_id) AS howmany_orders, (
SELECT SUM(opz.products_quantity) FROM orders AS oz, orders_products AS opz WHERE oz.date_purchased >=date()-30 and oz.orders_id = opz.orders_id and oz.orders_status = 1 and opz.products_id=op.products_id GROUP BY opz.products_id
) AS pendiente, op.products_model, Round((((7+1)*(howmany_orders/90))+1)-(p.stock_real-IIf(pendiente>0,pendiente,0)),0) AS pedir, p.ref_id
FROM orders AS o, orders_products AS op INNER JOIN Productos AS p ON op.products_model=p.cod
WHERE o.date_purchased>=date()-90 And o.orders_id=op.orders_id And o.orders_status In (7,1) And ((p.suspendido)=False) And ((p.quitar_de_la_web)=False)
GROUP BY op.products_model, p.ref_id, p.stock_real, op.products_id;

1 个答案:

答案 0 :(得分:0)

是的,这是你的问题。数据库无法使用任何索引进行连接,因此必须执行表扫描。无论如何你可以坚持这些数据,所以你不必做MID语句,只是加入吗?即在您的products_model表中有一个额外的列,其中MID数据存储在那里,并且该列上的连接