where substr(lot_id,1,2) not in('CA','CW','ES','E0','EM') and lot_type = 'Production' AND LOT_ID NOT LIKE 'H%' and substr(lot_id,7,4)='.00F' and ope_category in ('Ship','Unship')请优化下

时间: 2023-12-21 07:04:35 浏览: 33
该 SQL 语句可以优化如下: 1. 将 WHERE 子句中的函数调用改为使用列范围,例如: ``` WHERE lot_id >= 'F1000000.00F' AND lot_id < 'Z9999999.00G' ``` 2. 创建索引以覆盖 WHERE 子句中的所有列,例如: ``` CREATE INDEX idx_lot_id ON table_name (lot_id); CREATE INDEX idx_lot_type ON table_name (lot_type); CREATE INDEX idx_ope_category ON table_name (ope_category); ``` 3. 如果表比较大,可以考虑将这个表分区,例如按照 lot_type 进行分区,这样可以进一步提高查询性能。 4. 如果查询经常使用,则可以将结果缓存起来,以避免每次查询都进行全表扫描。 综上所述,这些优化措施可以显著提高 SQL 查询的性能。
相关问题

select lot_id, max(claim_time)claim_time, count(case when(ope_category='Ship')then lot_id else null end) as LS, count(case when(ope_category='Unship') then lot_id else null end) as LUS from f3rpt.F3_TB_DAILY_FABOUT where substr(lot_id,1,2) not in('CA','CW','ES','E0','EM') and lot_type = 'Production' AND LOT_ID NOT LIKE 'H%' and substr(lot_id,7,4)='.00F' and ope_category in ('Ship','Unship') and year(claim_time) = year(current date - 1 days) and month(claim_time) = month(current date - 1 days) group by lot_id) as a where LS - LUS > 0 ,以上sql如何优化

这个SQL语句中,可以优化的地方如下: 1. 在WHERE子句中,使用函数year()和month()可能导致索引失效,可以考虑将其改为claim_time的范围查询,如下所示: ``` AND claim_time >= DATE(current date - 1 month) AND claim_time < DATE(current date) ``` 2. 在COUNT函数中,使用CASE语句进行条件统计,可以改为使用SUM函数,如下所示: ``` SUM(CASE WHEN ope_category='Ship' THEN 1 ELSE 0 END) AS LS SUM(CASE WHEN ope_category='Unship' THEN 1 ELSE 0 END) AS LUS ``` 3. 在WHERE子句中,使用了聚合函数,将查询结果限制在LS-LUS>0的记录,这种方式不利于索引的使用,可以考虑使用HAVING子句,如下所示: ``` HAVING LS > LUS ``` 4. 在子查询中,只需要查询claim_time的最大值,可以使用MAX函数来代替ORDER BY和LIMIT 1子句,如下所示: ``` MAX(claim_time) AS claim_time ``` 综上所述,优化后的SQL语句如下: ``` SELECT lot_id, MAX(claim_time) AS claim_time, SUM(CASE WHEN ope_category='Ship' THEN 1 ELSE 0 END) AS LS, SUM(CASE WHEN ope_category='Unship' THEN 1 ELSE 0 END) AS LUS FROM f3rpt.F3_TB_DAILY_FABOUT WHERE substr(lot_id,1,2) NOT IN ('CA','CW','ES','E0','EM') AND lot_type = 'Production' AND LOT_ID NOT LIKE 'H%' AND substr(lot_id,7,4)='.00F' AND ope_category IN ('Ship','Unship') AND claim_time >= DATE(current date - 1 month) AND claim_time < DATE(current date) GROUP BY lot_id HAVING LS > LUS; ```

SELECT lot_id, max(claim_time) claim_time, count( case when (open_category= 'Ship') then lot_id else null end ) as LS, COUNT( case when (ope_category = "Unship") then lot_id else null end ) as LUS FROM f3rpt.F3_TB_DAILY_FABOUT WHERE SUBSTR(lot_id,1,2) not in ('CA','CW','ES','EO','EM') AND lot_type ='Production' and lot_id not like 'H%' and SUBSTR(lot_id,7,4) = '.00F' and ope_category in ('Ship','Unship') and YEAR(claim_time) = YEAR(current date -1 days) and month(claim_time) = month (current date -1 days) group by lot_id 请优化下这条sql

这个 SQL 查询中有一些可以优化的地方,以下是优化建议: 1. 使用别名缩短代码长度并提高可读性。 2. 使用 EXISTS 或者 INNER JOIN 来代替子查询,以提高查询效率。 3. 使用 BETWEEN 来代替 YEAR 和 MONTH 函数的组合使用,以提高查询效率。 4. 使用 IN 来代替多个 OR 条件的组合使用,以提高查询效率。 5. 删除无用的 else null,简化代码。 优化后的 SQL 查询如下所示: SELECT lot_id, MAX(claim_time) AS claim_time, COUNT(CASE WHEN open_category = 'Ship' THEN lot_id END) AS LS, COUNT(CASE WHEN ope_category = 'Unship' THEN lot_id END) AS LUS FROM f3rpt.F3_TB_DAILY_FABOUT t1 WHERE SUBSTR(lot_id, 1, 2) NOT IN ('CA', 'CW', 'ES', 'EO', 'EM') AND lot_type = 'Production' AND lot_id NOT LIKE 'H%' AND SUBSTR(lot_id, 7, 4) = '.00F' AND ope_category IN ('Ship', 'Unship') AND claim_time BETWEEN DATE('now', '-1 day', 'start of month') AND DATE('now', '-1 day', 'start of month', '+1 month', '-1 day') GROUP BY lot_id;

相关推荐

优化以下Oracle语句: SELECT SUBSTR(msn.serial_number, 1, 10) genset_sn, msi2.segment1 Genset_BOM_NUM, msi2.inventory_item_id, msi.segment1 key_component, mut1.serial_number component_sn, msi.description component_desc, wdj.date_completed, (SELECT MAX(aps.vendor_name) FROM ap_suppliers aps, bom_resources bor, mtl_unit_transactions mut, po_headers_all poh, po_lines_all pol, wip_osp_resources_val_v wor WHERE aps.vendor_id = poh.vendor_id AND bor.resource_id = wor.resource_id AND poh.po_header_id = pol.po_header_id AND pol.item_id = bor.purchase_item_id AND wor.wip_entity_id = mut.transaction_source_id AND mut.serial_number = mut1.serial_number AND mut.inventory_item_id = mut1.inventory_item_id AND mut.organization_id = mut1.organization_id AND mut.receipt_issue_type = 2 AND mut.transaction_source_type_id = 5 ) supplier FROM mtl_material_transactions mmt1, mtl_material_transactions mmt2, mtl_parameters mpa, mtl_serial_numbers msn, mtl_system_items msi, mtl_system_items msi2, mtl_transaction_types mtt1, mtl_transaction_types mtt2, mtl_unit_transactions mut1, mtl_unit_transactions mut2, wip_discrete_jobs_v wdj WHERE mmt1.inventory_item_id = mut1.inventory_item_id AND mmt1.organization_id = mut1.organization_id AND WDJ.PRIMARY_ITEM_ID = msi2.INVENTORY_ITEM_ID AND mmt1.transaction_id = mut1.transaction_id AND mmt1.transaction_source_id = wdj.wip_entity_id AND mmt1.transaction_type_id = mtt1.transaction_type_id AND mtt1.transaction_type_name = 'WIP Issue' AND NOT EXISTS (SELECT 'WIP Negative Issue or WIP Return' FROM mtl_material_transactions mmt3, mtl_transaction_types mtt3, mtl_unit_transactions mut3 WHERE mmt3.transaction_id = mut3.transaction_id AND mmt3.transaction_type_id = mtt3.transaction_type_id AND mmt3.transaction_date > mmt1.transaction_date AND mtt3.transaction_type_name IN ('WIP Negative Issue', 'WIP Return') AND mut3.serial_number = mut1.serial_number AND mut3.inventory_item_id = mut1.inventory_item_id) AND mmt2.transaction_id = mut2.transaction_id AND mmt2.transaction_source_id = wdj.wip_entity_id AND mmt2.transaction_type_id = mtt2.transaction_type_id AND mtt2.transaction_type_name = 'WIP Completion' AND mpa.organization_code = 'WHP' AND msn.current_organization_id = mpa.organization_id AND LENGTH(msn.serial_number) >= 10 AND msi.inventory_item_id = mmt1.inventory_item_id AND msi.organization_id = mmt1.organization_id AND (msi.planning_make_buy_code = 2 OR msi.segment1 LIKE 'SO%') AND mut2.serial_number = msn.serial_number AND mut2.inventory_item_id = msn.inventory_item_id AND mut2.organization_id = mpa.organization_id AND msi2.ORGANIZATION_ID = '323'

