AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 315435
Accepted
Parikshit Bharadwaj
Parikshit Bharadwaj
Asked: 2022-08-10 11:14:46 +0800 CST2022-08-10 11:14:46 +0800 CST 2022-08-10 11:14:46 +0800 CST

优化一个查询多个表和多个条件

  • 772

我需要优化具有多个连接的查询。并且还具有多个 OR AND 条件,这些条件取决于用户输入。

生成的查询是:

SELECT  emp.name as entryby, l.cname as location_name, sp.product AS product_name, 
        sp.refcode as pro_refcode, sup.vendorname, sp.mrpgross AS mrpgross,
        pro.cp AS cost_price, cust.name AS customer_names, cust.phone AS mobile_no,
        sp.uom1 as TotalQTY, sp.category_name, auth.*
FROM erp_salesorderproductsdetails sp
LEFT JOIN erp_salesorder s ON s.id = sp.salesorder_id
LEFT JOIN geopos_employees emp ON emp.id = s.entryby
LEFT JOIN geopos_locations l ON s.loc = l.id
LEFT JOIN geopos_productall pro ON pro.product_code = sp.refcode
LEFT JOIN geopos_supplier sup ON sup.id = pro.vendor
LEFT JOIN geopos_customers cust ON cust.id = s.customer
LEFT JOIN authorize_po auth ON auth.salesorder_id = s.id
WHERE sp.salesorder_id = 301 
AND ( sp.category_name IN(SPECTACLES, SPECTACLE LENSES) )
OR sp.salesorder_id = 310 
AND ( sp.category_name IN(SPECTACLES, SPECTACLE LENSES) )
OR sp.salesorder_id = 3234 
AND (sp.category_name IN(SPECTACLES, SPECTACLE LENSES) )

在这里,sp.salesorder_id根据用户输入,where 条件中的 AND 可以是多个。要检索大约 700 - 800 条记录,大约需要 2 到 3.5 分钟。

但大多数数据计数超过 2000。在这种情况下,它的执行效率不高。任何人都可以提出任何建议吗?

我还分享了正在生成的查询的解释结果。但是,共享表结构并不是一个好主意,因为有些表的列数超过 50。

1   SIMPLE  sp  ALL NULL    NULL    NULL    NULL    6211    Using where 
1   SIMPLE  s   eq_ref  PRIMARY PRIMARY 4   erp_billing.sp.salesorder_id    1       
1   SIMPLE  emp eq_ref  PRIMARY PRIMARY 4   erp_billing.s.entryby   1   Using where 
1   SIMPLE  l   eq_ref  PRIMARY PRIMARY 4   erp_billing.s.loc   1   Using where 
1   SIMPLE  pro ALL NULL    NULL    NULL    NULL    340264  Using where; Using join buffer (flat, BNL join) 
1   SIMPLE  sup eq_ref  PRIMARY PRIMARY 4   erp_billing.pro.vendor  1   Using where 
1   SIMPLE  cust    eq_ref  PRIMARY PRIMARY 4   erp_billing.s.customer  1   Using where 
1   SIMPLE  auth    ALL NULL    NULL    NULL    NULL    13  Using where; Using join buffer (flat, BNL join) 
mysql php
  • 1 1 个回答
  • 60 Views

1 个回答

  • Voted
  1. Best Answer
    Ergest Basha
    2022-08-10T12:48:07+08:002022-08-10T12:48:07+08:00

    我建议添加以下复合键:

    ALTER TABLE erp_salesorderproductsdetails ADD INDEX `idx_1` (`salesorder_id`,`category_name`);
    ALTER TABLE geopos_productall ADD INDEX `idx_2` (`product_code`,`vendor`);
    ALTER TABLE erp_salesorder  ADD INDEX `idx_3` (`id`,`entryby`,`customer`);
    

    将最后一个查询部分从

    WHERE sp.salesorder_id = 301 
    AND ( sp.category_name IN(SPECTACLES, SPECTACLE LENSES) )
    OR sp.salesorder_id = 310 
    AND ( sp.category_name IN(SPECTACLES, SPECTACLE LENSES) )
    OR sp.salesorder_id = 3234 
    AND (sp.category_name IN(SPECTACLES, SPECTACLE LENSES) )
    

    至

    WHERE sp.salesorder_id in (301,310,3234) 
    AND  sp.category_name IN('SPECTACLES', 'SPECTACLE LENSES')
    

    编辑:在 authorize_po 表上添加另一个键

    ALTER TABLE authorize_po  ADD INDEX `salesorder_id` (`salesorder_id`);
    
    • 3

相关问题

  • 是否有任何 MySQL 基准测试工具?[关闭]

  • 我在哪里可以找到mysql慢日志?

  • 如何优化大型数据库的 mysqldump?

  • 什么时候是使用 MariaDB 而不是 MySQL 的合适时机,为什么?

  • 组如何跟踪数据库架构更改?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve