简体   繁体   English

使用PHP循环执行SQL查询

[英]SQL Queries in loops with PHP

So I have recently taken over maintenance of an in-house Content Management System, and database optimization is not really my area of expertise. 因此,我最近接手了内部内容管理系统的维护,而数据库优化并不是我真正的专长。

Anyway a couple of things fell out to my eye when I was looking over the code. 无论如何,当我查看代码时,有几件事掉到了我的眼前。

The php code is a little bit "spaghetti with meatballs" with little to no comments. php代码有点“意大利面加肉丸”,几乎没有注释。

But the biggest thing: As far as I can tell, the original programmer decided to forgo table joins entirely in the database code (both implicit and explicit). 但是最大的事情是:据我所知,原始程序员决定放弃表联接完全包含在数据库代码中(隐式和显式)。

For example here is the process to display a page column: 例如,以下是显示页面列的过程:

Fetch element list from the database For each element call a subroutine to check display permissions and if successful, call another subroutine to fetch the element's html data. 从数据库中获取元素列表对于每个元素,调用一个子例程以检查显示权限,如果成功,则调用另一个子例程以获取该元素的html数据。

Each of the subroutines effectively calls a separate query for each element. 每个子例程有效地为每个元素调用一个单独的查询。 And the permissions step, I believe, involves querying two separate tables. 我认为,权限步骤涉及查询两个单独的表。

Performance isn't really a problem at the moment, and I wasn't asked to look into this. 目前,性能并不是真正的问题,也没有要求我对此进行研究。 Although the page requests are a bit slow in my opinion. 虽然页面请求在我看来有点慢。

Is it worth trying to rewrite the SQL stuff? 值得尝试重写SQL东西吗? I am thinking that the increase in maintainability will be worth it in the end, and that it will make things easier for me should scalability become an issue in the future. 我认为提高可维护性最终是值得的,并且如果将来可伸缩性成为一个问题,它将使我更轻松。

Or is it not really as bad as I think? 还是真的不像我想的那么糟糕? Maybe I am just overreacting. 也许我只是反应过度。 An expert opinion would be appreciated. 专家的意见将不胜感激。

If its about making your job in the future easier, I would start refactoring the spaghetti code before fixing the queries. 如果要使将来的工作变得更轻松,我将在修复查询之前开始重构意大利面条代码。 Once you have a nice design, it should also be more straight forward how to integrate (and maybe even eleminate) some of those manual joins. 一旦有了一个不错的设计,它也应该更直接地说明如何集成(甚至消除)一些手动联接。

Refactoring is an important part of development process. 重构是开发过程的重要组成部分。 Ignoring this fact means more problems in the future. 忽略这一事实意味着将来会有更多问题。 The part of the problem is that not many managers understand the importance of continuous refactoring. 问题的部分在于,没有多少管理者了解连续重构的重要性。
I'd recommend you to read "Refactoring to Patterns" by Joshua Kerievsky that has many good examples of how to safely change existing code by implementing new design approaches. 我建议您阅读Joshua Kerievsky的“重构为模式”,其中有许多很好的示例,说明了如何通过实施新的设计方法来安全地更改现有代码。

As of your question about SQL queries, it may or may not be the first thing you need to change. 关于SQL查询的问题,它可能不是您需要更改的第一件事。

I think it would be worth rewriting while you have the time to do so. 我认为在您有时间的时候值得重写。 You don't want to put it off until it becomes a problem. 在出现问题之前,您不想拖延它。 I say, let the database do what it does best. 我说,让数据库尽其所能。 In this case, table joins would certainly perform better than multiple queries in a loop. 在这种情况下,表联接肯定会比循环中的多个查询更好。

Your question is a bit too generic to give a good answer to and you haven't provided enough information to make educated guesses either, for your particular situtaion. 您的问题过于笼统,无法给出一个很好的答案,并且您也没有针对您的特定情况提供足够的信息来做出有根据的猜测。

Things to take in to account: 需要考虑的事项:

Will the system grow over time? 系统会随着时间增长吗? And how? 如何? If there will be more users online, more preassure on the system - then there's cause for alarm. 如果会有更多的用户在线,则对系统的确定程度更高-则可能会引起警报。 Systems with bad design does not scale well. 设计不良的系统无法很好地扩展。

Bad code is one thing, but bad database- and general system design is worse. 不好的代码是一回事,但是不好的数据库和一般的系统设计则更糟。 I think the key might be your comment about "next few years". 我认为关键可能是您对“未来几年”的评论。 If you intend to stick with the sytem for that long - then a serious look at the basics is a good idea. 如果您打算长期使用该系统-那么认真看一下基础知识是个好主意。 If your system might be up for replacement by something else (inhouse CMS's tend to be replaced) then you can patch along while shopping for the right thing. 如果您的系统可能要用其他东西代替(内部CMS倾向于替换),那么您可以在购物时购买合适的东西。

But as your question stands - rewrite some of the questions, make sure your DB is normalized and refactor your code. 但是就您的问题而言,请重写一些问题,确保您的数据库已规范化并重构了代码。 Tell your boss it's the right thing to do if you're going to stick with the system. 告诉老板,如果您要坚持使用系统,这是正确的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM