有一朋友现在遇到一个问题,有一目录存放的是图片文件,目录结构为三层,/uploads/avator/140320/*.jpg
也就是avator目录下有很多以目期命名的子目录,大概几百了。整个avator目录大概有8W左右的文件,其中有一部分文件
是无用了的垃圾文件,他想清掉。
这个目录中的大部分文件有一个表中的img_url字段记录了完全路径,即 路径+文件名, 比如:/upload/avator/140320/bid001.jpg。
他现在的作法是,遍历目录拿到所有文件名,然后查表 select 查 img_url like '%K',如果返回记录数等于0,就把这个文件从目录中删除掉。
表中的记录数有6W多条。
等于说有2W左右的文件是可删除的。
但是从运行来看,遍历目录很快,但是like时太慢了。。有没有更好更快的办法呀。
img_url字段建了索引
回复讨论(解决方案)
既然表中存放有完整的路径,那为什么还 like 呢?直接 = 不就快些
读取目录 glob('/uploads/avator/*/*.jpg') 放到一个临时表中
用这个临时表左连接数据表,右表为 null 的就是该删除的文件
img的src=“”时, 会导致有些浏览器显示一个x。 所以是不推荐将img的src设置为空的,。比较好的方案是remove image这个元素节点。
%开头的like是不启用索引的,像斑竹说的直接=
要么换个角度,遍历数据库,将有用的图片剪切到一个新文件夹,然后你懂的
谢谢各位指点

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

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


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

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Notepad++7.3.1
Easy-to-use and free code editor

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