优化sql:SELECT we.organization_id ,we.wip_entity_id ,case when wl.line_id is null then we.wip_entity_name else '' end wip_entity_name ,we.primary_item_id ,mtt.transaction_type_name ,mmt.transaction_date ,bd.department_code ,mmt.inventory_item_id ,mmt.subinventory_code ,mta.reference_account ,br.resource_code ,lu2.meaning as line_type_name ,mta.base_transaction_value ,mta.cost_element_id ,flv.meaning as cost_element ,wdj.class_code job_type_code ,ml.meaning job_type_name FROM (select * from gerp.mtl_material_transactions where substr(transaction_date,1,7) >= '2023-06' and transaction_source_type_id = 5) mmt inner join gerp.wip_entities we on mmt.organization_id = we.organization_id inner join gerp.mtl_transaction_accounts mta on mta.transaction_source_id = we.wip_entity_id and mta.transaction_id = mmt.transaction_id and mta.transaction_source_type_id = 5 inner join gerp.mtl_transaction_types mtt on mtt.transaction_type_id = mmt.transaction_type_id inner join mfg_lookups lu2 on lu2.lookup_code = mta.accounting_line_type and lu2.lookup_type = 'CST_ACCOUNTING_LINE_TYPE' inner join gerp.mtl_system_items_b msi on msi.inventory_item_id = mmt.inventory_item_id and msi.organization_id = mta.organization_id left join gerp.bom_departments bd on bd.department_id = mmt.department_id left join gerp.bom_resources br on br.resource_id = mta.resource_id left join gerp.wip_lines wl on wl.line_id = mmt.repetitive_line_id left join gerp.wip_discrete_jobs wdj on wdj.wip_entity_id = mta.transaction_source_id left join gerp.fnd_lookup_values_vl flv on cast(mta.cost_element_id as string) = flv.lookup_code and flv.lookup_type = 'CST_COST_CODE_TYPE' left join mfg_lookups ml on ml.lookup_code = wdj.job_type and ml.lookup_type = 'WIP_DISCRETE_JOB' 。其中mmt,we,mta,msi,wdj数据量很大

最新推荐

recommend-type

5153-微信小程序个人日程安排微信小程序的实现+ssm(源码+数据库+lun文).zip

本系统主要针对计算机相关专业的正在做毕业设计的学生和需要项目实战练习的学习者,可作为毕业设计、课程设计、期末大作业。本系统主要针对计算机相关专业的正在做毕业设计的学生和需要项目实战练习的学习者,可作为毕业设计、课程设计、期末大作业。本系统主要针对计算机相关专业的正在做毕业设计的学生和需要项目实战练习的学习者,可作为毕业设计、课程设计、期末大作业。本系统主要针对计算机相关专业的正在做毕业设计的学生和需要项目实战练习的学习者,可作为毕业设计、课程设计、期末大作业。
recommend-type

旅行智能推荐系统.zip

本项目旨在开发一个智能旅行推荐系统。该系统利用Yelp数据集,采用混合的内容推荐和协同过滤方法为用户提供个性化的旅行建议。通过整合React前端和fla后sk端技术,系统提供了一个无缝的用户体验,能够分析用户偏好和行为,并根据这些信息进行定制化的推荐。此外,该系统具有适应性,随着用户交互的持续进行,能够不断完善推荐结果。整体而言,这是一个结合了先进推荐算法和现代化Web技术的先进项目,旨在为用户提供更加智能和个性化的旅行建议。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
recommend-type

自己整理的Ansible 自动化运维实战笔记分享给需要的同学.zip

自己整理的Ansible 自动化运维实战笔记分享给需要的同学.zip
recommend-type

C++多态实现机制详解:虚函数与早期绑定

C++多态性实现机制是面向对象编程的重要特性,它允许在运行时根据对象的实际类型动态地调用相应的方法。本文主要关注于虚函数的使用,这是实现多态的关键技术之一。虚函数在基类中声明并被标记为virtual,当派生类重写该函数时,基类的指针或引用可以正确地调用派生类的版本。 在例1-1中,尽管定义了fish类,但基类animal中的breathe()方法并未被声明为虚函数。因此,当我们创建一个fish对象fh,并将其地址赋值给animal类型的指针pAn时,编译器在编译阶段就已经确定了函数的调用地址,这就是早期绑定。这意味着pAn指向的是animal类型的对象,所以调用的是animal类的breathe()函数,而不是fish类的版本,输出结果自然为"animalbreathe"。 要实现多态性,需要在基类中将至少一个成员函数声明为虚函数。这样,即使通过基类指针调用,也能根据实际对象的类型动态调用相应的重载版本。在C++中,使用关键字virtual来声明虚函数,如`virtual void breathe();`。如果在派生类中重写了这个函数,例如在fish类中定义`virtual void breathe() { cout << "fishbubble" << endl; }`,那么即使使用animal类型的指针,也能调用到fish类的breathe()方法。 内存模型的角度来看,当一个派生类对象被赋值给基类指针时,基类指针只存储了派生类对象的基类部分的地址。因此,即使进行类型转换,也只是访问基类的公共成员,而不会访问派生类特有的私有或保护成员。这就解释了为什么即使指针指向的是fish对象,调用的还是animal的breathe()函数。 总结来说,C++多态性是通过虚函数和早期/晚期绑定来实现的。理解这两个概念对于编写可扩展和灵活的代码至关重要。在设计程序时,合理使用多态能够提高代码的复用性和可维护性,使得程序结构更加模块化。通过虚函数,可以在不改变接口的情况下,让基类指针动态调用不同类型的子类对象上的同名方法,从而展现C++强大的继承和封装特性。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

Parallelization Techniques for Matlab Autocorrelation Function: Enhancing Efficiency in Big Data Analysis

# 1. Introduction to Matlab Autocorrelation Function The autocorrelation function is a vital analytical tool in time-domain signal processing, capable of measuring the similarity of a signal with itself at varying time lags. In Matlab, the autocorrelation function can be calculated using the `xcorr
recommend-type

matlab处理nc文件,nc文件是1979-2020年的全球降雨数据,获取一个省份区域内的日降雨量,代码怎么写

在MATLAB中处理`.nc`(NetCDF)文件通常需要使用`netcdf`函数库,它是一个用于读写多种科学数据格式的工具。对于全球降雨数据,你可以按照以下步骤编写代码: 1. 安装必要的库(如果还没有安装): ```matlab % 如果你尚未安装 netcdf 包,可以安装如下: if ~exist('netcdf', 'dir') disp('Installing the NetCDF toolbox...') addpath(genpath(fullfile(matlabroot,'toolbox','nco'))); end ``` 2. 加载nc文件并查看其结
recommend-type

Java多线程与异常处理详解

"Java多线程与进程调度是编程领域中的重要概念,尤其是在Java语言中。多线程允许程序同时执行多个任务,提高系统的效率和响应速度。Java通过Thread类和相关的同步原语支持多线程编程,而进程则是程序的一次执行实例,拥有独立的数据区域。线程作为进程内的执行单元,共享同一地址空间,减少了通信成本。多线程在单CPU系统中通过时间片轮转实现逻辑上的并发执行,而在多CPU系统中则能实现真正的并行。 在Java中,异常处理是保证程序健壮性的重要机制。异常是程序运行时发生的错误,通过捕获和处理异常,可以确保程序在遇到问题时能够优雅地恢复或终止,而不是崩溃。Java的异常处理机制使用try-catch-finally语句块来捕获和处理异常,提供了更高级的异常类型以及finally块确保关键代码的执行。 Jdb是Java的调试工具,特别适合调试多线程程序。它允许开发者设置断点,查看变量状态,单步执行代码,从而帮助定位和解决问题。在多线程环境中,理解线程的生命周期和状态(如新建、运行、阻塞、等待、结束)以及如何控制线程的执行顺序和同步是至关重要的。 Java的多线程支持包括Thread类和Runnable接口。通过继承Thread类或者实现Runnable接口,用户可以创建自己的线程。线程间同步是多线程编程中的一大挑战,Java提供了synchronized关键字、wait()、notify()和notifyAll()等方法来解决这个问题,防止数据竞争和死锁的发生。 在实际应用中,多线程常用于网络编程、数据库访问、GUI应用程序(如Swing或JavaFX)的事件处理、服务器端的并发处理等场景。例如,一个Web服务器可能需要同时处理多个客户端请求,这时使用多线程可以显著提升性能。此外,多线程在动画制作、游戏开发、多媒体应用等领域也发挥着重要作用,因为它允许同时处理渲染、计算和用户交互等多个任务。 Java的多线程与进程调度是构建高效、健壮应用的基础,而异常处理则提升了程序的稳定性。通过深入理解和熟练运用这些概念,开发者可以创建出更加灵活和可靠的软件系统。"
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依
recommend-type

The Application of Autocorrelation Function in Economics: Economic Cycle Analysis and Forecasting Modeling

# Application of Autocorrelation Function in Economics: Analysis and Forecasting Models for Economic Cycles ## 1. Theoretical Foundations of Autocorrelation Function The Autocorrelation Function (ACF) is a statistical tool used to measure the correlation between data points in time series data tha