mdb使用_使用MDB2进行性能调优

mdb使用

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.

这是LarsPEAR书评论的后续。 在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渴望,但我希望我添加足够的评论。 我也希望我在格式化它以便在此处发布,砍掉长行等时,不会引入任何语法错误。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290

Tell your friends about this post on Facebook and Twitter

FacebookTwitter上告诉您的朋友有关此帖子的信息

翻译自: https://www.phpied.com/performance-tuning-with-mdb2/

mdb使用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值