Developer Area/How to Review Code: Difference between revisions
From Mahara Wiki
No edit summary |
No edit summary |
||
Line 7: | Line 7: | ||
Kevin | Kevin | ||
:That's a typo. It should say "in a single query". As in, rather than doing a loop over the ids (1,2,3,5) in which you do "select owner from view where id=?" four times, it's more efficient to do one query, "select owner from view where id in (1,2,3,5)" and then loop over the results. Although | :That's a typo. It should say "in a single query". As in, rather than doing a loop over the ids (1,2,3,5) in which you do "select owner from view where id=?" four times, it's more efficient to do one query, "select owner from view where id in (1,2,3,5)" and then loop over the results. Although, I would caution against overdoing this, because if you put SQL queries directly into your page code, then you can wind up making it too tightly coupled to the current database structure. From a code reuse standpoint, it's better to call something like the "get('owner')" function of the View class in lib/view.php, and let that class decide how to query the database. Then, if we decided that we needed to refactor the "view" table so that the "owner" column no longer exists or belongs to a different table, we'd simply rewrite View in such a way that it continues to honor its api, and your code would continue to function. Sometimes the class will have a bulk function that will let you get many results from a single query, or you can write such a function and propose to add it to the class. | ||
:Also, you'll probably get a quicker response if you post your questions in the mahara.org forums, or our IRC channel, #mahara-dev or freenode. --[[User:Aaronw|aaronw]] 22:05, 14 July 2013 (UTC) | :Also, you'll probably get a quicker response if you post your questions in the mahara.org forums, or our IRC channel, #mahara-dev or freenode. --[[User:Aaronw|aaronw]] 22:05, 14 July 2013 (UTC) |
Latest revision as of 11:11, 15 July 2013
Hi,
I am a bit confused as to what a 'si query' is in the line: 'Watch out for queries inside loops (do it outside the loop all at once in a si query)' .
I have tried several searches but have drawn a blank.
Kevin
- That's a typo. It should say "in a single query". As in, rather than doing a loop over the ids (1,2,3,5) in which you do "select owner from view where id=?" four times, it's more efficient to do one query, "select owner from view where id in (1,2,3,5)" and then loop over the results. Although, I would caution against overdoing this, because if you put SQL queries directly into your page code, then you can wind up making it too tightly coupled to the current database structure. From a code reuse standpoint, it's better to call something like the "get('owner')" function of the View class in lib/view.php, and let that class decide how to query the database. Then, if we decided that we needed to refactor the "view" table so that the "owner" column no longer exists or belongs to a different table, we'd simply rewrite View in such a way that it continues to honor its api, and your code would continue to function. Sometimes the class will have a bulk function that will let you get many results from a single query, or you can write such a function and propose to add it to the class.
- Also, you'll probably get a quicker response if you post your questions in the mahara.org forums, or our IRC channel, #mahara-dev or freenode. --aaronw 22:05, 14 July 2013 (UTC)