search
HomeBackend DevelopmentPHP Tutorialphp多数据库支持的应用程序设计第1/2页_php技巧

所以我想在主从数据库设计上,应该将所有会话相关表进行特殊对待。即:所有的会话数据表都可以更新和查询,当一个用户访问站点的时候,即将此用户绑定到指定数据库,所有会话访问和查询操作都对此数据库进行。会话表不做同步,其他非会话类更新也从主数据库更新。这样做其实也逃脱不了会话更新时候的数据库切换,所以如果不想麻烦,还是将会话存放在文本中进行的好。
  分数据库设计,将可能从压力性能上会提升几个档次,当然单次执行效率不会比单数据库来的高的,毕竟存在着数据库切换的效率问题。分库以及主从数据库搭配是可以比较好改善数据库并发瓶颈的方案。原则:大数据量,分库;大访问量,主从。很多时候,都是这两者并行(本文不讨论cache)。
  我想,如果要实现分库以及主从关系,那么数据库服务器数量将是非常可观,在应用程序中随时切换到某一台服务器,将是非常头痛的问题,配置更换,变量名称,是不是会有一大堆呢?如何寻找更好的解决方案将是本文谈论的话题。
  首先是分库使得数据库颇多的问题。什么情况下分库?或许有些人还搞不明白为什么要分库,我就简要说一下自己的经验猜测。比如一个博客程序,一般设计是将日志存放在一张日志表中。假设是一个多用户博客,那么将会关联一个uid,如果数据量不大,这样设计是没有问题的,但是当日志量巨大,一天有几十万条日志记录录入的时候,而且访问量也比较可观的时候,我想不可能每个用户来访问日志列表,都去从这包含几千万条日志记录的数据表中去找那么几条,效率可见一斑。这个时候就该考虑到分库的问题。如何分?有一个很简单的分表方法,即,根据uid段,将日志记录在各个数据库中,当然,这个分布还是需要根据以往统计结果做出调整的,因为用户日志分布肯定不是均匀的。设置好uid段,然后根据uid索引到指定数据库配置,创建一个数据库对象即可。配置信息可能如下: 


$configs['db_info']['blog'][0] = array(
    'db_host' => '192.168.0.1',
    'db_name' => 'blog',
    'db_user' => 'root',
    'db_pass' => '',
);
$configs['db_info']['blog'][1] = array(
    'db_host' => '192.168.0.2',
    'db_name' => 'blog',
    'db_user' => 'root',
    'db_pass' => '',
);
$configs['db_info']['blog'][2] = array(
    'db_host' => '192.168.0.2',
    'db_name' => 'blog',
    'db_user' => 'root',
    'db_pass' => '',
);
//...还有很多


  至于选择哪一台服务器,只需要根据uid做一个简单的匹配就可以了。
  再谈到的就是主从数据库了。什么情况下使用主从数据库?比如某个名人博客,访问量相当的大,已经没有办法把他的数据再进行拆分了,这个时候就得考虑主从数据库服务器了,使用多台数据库来分流。这样要适用主从和分库,可能上面配置信息得稍微改动一下。



$configs['db_info']['blog'][0]['master'] = array(
    'db_host' => '192.168.0.1',
    'db_name' => 'blog',
    'db_user' => 'root',
    'db_pass' => '',
);
$configs['db_info']['blog'][0]['slave'][0] = array(
    'db_host' => '192.168.0.2',
    'db_name' => 'blog',
    'db_user' => 'root',
    'db_pass' => '',
);
$configs['db_info']['blog'][0]['slave'][1] = array(
    'db_host' => '192.168.0.3',
    'db_name' => 'blog',
    'db_user' => 'root',
    'db_pass' => '',
);
$configs['db_info']['blog'][1]['master'] = array(
    'db_host' => '192.168.0.4',
    'db_name' => 'blog',
    'db_user' => 'root',
    'db_pass' => '',
);
$configs['db_info']['blog'][1]['slave'][0] = array(
    'db_host' => '192.168.0.5',
    'db_name' => 'blog',
    'db_user' => 'root',
    'db_pass' => '',
);
$configs['db_info']['blog'][1]['slave'][1] = array(
    'db_host' => '192.168.0.6',
    'db_name' => 'blog',
    'db_user' => 'root',
    'db_pass' => '',
);
$configs['db_info']['session'][0]['master'] = array(
    'db_host' => '192.168.0.7',
    'db_name' => 'session',
    'db_user' => 'root',
    'db_pass' => '',
);
$configs['db_info']['session'][1]['master'] = array(
    'db_host' => '192.168.0.8',
    'db_name' => 'session',
    'db_user' => 'root',
    'db_pass' => '',
);


  写到这里,我想都应该知道如何分表和分配你的数据库了吧,接下去我就来说一下如何轻松的读取这样的配置信息,如何将这些配置融入你的数据库驱动中。


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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

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

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

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.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft