在一开始接触PHP接触MYSQL的时候就听不少人说:“MySQL就跑跑一天几十万IP的小站还可以,要是几百万IP就不行了”,原话不记得了,大体就是这个意思。一直也没有好的机会去验证这个说法,一是从没有接手过这么大流量的网站,二是平时工作也比较忙,懒得去管这
在一开始接触PHP接触MYSQL的时候就听不少人说:“MySQL就跑跑一天几十万IP的小站还可以,要是几百万IP就不行了”,原话不记得了,大体就是这个意思。一直也没有好的机会去验证这个说法,一是从没有接手过这么大流量的网站,二是平时工作也比较忙,懒得去管这些,反正现在用不着,抱着这个想法把这个问题一直留到了最近,才把这个问题搞明白。
就在前几天公司旗下一网站(由于这是公司的商业内容我就不说是那个网站了)以下简称A站,这A站在年后流量猛增从一天的七八十万猛跑到了好几百万的IP,一天下来接近一千万的PV让整个服务器在高压下超负荷的工作着,时不时的服务就出现宕机。
最首先反映出情况的是数据统计,一天下来一个数据也没有统计上,原来是MySQL停止工作了。
本文就围绕这个问题来讲讲我们公司几个技术人员的解决方案。
1. MySQL服务器集群
由于是小公司在资金和成本上都有所限制,而且在技术上也没有几个技术员接触过,所以这个方法自然就让大伙否决了。
这里说说我个人的理解!做集群不但添加资费的开销,而且在技术上也有很大的挑战,对于我们公司目前的情况是不大现实的。集群无非就是把一台服务器的压力转接到两台或是多台服务器上,我是这么理解的,也许我理解有误,还请大家指教。
2. 分而治之
这个方法和集群差不多,不过是把统计的代码放在不同的服务器上跑,由于公司有不少配置低的服务器跑几万到几十万IP还是没有问题的,我们可以把几百万流量分成十来个几十万的量分而统计。
优点:充分的利用了现在的资源,解决了目前的问题。
缺点:这样的方法不是长久之计,迟早还是会出问题的。而且在统计数据的时候比较麻烦。
3. 统计代码的修改
由于之前采用的是在插入数据之前加以判断,这个IP是否存在,来路等的处理,无形中增加了服务器的压力,所以大伙把统计代码改成来一个就插入数据库,不管三七二十一等以后在处理。
这个方法基本上把当天的数据保留下来了,可是在处理的时候由于数据量的庞大,来来回回还是把服务器跑死了,而且在插入的时候由于当时设计数据结构的时候留有的索引,也大大的消耗了不少的服务器资源。
那么把索引去掉到最后处理的时候又是老慢的,得不偿失。
4. 统计方式的修改
最后这一个方法,效果非常的明显。那是什么方法呢!
这里就主要介绍这个方法:
A、 保留原用的数据结构不变,并把所有的数据按一定的结构存入文件
结构:可以是xml,json,也可以是你自己想的任何有规律的数据排放。
例如:
1 221.2.70.52,http://www.baidu.com,windowxp\r\n
2 221.2.70.52,http://www.baidu.com,windowxp\r\n
写入文件:fopen,fwrite??no 这里介绍一个非常好用的技巧,也许大家都知道,但是像我这样的菜鸟大概都不知道,那就是用error_log,这不是写错误日志的吗?对就是他,非常方便。
格式:
1 error_log("内容", 3, "/date.dat"); |
这里我就不说他的具体用了,不明白的朋友可以查一下手册。
B、数据文件的命名
为什么这里要讲文件的命名呢?如果就一味的把数据的写入文件不作任何的处理那么他和直接插入数据库有多大的区别呢?那么我们所作的一切都是无用功了。
首先是在时间的利用上:date(‘YmdH’)得到的值是如:2008121112这有什么好入,这样一来这个数据就是一个小时一个文件必免了文件过大,而且不用去判断自动生成。
IP的应用:由于在很多数据上都是一个IP操作的,所以把相同IP的数据放在一个文件里在后面的处理就非常方便处理。请看后面的介绍,这里我们取IP成三位为文件名字的一部分。
C、数据的导入处理
通过以上两位的操作当天的数据会一个不少的保留下来,接下来怎么处理其实并不是很重要了。但是这里还是讲讲我的想法。
入库前处理:
前面讲到把IP三位相同的放一个文件就是为了入库前到它们进行处理,首先可以用最笨的方法把数据拆成N个数据。在进行重复数据的删除。
如果一个IP浏览多页那么PV在这里就可以得到统计,并把访问的页面进行处理,组成新的数据。
导入方法:
这里要介绍一下数据的导入方法
1 $sql="LOAD DATA INFILE '".$file."' INTO TABLE `test` FIELDS TERMINATED BY ', ' LINES TERMINATED BY '\r\n'(<br>2 <br>3 `time` , `md5`<br>4 <br>5 )"; |
就这一个语句,在导入几十万数据的时候可以说是十分快速的,用法我这里也不过多介绍了,大家有不明白的可以查手册,所以说解决数据库的瓶颈的办法不定要加设备,也不一定要换数据库,只要换一个思路就能解决不少问题。

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

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.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

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.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools