![mdb使用](https://img-blog.csdnimg.cn/img_convert/3194da624917213980e6047ccb7693c7.png)
mdb使用
This is a follow-up to Lars' comment about the PEAR book. In the MDB2 chapter I showed an example how you can create custom debug handlers in MDB2 and then gave a suggestion about a useful application of this functionality for performance tuning. Basically the idea is that your custom debug handler collects all queries that are executed during the life of a given script. Then, once the script finishes execution, the debug handler reports the stats that it has collected. In the book, the example is how you count the number of times each distinct query is executed, this way you can spot problems caused by the OO abstraction. For example, say you have a come class Users
that has a method loadUser()
, which abstracts the database work. While debugging with the custom error handler, you might figure out that without noticing, you're calling this method in a few places and it makes the same repeating query(queries) over and over again. So you can now optimize/cache results and so on.
这是Lars对PEAR书的评论的后续。 在MDB2章节中,我展示了一个示例,您可以如何在MDB2中创建自定义调试处理程序,然后就如何使用此功能进行性能优化提出了建议。 基本上,想法是您的自定义调试处理程序将收集在给定脚本有效期内执行的所有查询。 然后,脚本完成执行后,调试处理程序将报告其已收集的统计信息。 在本书中,该示例说明了如何计算每个不同查询的执行次数,从而可以发现由OO抽象引起的问题。 例如,假设您有一个loadUser()
类Users
,该类具有方法loadUser()
,该方法抽象化数据库工作。 在使用自定义错误处理程序进行调试时,您可能会发现,没有注意到,您在几个地方调用了该方法,并且一次又一次地重复执行相同的重复查询。 因此,您现在可以优化/缓存结果等等。
The suggestion I made in the book is that in addition to counting, you might want to try executing all SELECTs again, just to see how much time they take and you can execute them once again, prepending them with EXPLAIN to get some details on possible room for improvement.
我在书中提出的建议是,除了计算之外,您可能还想尝试再次执行所有SELECT,以查看它们花费了多少时间,并且可以再次执行它们,并在它们前面加上EXPLAIN以获得可能的详细信息。有改进的空间。
Now here's one solution to this suggestion. What you can see in this script is:
现在,这是此建议的一种解决方案。 您可以在此脚本中看到:
- Setting up MDB2 设置MDB2
- Declaring a custom debug handler class 声明自定义调试处理程序类
- "Attaching" it to the MDB2 instance将其“附加”到MDB2实例
- Registering it for execution at the end of each script在每个脚本末尾注册执行代码
- Testing it (creating a DB, table, some queries)测试它(创建数据库,表,一些查询)
I hope you like it and try it out.
希望您喜欢并尝试一下。
Here's the result of executing this script, you can see what you get back.
有待改进 (Room for improvement)
Obviously, the method dumpInfo()
can be improved. First, it can print out a nice table, instead of lazy print_r()
. Then, it can include some logic, my idea is for it to "understand" the EXPLAIN results and to give you a hint by using colors, for exampe green background for queries that are OK, yellow for warnings and red for queries that definitelly need some work. Could be nice, no?
显然,可以改进方法dumpInfo()
。 首先,它可以打印出一个漂亮的表,而不是懒惰的print_r()
。 然后,它可以包含一些逻辑,我的想法是让它“理解” EXPLAIN结果,并通过使用颜色为您提供提示,例如,绿色背景表示确定的查询,黄色表示警告,红色表示绝对需要的查询一些工作。 会很好,不是吗?
测试脚本 (Test script)
Kinda longish, but I hope I added enough comments. I also hope I didn't introduce any syntax errors while formatting it for posting here, chopping long lines, etc.
Kinda渴望,但我希望我添加足够的评论。 我也希望我在格式化它以便在此处发布,砍掉长行等时,不会引入任何语法错误。
|
|
Tell your friends about this post on Facebook and Twitter
在Facebook和Twitter上告诉您的朋友有关此帖子的信息
mdb使用