Home php教程 php手册 php多数据库支持的应用程序设计第1/2页

php多数据库支持的应用程序设计第1/2页

Jun 13, 2016 pm 12:27 PM
php session application I support database of programming

所以我想在主从数据库设计上,应该将所有会话相关表进行特殊对待。即:所有的会话数据表都可以更新和查询,当一个用户访问站点的时候,即将此用户绑定到指定数据库,所有会话访问和查询操作都对此数据库进行。会话表不做同步,其他非会话类更新也从主数据库更新。这样做其实也逃脱不了会话更新时候的数据库切换,所以如果不想麻烦,还是将会话存放在文本中进行的好。
  分数据库设计,将可能从压力性能上会提升几个档次,当然单次执行效率不会比单数据库来的高的,毕竟存在着数据库切换的效率问题。分库以及主从数据库搭配是可以比较好改善数据库并发瓶颈的方案。原则:大数据量,分库;大访问量,主从。很多时候,都是这两者并行(本文不讨论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 of this Website
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

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

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 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.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 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 Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 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.

PHP's Impact: Web Development and Beyond PHP's Impact: Web Development and Beyond Apr 18, 2025 am 12:10 AM

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

PHP vs. Python: Use Cases and Applications PHP vs. Python: Use Cases and Applications Apr 17, 2025 am 12:23 AM

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

The Continued Use of PHP: Reasons for Its Endurance The Continued Use of PHP: Reasons for Its Endurance Apr 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.

Oracle's Role in the Business World Oracle's Role in the Business World Apr 23, 2025 am 12:01 AM

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

MySQL: Structured Data and Relational Databases MySQL: Structured Data and Relational Databases Apr 18, 2025 am 12:22 AM

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

See all articles