Home Backend Development PHP Tutorial 匹配csdn用户数据库与官方用户的重合度并将重叠部分的用户筛选出来_php实例

匹配csdn用户数据库与官方用户的重合度并将重叠部分的用户筛选出来_php实例

Jun 07, 2016 pm 05:25 PM

过程:
1、获取csdn的用户数据库导入本地
试用editplus打开提示内存不足,没找到办法,同事的linux下查看了一下,基本的格式如下:
用户名 # 密码 # 邮箱
用户名 # 密码 # 邮箱
相应数据结构:

复制代码 代码如下:

CREATE TABLE IF NOT EXISTS `csdn_userdb` (
  `id` int(10) NOT NULL auto_increment,
  `username` varchar(50) character set gbk NOT NULL,
   `password` varchar(50) character set gbk NOT NULL,
  `email` varchar(50) character set gbk NOT NULL,
   PRIMARY KEY (`id`),
  KEY `username` (`username`),
  KEY `email` (`email`)
  ) ENGINE=MyISAM DEFAULT CHARSET=gbk AUTO_INCREMENT=1 ;

一直怀疑fopen打开文件是写入缓存的,但是实践证明速度很快,应该是没有写入缓存,以下为导入数据的代码
复制代码 代码如下:

$link = mysql_connect('localhost', 'root', 'admin', true);
mysql_select_db('csdn',$link);
$handle = fopen("C:\Users\zhudong\Desktop\www.csdn.net.sql", "r");
while (!feof($handle)){
$i++;
$buffer = fgets($handle);
list($u,$p,$e) = explode(" # ",$buffer);
mysql_query("INSERT INTO csdn_userdb(username,password,email) VALUES ('$u','$p','$e')",$link);
if ($i%1000 == 0) echo $i."\n";
}
fclose($handle);
?>

以上代码效率非常差,故做修改后代码如下:
复制代码 代码如下:

$link = mysql_connect('localhost', 'root', 'admin', true);
mysql_select_db('csdn',$link);
$handle = fopen("C:\Users\zhudong\Desktop\www.csdn.net.sql", "r");
$perpage = 50;
while (!feof($handle)){
$i++;
$buffer = fgets($handle);
list($u,$p,$e) = explode(" # ",$buffer);
$insertValue[] = "('$u','$p','$e')";
if ($i% $perpage == 0){ $perpage == 0){
$instrtValueString = implode(',',$insertValue);
mysql_query("INSERT INTO csdn_userdb(username,password,email) VALUES $instrtValueString",$link);
echo $i."\n";
$insertValue = array();
}
}
fclose($handle);

为了搞清楚有那些因素对导入数据的效率产生影响,故根据不同的设置进行了测试
CSDN用户总数据 6428600
当$perpage=500;导入后数据:5,902,000;数据丢失526600 丢失率:8%;数据表引擎:MyISAM;索引:有;总耗时 :15分钟
当$perpage=200,导入后数据总数:6,210,200;数据丢失:218400;丢失率:3.3%;数据表引擎: MYISAM ;索引:有;总耗时:30分钟
当$perpage=200,导入后数据总数:6,210,200;数据丢失:218400;丢失率:3.3%;数据表引擎:INNODB;索引:有;总耗时:65分钟
当$perpage=200,导入后数据总数:6,210,200;数据丢失:218400;丢失率:3.3%;数据表引擎:MYISAM;索引:无;总耗时:14分钟(数据导入完毕后单独再建索引)
当$perpage=50,导入后数据总数:6,371,200;数据丢失:57400,丢失率:0.8%;数据表引擎:MYISAM;索引:无:总耗时:20分钟
根据以上情况总结如下:
1、先导入数据后加索引的效率要比先加索引后导入数据的高一倍
2、InnoDB 在单进程数据插入上的效率要比MYISAM低很多
3、当perpage=50的情况下数据丢失率在1%以下
复制代码 代码如下:

因为通过浏览器执行会有超时的问题,而且效率地下,故通过命令行方式运行,此过程中遇到一点小麻烦耽搁了不少时间
起初我执行如下代码:
php.exe E:\usr\www\importcsdndb.php
但是一直报错:call to undefined function mysql_connect
折腾发现没有载入php.ini
正确代码为:
php.exe -c E:/usr/local/apache2/php.ini importcsdndb.php

2、导入需要匹配的用户数据数据至本地
命令行进入msyql(不会的自己百度)
然后执行:mysql>source C:/Users/zhudong/Desktop/userdb.sql
3、对比筛选用户
对比程序写好了,切记在命令行下运行:
复制代码 代码如下:

$link = mysql_connect('localhost', 'root', 'admin', true);
mysql_select_db('csdn',$link);
$handle_username = fopen("E:/records_username.txt","a");
//$handle_email = fopen("E:/records_email.txt","a");
$username_num = $email_num = $uid = 0;
while ($uid$nextuid=$uid+10000;
$query = mysql_query("SELECT * FROM pw_members WHERE uid>'$uid' AND uidwhile ($rt = mysql_fetch_array($query,MYSQL_ASSOC)) {
$username = $rt['username'];
$email = $rt['email'];
$query2 = mysql_query("SELECT * FROM scdn_userdb WHERE username='$username' OR email='$email'");
while ($rt2 = mysql_fetch_array($query2,MYSQL_ASSOC)) {
if ($rt['password'] = md5($rt2['password'])) {
if ($rt2['username'] == $username) {
$username_num++;
fwrite($handle_username,'OWN:'.$rt['uid'].'|'.$rt['username'].'|'.$rt['password'].'|'.$rt['email'].' CSDN:'.$rt2['username'].'|'.$rt2['password'].'|'.$rt2['email']."\r\n");
echo 'username_num='.$username_num."\r\n";
continue;
}
/*
if ($rt2['email'] == $email) {
$email_num++;
fwrite($handle_email,'OWN:'.$rt['uid'].'|'.$rt['username'].'|'.$rt['password'].'|'.$rt['email'].' CSDN:'.$rt2['username'].'|'.$rt2['password'].'|'.$rt2['email']."\r\n");
echo 'email_num='.$email_num."\r\n";
}
*/
}
}
mysql_free_result($query2);
}
$uid = $nextuid;
}
?>

您看到的以上的代码是非常蹩脚的,因为其效率特别低 ,几百万的数据,要跑10多个小时,怎么能忘记连表查询这么基本的东西呢,以下为修正后的方法
复制代码 代码如下:

$link = mysql_connect('localhost', 'root', 'admin', true);
mysql_select_db('csdn',$link);
$handle_username = fopen("E:/records_username.txt","a");
while($uid$nextuid= $uid+10000;
$query = mysql_query("SELECT m.uid,m.username,m.password,m.email,u.password as csdn_password,u.email as csdn_email FROM own_members m LEFT JOIN csdn_userdb u USING(username) WHERE m.uid>'$uid' AND m.uidwhile ($rt = mysql_fetch_array($query,MYSQL_ASSOC)) {
if ($rt['password'] == md5($rt['csdn_password'])) {
$username_num++;
fwrite($handle_username,'OWN:'.$rt['uid'].'|'.$rt['username'].'|'.$rt['password'].'|'.$rt['email'].' CSDN:'.$rt['username'].'|'.$rt['csdn_password'].'|'.$rt['csdn_email']."\r\n");
echo 'username_num='.$username_num."\r\n";
}
}
$uid = $nextuid;
echo 'uid='.$uid;
}
?>

总对比时间25分钟,相比较之前10多个小时的执行真是大有提升
总重名用户:34175
占总会员比例:1.7%
1.7%的重名用户还是挺严重的,希望本文对各位站长对比出本站的用户有所帮助
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 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)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

What are Enumerations (Enums) in PHP 8.1? What are Enumerations (Enums) in PHP 8.1? Apr 03, 2025 am 12:05 AM

The enumeration function in PHP8.1 enhances the clarity and type safety of the code by defining named constants. 1) Enumerations can be integers, strings or objects, improving code readability and type safety. 2) Enumeration is based on class and supports object-oriented features such as traversal and reflection. 3) Enumeration can be used for comparison and assignment to ensure type safety. 4) Enumeration supports adding methods to implement complex logic. 5) Strict type checking and error handling can avoid common errors. 6) Enumeration reduces magic value and improves maintainability, but pay attention to performance optimization.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

See all articles