search
HomeBackend DevelopmentPHP Tutorial文章表500万条数据,每天会有10万条数据更新,从更新的10万条中随机选3000条做数据研究,如果做到高效?

题目是一道面试题
我的想法是另起一张表,存放今天更新的10万条都有哪些;
我只想到这个第一步,接下来该怎么做我还不知道怎么去实现;
假设按我这样的思路,我就算知道了每天更新的是哪10万条数据,那我还是得去500万条中找出3000条数据哦
不知道各位兄弟,有啥好的想法呢?

回复内容:

题目是一道面试题
我的想法是另起一张表,存放今天更新的10万条都有哪些;
我只想到这个第一步,接下来该怎么做我还不知道怎么去实现;
假设按我这样的思路,我就算知道了每天更新的是哪10万条数据,那我还是得去500万条中找出3000条数据哦
不知道各位兄弟,有啥好的想法呢?

10W中的3000条,概率是3%

那么只要在保存文章时,按照3%的概率,把本次更新文章保存到缓存中

这种缓存用redis的set类型最好,set类型不会保存重复的元素,所以文章反复更新也不会在列表里面产生多个结果

key的格式可以用"analyze:list:(Y-m-d)"

然后这个缓存可以设置为48小时过期,如果有需要的话,每天可以拿前一天的缓存归档到数据库

考虑到随机概率的误差,可以把3%放大到5%,最后肯定会记录得超过3000,但是也不会超太多,反正最后只拿3000条来用就行了

把每次更新都记录起来的话,无论是记录到缓存还是数据库,其实大部分的记录是没用的,不如按照概率先过滤一遍

其实记录每条文章的update_time也可以,我觉得where update_time >= ? and update_time

优点:
1、没有update_time字段也能玩,对现有表结构无要求,给生产环境的数据库加字段是件麻烦事
2、万一生产环境的数据库负载比较高,order by random()查询导致数据库卡死也不好,这样的话,最好是读写分离架构,在只读库上查询才行,产生了架构要求,我这个设计完全是个旁路记录,除了redis之外没要求
3、需要多少才记多少,额外IO少

一些粗陋的想法,仅供参考
分区
500万条,为了方便。根据数据的更新时间进行数据库分区(没用过mysql分区的看这个,在文章后面讲了),
比如说按照月份,我假设你这500万条数据是一年的,那么分成12份,每个区大约算42万条记录
这样,当使用更新时间进行搜索的时候,mysql就会根据你的更新时间 去选择分区,
也就是被搜索的数据是在这42万条里面去找(这肯定要比你在500万里面快多了,当然你要是按照天来分,那会更快)

加缓存
这没啥,就是你每天写入mysql的时候取3000条数据写入redis或者mongodb里面,做研究就不从mysql里面读了。用php从缓存里面读

多进程
你说的要做研究嘛,我假设你的研究算法很复杂。你去学学swoole,开三个进程,一个进程处理1000个数据,最后汇总结果

  1. 取出当日更新的10万

  2. id放入一个数组在数组中随机取出3000个id

  3. 用select in读取指定的3000条记录

<code>SELECT id FROM table WHERE date_refresh = 20120329

SELECT * FROM table WHERE id IN (id_0, id_1, id_2, ..., id_2999)
</code>

https://www.zhihu.com/question/20151242

  • 首先,我会使用缓存的方式,将每天更新的数据的主键 记录下来。

  • 从缓存中,随机获取3000主键

  • 拿着这3000 个主键,使用 IN 查询,获取对应的数据。

浅陋分析,勿笑。

1.获取id区间

<code>select max(id) as max_id, min(id) as min_id 
from (
   select id from article_tb where update_time >= '2016-02-26 00:00:00'
) 
</code>

update_time有索引,id为自增长id
2.随机获取

<code>select * 
from article_tb 
where id >= min_id and id </code>

查询3000次

<code>// STEP 1 : 获取当天文章ID区间
// maxId -> select max(id) from news where 当天时间限定
// minId -> select min(id) from news where 当天时间限定


// STEP 2 : 取得随机ID
// 因为你一天有10万数据,数据总量有很高
// 所以避免使用MYSQL中的随机

$minId = 5000000;
$maxId = 5100000;
$i = 0;
$resultIds = [];
while(true){
    $randId = rand($minId,$maxId);
    if(in_array($randId, $resultIds)){
        continue;
    }
    
    // 查询验证
    // 根据你的需要验证数据是否是审核的呀,是否是正常数据呀
    // 如果正常就载入到结果数组中。
    $resultIds[] = $randId;
    $i++;
    
    if($i==3000){
        break;
    }
}

// 到这里结果已经有了
// 可以储存到结果集用其他方式分页进行研究或者浏览。</code>
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
PHP's Current Status: A Look at Web Development TrendsPHP's Current Status: A Look at Web Development TrendsApr 13, 2025 am 12:20 AM

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP vs. Other Languages: A ComparisonPHP vs. Other Languages: A ComparisonApr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP vs. Python: Core Features and FunctionalityPHP vs. Python: Core Features and FunctionalityApr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP: A Key Language for Web DevelopmentPHP: A Key Language for Web DevelopmentApr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP: The Foundation of Many WebsitesPHP: The Foundation of Many WebsitesApr 13, 2025 am 12:07 AM

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

Beyond the Hype: Assessing PHP's Role TodayBeyond the Hype: Assessing PHP's Role TodayApr 12, 2025 am 12:17 AM

PHP remains a powerful and widely used tool in modern programming, especially in the field of web development. 1) PHP is easy to use and seamlessly integrated with databases, and is the first choice for many developers. 2) It supports dynamic content generation and object-oriented programming, suitable for quickly creating and maintaining websites. 3) PHP's performance can be improved by caching and optimizing database queries, and its extensive community and rich ecosystem make it still important in today's technology stack.

What are Weak References in PHP and when are they useful?What are Weak References in PHP and when are they useful?Apr 12, 2025 am 12:13 AM

In PHP, weak references are implemented through the WeakReference class and will not prevent the garbage collector from reclaiming objects. Weak references are suitable for scenarios such as caching systems and event listeners. It should be noted that it cannot guarantee the survival of objects and that garbage collection may be delayed.

Explain the __invoke magic method in PHP.Explain the __invoke magic method in PHP.Apr 12, 2025 am 12:07 AM

The \_\_invoke method allows objects to be called like functions. 1. Define the \_\_invoke method so that the object can be called. 2. When using the $obj(...) syntax, PHP will execute the \_\_invoke method. 3. Suitable for scenarios such as logging and calculator, improving code flexibility and readability.

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),