search
HomeBackend DevelopmentPHP TutorialMySQL分表后,怎么查询所有表中的记录?

抛开性能等问题,一张表分成N张表后,如何查询所有分表 type = 1 的数据?
(一张用户表,分成10张表后,我想取所有用户 type = 1 的数据)

回复内容:

抛开性能等问题,一张表分成N张表后,如何查询所有分表 type = 1 的数据?
(一张用户表,分成10张表后,我想取所有用户 type = 1 的数据)

MySQL有一个MERGE存储引擎是专门做这个事情的,借这篇博文了解:Mysql MERGE引擎分表

(SELECT xx FROM table1 WHERE type=1) union
(SELECT xx FROM table1 WHERE type=1) union
(SELECT xx FROM table1 WHERE type=1) union
(SELECT xx FROM table1 WHERE type=1) 

如果你需要二次提取

  • 要么自己写程序从上面的union结果集里提取

  • 要么子查询

SELECT xxx FROM (
    (SELECT xx FROM table1 WHERE type=1) union
    (SELECT xx FROM table1 WHERE type=1) union
    (SELECT xx FROM table1 WHERE type=1) union
    (SELECT xx FROM table1 WHERE type=1) 
) t WHERE t.xxx....

不过, 我上面写的那些你可以不用看, 现在一些通用程序使用分表是因为MySQL 5.1之前不支持分区, 至少你这个场景合适的是分区, 而非分表.

分表的数量会影响采用哪种方案。

从数据库层面来解决:

  • Mysql MERGE 引擎,@samoay 回答了
  • Mysql 表分区,5.1 版本后才支持,@Yj.Lee 回答了

从 SQL 层面来解决:

标准的用法就是 UNION了,@Yj.Lee 也回答了。

题主的分表数量可能比较多,那么 SQL 方式可能不太适用,可以考虑数据库层面的解决方法。或者从应用层来解决,多个查询、拼合结果;或者按 type 建简单的冗余表/缓存进行查询;或者采用任何方法但缓存查询结果等等。

以上都是分表的情况,如果是分库,数据库层面的方法基本上也不可行了,只能从应用上来解决。

如果数据量就是不拆不行,那么就直接拆干净。MySQL拆了以后就把它看作key value的持久层,不要想跨表跨库。(除非是统计/数据仓库之类的需求,一个查询可以不计成本做很久堵死整台服务器也无妨)

慎重选择拆分依据,比如type很均衡,应用场景又基本总是已知type,不妨直接按type拆分。
其次如果就是大量数据,且就有和拆分纬度不同的检索需求,那么你需要的是搭搜索服务,或者说索引服务。这方面我没有实际经验,只能给一串关键词了 lucene solr sphinx elasticsearch

这中应该是按照路由去存储表格的吧,但是如果想查询一个不是按路由键的时候,就需要全库查询了吧

目测你需要的是分区而不是分表。

分表最好的办法就是merge引擎,适用于大部分场景,当然要看具体的数据量。

mysql部署中依次参考:

分区
分表
sharding

不过mysql千万级表索引设计得当,查询速度很快的。不知道为啥要分表。

建个视图吧

分表架构时最好是有中间件做支持,用来屏蔽这些细节。

如果是分库分表,有成熟的方案不?

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
What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

PHP Email Security: Best Practices for Sending EmailsPHP Email Security: Best Practices for Sending EmailsMay 08, 2025 am 12:16 AM

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

How do you optimize PHP applications for performance?How do you optimize PHP applications for performance?May 08, 2025 am 12:08 AM

TooptimizePHPapplicationsforperformance,usecaching,databaseoptimization,opcodecaching,andserverconfiguration.1)ImplementcachingwithAPCutoreducedatafetchtimes.2)Optimizedatabasesbyindexing,balancingreadandwriteoperations.3)EnableOPcachetoavoidrecompil

What is dependency injection in PHP?What is dependency injection in PHP?May 07, 2025 pm 03:09 PM

DependencyinjectioninPHPisadesignpatternthatenhancesflexibility,testability,andmaintainabilitybyprovidingexternaldependenciestoclasses.Itallowsforloosecoupling,easiertestingthroughmocking,andmodulardesign,butrequirescarefulstructuringtoavoidover-inje

Best PHP Performance Optimization TechniquesBest PHP Performance Optimization TechniquesMay 07, 2025 pm 03:05 PM

PHP performance optimization can be achieved through the following steps: 1) use require_once or include_once on the top of the script to reduce the number of file loads; 2) use preprocessing statements and batch processing to reduce the number of database queries; 3) configure OPcache for opcode cache; 4) enable and configure PHP-FPM optimization process management; 5) use CDN to distribute static resources; 6) use Xdebug or Blackfire for code performance analysis; 7) select efficient data structures such as arrays; 8) write modular code for optimization execution.

PHP Performance Optimization: Using Opcode CachingPHP Performance Optimization: Using Opcode CachingMay 07, 2025 pm 02:49 PM

OpcodecachingsignificantlyimprovesPHPperformancebycachingcompiledcode,reducingserverloadandresponsetimes.1)ItstorescompiledPHPcodeinmemory,bypassingparsingandcompiling.2)UseOPcachebysettingparametersinphp.ini,likememoryconsumptionandscriptlimits.3)Ad

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 Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.