sql - 运行查询时,MySQL挂起。如何提高效率?

标签 sql mysql query-optimization

我们正在尝试运行查询以获取所有未付款的发票。当我运行查询时,它将挂起我们的整个系统。我只是想知道是否有办法提高效率。 SQL不是我的强项。
$ query =“ SELECT SQL_CALC_FOUND_ROWS i.order_id,请访问,(SELECT SUM(items.item_qty)AS数量,来自WHERE items.order_id = o.order_id的项目)AS order_qty
                来自AS i项目
                INNER JOIN订单AS o ON o.order_id = i.order_id
                内联交易t ON.t_order_id = o.order_id
                IN.JOIN帐户上的INNER JOIN帐户o.acct_id = acct.acct_id
                INNER JOIN ship_to AS on ON.ship_id = st.ship_id
                WHERE o.order_status = 7 AND o.order_date>'2009-05-01 00:00:00'AND(SELECT SUM(items.item_price)作为商品的价格WHERE items.order_id = o.order_id)*(SELECT SUM( items.item_qty)作为商品的数量,其中item.order_id = o.order_id)+ o.order_ship_amount-(从交易中选择t.order_id = o.order_id AND trans_pending = 0)的总和!= acct.is_wholesale = 1
            o.order_id的分组
            o.order_date订购
            LIMIT $ offset,$ limit“;以下是所需表的表布局信息:

创建表items
  item_id int(11)非空auto_increment,order_id int(11)非空默认'0',prod_id int(11)非空默认'0',scat_id int(11)非空默认' 0',inv_id int(11)NOT NULL默认值'0',item_qty int(11)NOT NULL默认值'0',item_price float(10,3)NOT NULL默认值'0.000',item_mfg varchar(200)NOT NULL默认值'0',item_group int(11)NOT NULL默认值'0',item_ship_date datetime NOT NULL默认值'0000-00-00 00:00:00',date_created datetime NOT NULL默认为'0000-00-00 00:00:00',PRIMEARY KEY(item_id),KEY prod_idprod_id),KEY order_idorder_id),KEY inv_id),键inv_idscat_id))ENGINE = MyISAM AUTO_INCREMENT = 834659 DEFAULT CHARSET = latin1
CREATE TABLE scat_idorders int(11)NOT NULL auto_increment,order_id int(11)NOT NULL default'0',acct_id timestamp NOT NULL default CURRENT_TIMESTAMP,order_date datetime NOT NULL default'0000- 00-00 00:00:00',order_confirm_date日期时间NOT NULL默认值'0000-00-00 00:00:00',order_approval_date日期时间NOT NULL默认值'0000-00-00 00:00:00', order_deposit_date datetime NOT NULL默认值'0000-00-00 00:00:00',order_sent_to_mfg datetime NOT NULL默认值'0000-00-00 00:00:00',order_due_date datetime NOT NULL默认值'0000- 00-00 00:00:00',order_ship_date日期时间NOT NULL默认值'0000-00-00 00:00:00',order_exp_ship_date文本NOT NULL,order_ship_type float(10,2)NOT NULL默认值' 0.00',order_ship_amount int(11)NOT NULL默认值'0',order_mfg_name文本NOT NULL,order_notes int(11)NOT NULL默认值'0',order_status int(11)NOT NULL默认值'0 ',ship_id int(11)NOT NULL默认值'0',bill_id int(11)NOT NULL默认值'0',order_requested_quote int(11)NOT NULL默认值'0',order_submitted int(11 )NOT NULL默认值'0',order_origin varchar(25)NOT NULL默认值'',order_po_no int(11)NOT NULL默认值'0',qd_id int(11)NOT NULL默认值'0',< cc> datetime NOT NULL d efault'0000-00-00 00:00:00',order_inactive int(11)NOT NULL默认为'0',PRIMARY KEY(order_cancelled),KEY site_idorder_id),KEY ship_idship_id),密钥bill_idbill_id),密钥acct_idacct_id))ENGINE = MyISAM AUTO_INCREMENT = 20311622 DEFAULT CHARSET = latin1
CREATE TABLE site_idsite_id int(11)NOT NULL auto_increment,transactions int(11)NOT NULL default'0',trans_id text NOT NULL,order_id text NOT NULL,trans_pnref datetime NOT NULL默认值'0000-00-00 00:00:00',trans_card_type float(10,2)NOT NULL默认值'0.00',trans_date文本NOT NULL,trans_amount文本NOT NULL,trans_type文本NOT NULL,trans_tender文本NOT NULL,trans_po_no int(11)NOT NULL默认值'0',trans_origin datetime NOT NULL默认值'0000-00-00 00:00:00',trans_rep int(11) NOT NULL默认'0',trans_po_received int(11)NOT NULL默认'0',trans_inactive int(11)NOT NULL默认'0',trans_pending int(11)NOT NULL默认'0',主密钥(trans_secured),密钥site_idtrans_id),密钥cod_idorder_id))ENGINE = MyISAM AUTO_INCREMENT = 211554 DEFAULT CHARSET = latin1
    创建表site_idsite_id int(11)NOT NULL auto_increment,accounts时间戳NOT NULL默认CURRENT_TIMESTAMP,acct_id文本NOT NULL,acct_signup文本NOT NULL,acct_first文本NOT NULL,acct_last文字NOT NULL,acct_company文字NOT NULL,acct_email文字NOT NULL,acct_email_cc文字NOT NULL,acct_email_bcc文字NOT NULL,acct_phone文字NOT NULL,acct_fax int(11)NOT NULL默认值' 0',acct_password int(11)NOT NULL默认值'0',acct_default_ship int(1)NOT NULL默认值'0',acct_default_bill int(1)NOT NULL默认值'0',is_account int( 11)NOT NULL默认值'0',is_wholesale int(11)NOT NULL默认值'0',主键(site_id),KEY tpsg_idacct_id),KEY acct_default_shipacct_default_ship) ,键acct_default_billacct_default_bill),键site_idsite_id))ENGINE = MyISAM AUTO_INCREMENT = 264476 DEFAULT CHARSET = latin1
创建表tpsg_idtpsg_id int(11)NOT NULL auto_increment,ship_to int(11)NOT NULL默认值'0',ship_id文本NOT NULL,acct_id文本NOT NULL,ship_first文本NOT NULL,ship_last文本NOT NULL,ship_company文本NOT NULL,ship_address1文本NOT NULL,ship_address2文本NOT NULL,ship_city文本NOT NULL,ship_state文本NOT NULL,ship_zip文本NOT NULL ,ship_country文本NOT NULL,ship_phone文本NOT NULL,ship_fax int(1)无符号NOT NULL默认为'0',PRIMARY KEY(ship_notes),KEY ship_inactiveship_id))ENGINE = MyISAM AUTO_INCREMENT = 241339 DEFAULT CHARSET = latin1

我要此查询执行的操作是提取状态为7的所有订单的订单信息(在2009年5月1日之后),然后将订单中所有项目的价格乘以数量,然后将装运数量与从中减去已支付的金额并检查其是否不为0。此外,该帐户必须是批发帐户。我知道此查询的效率很低,但是我不确定该怎么做。我们的系统太不堪重负了,总体来说非常落后。
我将不胜感激任何帮助!

最佳答案

SELECT   
    o.order_id,  
    o.order_ship_amount,  
    SUM(i.item_qty * i.item_price) AS item_amount,  
    (   SELECT SUM(trans_amount) FROM transactions
        WHERE order_id = o.order_id AND trans_pending = 0
    ) AS trans_amount,
    ... etc.
FROM  
    orders AS o  
INNER JOIN accounts AS acct ON o.acct_id = acct.acct_id  
INNER JOIN ship_to AS st ON o.ship_id = st.ship_id  
INNER JOIN items AS i ON o.order_id = i.order_id  
WHERE   
    o.order_status = 7   
    AND o.order_date > '2009-05-01 00:00:00'  
    AND acct.is_wholesale = 1   
GROUP BY o.order_id  
HAVING item_amount + order_ship_amount - trans_amount != 0  
ORDER BY o.order_date  

关于sql - 运行查询时,MySQL挂起。如何提高效率?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3085653/

相关文章:

sql - sybase:如果可能的话,我如何删除所有表和存储过程?

mysql - 在 GROUP BY 中选择 MAX,但在 MYSQL 中将结果限制为 1

sql - MySQL - Max() 返回错误结果

mysql - 将 phpMyAdmin docker 图像连接到仅在 127.0.0.1 上监听的 HOST MySQL 服务器

sql - 递归关系 - 列出有主管和没有主管的人

sql - Oracle SQL 分层查询以了解两个元素之间的路径

mysql - 如果值是从另一个表中选择的,则更新表

scala - 在 Spark 1.6 中加入数据帧时未发生广播

sql - 查询性能: single column vs multiple column

mysql - "using join buffer"在 MySQL 查询的解释表中意味着什么?