search
HomeBackend DevelopmentPHP Tutorialunion all效率低下,该用存储过程、视图、临时表哪种方法来解决?

union all效率低下,大概union all数十张表,单表select仅需0.0003-0.002秒,但union all后需要0.6秒左右
想改善下效率

这种情况该用存储过程、视图、临时表哪种方法来解决?


回复讨论(解决方案)

很明显,你的数据组织的不合理

很明显,你的数据组织的不合理

难道从来不会有这种情况发生吗

组织合理就没有这种情况

你把同类数据垂直分配在数十个表中,就是不合理

组织合理就没有这种情况

你把同类数据垂直分配在数十个表中,就是不合理

这些表的结构不完全相同的。当时就是考虑到放在一张表会复杂。所以就分开了。不过后来发现要汇总时,问题就来了。

大家有没有好的办法。之前建立的那些表已经应用到一些功能上了。现在改也太大工程了。

你能说一下你的那些表的结构吗?
既然并不相同,那么叠加在一起的意义何在?

你能说一下你的那些表的结构吗?
既然并不相同,那么叠加在一起的意义何在?
我刚刚查了一下,7个表,分别是
1.销售凭证、红字销售凭证
2.采购凭证、红字采购凭证
3.收款凭证、付款凭证、转账凭证。
  
其中1组和2组结构是一样的,1组和2组只有一个字段不同。
3组中的收款凭证和付款凭证也是只有一字段不同,转账凭证的结构则是被收款凭证和付款凭证所包含。

补充一下,1组和2组各自内部的表结构是一样的。1组和2组之间只有一个字段不同。

1组和2组不同的字段是,
1组有‘销售单号、客户编码‘。
2组有 ’采购单号、供应商编码’。

1组和2组的表结构是这样,能合并成一个表吗?

数据量有多大啊?要达到分表的情况下了。分表的话,这种水平分也不合理吧。

你用 union 这不就对了!
这7个表是平行的,就算要连接也只能是 join

数据量有多大啊?要达到分表的情况下了。分表的话,这种水平分也不合理吧。

其实它们都是描述不同的东西。所以才分表的。

1.销售凭证、红字销售凭证
2.采购凭证、红字采购凭证
3.收款凭证、付款凭证、转账凭证。




你用 union 这不就对了!
这7个表是平行的,就算要连接也只能是 join

将1、2组表结构合并,3组表结构合并,这样合理么。

不必了吧?你不是说已经应用到一些功能上了吗?

不必了吧?你不是说已经应用到一些功能上了吗?

就是各自的一个增删查改的功能。我不知道这样设计,以后会不会有其它不好处理的地方出现。

愿意的话,你可以描述一下业务流程

刚刚那7个表是接下来要做的几个功能的数据来源。

比如,有一个日记账的功能,这个功能就是要从刚刚那7个表中的记录搜索有“银行存款和现金”这两个科目存在的明细记录。要把有这两个存的的明细都抽出来,然后汇总显示。

第二个,明细账功能,这个也是要从刚刚那7个表中搜索除了“银行存款和现金”以外的所有科目存在的明细记录,汇总显示。

第三个,科目汇总表,这个是在一定时间范围内,所有凭证按科目汇总。

第四个,总分类账,就是显示每个科目发生的所有明细记录。

可能描述的不是很好。请见谅。

union all 之后还做了order之类? 要不不该差这么大啊

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

Simple Guide: Sending Email with PHP ScriptSimple Guide: Sending Email with PHP ScriptMay 12, 2025 am 12:02 AM

PHPisusedforsendingemailsduetoitsbuilt-inmail()functionandsupportivelibrarieslikePHPMailerandSwiftMailer.1)Usethemail()functionforbasicemails,butithaslimitations.2)EmployPHPMailerforadvancedfeatureslikeHTMLemailsandattachments.3)Improvedeliverability

PHP Performance: Identifying and Fixing BottlenecksPHP Performance: Identifying and Fixing BottlenecksMay 11, 2025 am 12:13 AM

PHP performance bottlenecks can be solved through the following steps: 1) Use Xdebug or Blackfire for performance analysis to find out the problem; 2) Optimize database queries and use caches, such as APCu; 3) Use efficient functions such as array_filter to optimize array operations; 4) Configure OPcache for bytecode cache; 5) Optimize the front-end, such as reducing HTTP requests and optimizing pictures; 6) Continuously monitor and optimize performance. Through these methods, the performance of PHP applications can be significantly improved.

Dependency Injection for PHP: a quick summaryDependency Injection for PHP: a quick summaryMay 11, 2025 am 12:09 AM

DependencyInjection(DI)inPHPisadesignpatternthatmanagesandreducesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itallowspassingdependencieslikedatabaseconnectionstoclassesasparameters,facilitatingeasiertestingandscalability.

Increase PHP Performance: Caching Strategies & TechniquesIncrease PHP Performance: Caching Strategies & TechniquesMay 11, 2025 am 12:08 AM

CachingimprovesPHPperformancebystoringresultsofcomputationsorqueriesforquickretrieval,reducingserverloadandenhancingresponsetimes.Effectivestrategiesinclude:1)Opcodecaching,whichstorescompiledPHPscriptsinmemorytoskipcompilation;2)DatacachingusingMemc

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools