search
HomeBackend DevelopmentPHP Tutorialphp内存消耗和,高并发处理,php排序。


做了一个sql查询,还通过了获取的数组下标来排序。但是前辈说会有大量访问的时候这样很消耗内存,网上看了下好多说的方法是做cache,有没有其他的方法处理呢?


回复讨论(解决方案)

你的语句,不会很消耗内存。activeNum是索引就可以了。
当然有cache是最好的。

你的语句,不会很消耗内存。activeNum是索引就可以了。
当然有cache是最好的。


要是有几十万上百万的数据,每次有人访问index就去取一次不会消耗资源吗?
如果加索引的话,加什么索引好呢?这个字段是随时变化的


你的语句,不会很消耗内存。activeNum是索引就可以了。
当然有cache是最好的。


要是有几十万上百万的数据,每次有人访问index就去取一次不会消耗资源吗?
如果加索引的话,加什么索引好呢?这个字段是随时变化的

如果数据值是唯一的   就加唯一索引   否则加普通索引
如果mysql表数据多  几百万的时候  那需要考虑分表

1、没看到 数组下标来排序 的代码
2、activeNum 上应有索引(普通索引即可)
3、你知道访问数据库和访问 cache 的区别吗?
将数据库的压力转嫁到 cache 就一定合适吗?
如果有有几十万上百万的数据,那么你的 cache 策略是什么?

1、没看到 数组下标来排序 的代码
2、activeNum 上应有索引(普通索引即可)
3、你知道访问数据库和访问 cache 的区别吗?
将数据库的压力转嫁到 cache 就一定合适吗?
如果有有几十万上百万的数据,那么你的 cache 策略是什么?



activeNum 是数字型,用索引也有效吗?一直以为 索引只用在where作用的字段上,排序的字段也可以用索引吗

排序也可以用到索引,不然数据量很大,不适用索引,你数据库不是崩溃。

1、没看到 数组下标来排序 的代码
2、activeNum 上应有索引(普通索引即可)
3、你知道访问数据库和访问 cache 的区别吗?
将数据库的压力转嫁到 cache 就一定合适吗?
如果有有几十万上百万的数据,那么你的 cache 策略是什么?


目前 activeNum这个字段没有加索引。

EXPLAIN 你的查询指令

MySQL 会给你有益的建议!而不是自己想当然的说

EXPLAIN 你的查询指令

MySQL 会给你有益的建议!而不是自己想当然的说



用命令查出来貌似不能用索引,我现在想到的一个办法是,新建一个rank表把用户的id,排名根据数组取下标放到rank表里面,然后在新表里面加一个uid等于0的字段,排名那里放我写入进去的时间,每次来取排名的时候先取uid=0的时间,对比当前时间,如果大于30分钟,那么就重新去active表读取一次排名放到rank表里面。不知道这方法可行么?

possible_key 是可被使用的索引,由于你没有对 activeNum 做索引,自然就没有啦
于是 Extra 列就有了 filesort,表示用了一个临时文件来对 activeNum 进行排序

possible_key 是可被使用的索引,由于你没有对 activeNum 做索引,自然就没有啦
于是 Extra 列就有了 filesort,表示用了一个临时文件来对 activeNum 进行排序


好吧,我再捣鼓捣鼓,php在linux里面有没有区分是否线程安全呢?每个用户访问一次比如我写了一个DB调用数据库,是只引用一次,所有用户都用的这一个,还是每个人都是独立的呢?

每个人都是独立的

你可以试试 单例模式,  把结果存在静态变量中试试

如果查询的量非常大,而数据又不是实时的可以做缓存来处理.但是如果数据又要是实时的 我觉得先找出瓶颈在什么地方,普通的索引等优化这些都是基础。

谢谢大家,用临时表做缓存解决了。

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 Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

Simple Guide: Sending Email with PHP ScriptSimple Guide: Sending Email with PHP ScriptMay 12, 2025 am 12:02 AM

PHPisusedforsendingemailsduetoitsbuilt-inmail()functionandsupportivelibrarieslikePHPMailerandSwiftMailer.1)Usethemail()functionforbasicemails,butithaslimitations.2)EmployPHPMailerforadvancedfeatureslikeHTMLemailsandattachments.3)Improvedeliverability

PHP Performance: Identifying and Fixing BottlenecksPHP Performance: Identifying and Fixing BottlenecksMay 11, 2025 am 12:13 AM

PHP performance bottlenecks can be solved through the following steps: 1) Use Xdebug or Blackfire for performance analysis to find out the problem; 2) Optimize database queries and use caches, such as APCu; 3) Use efficient functions such as array_filter to optimize array operations; 4) Configure OPcache for bytecode cache; 5) Optimize the front-end, such as reducing HTTP requests and optimizing pictures; 6) Continuously monitor and optimize performance. Through these methods, the performance of PHP applications can be significantly improved.

Dependency Injection for PHP: a quick summaryDependency Injection for PHP: a quick summaryMay 11, 2025 am 12:09 AM

DependencyInjection(DI)inPHPisadesignpatternthatmanagesandreducesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itallowspassingdependencieslikedatabaseconnectionstoclassesasparameters,facilitatingeasiertestingandscalability.

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 Article

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.