Best Practices - mysql calls
-
I traditionally live in the client app world, so minimizing lines of code is more important than performance, so my coding style is heavy on common functions, etc. Now that I am working on a web app (PHP and mySQL), I find myself second guessing everything. Here is an example. I am working on a shopping cart for an existing site. There are multiple places where the price of a particular product is returned: Product Page Cart over view Buy process page, receipt print, etc. In most cases, the product data (price) is already available, but the price can vary in different circumstances. I'm considering one function that gets passed the important variables, calculated the price of the product, and returns that price. However, to do so will require polling the database each time each price is requested (in some cases that is multiple prices on one page, so multiple calls to the database while the page is loading). So my question is really one of best practice. How much of a noticeable performance hit does each call to the database take? Is it common to try to reduce the number of mysql calls? Do you not care? should each page have its own code, and just copy paste all over, and tweek each place where common data is shown? There is a lot of how to questions (I have posted them) but I am hoping the community here can offer some more higher level theory. Thank you in advance!
***************** "We need to apply 21st-century information technology to the health care field. We need to have our medical records put on the I.T." —GW
-
I traditionally live in the client app world, so minimizing lines of code is more important than performance, so my coding style is heavy on common functions, etc. Now that I am working on a web app (PHP and mySQL), I find myself second guessing everything. Here is an example. I am working on a shopping cart for an existing site. There are multiple places where the price of a particular product is returned: Product Page Cart over view Buy process page, receipt print, etc. In most cases, the product data (price) is already available, but the price can vary in different circumstances. I'm considering one function that gets passed the important variables, calculated the price of the product, and returns that price. However, to do so will require polling the database each time each price is requested (in some cases that is multiple prices on one page, so multiple calls to the database while the page is loading). So my question is really one of best practice. How much of a noticeable performance hit does each call to the database take? Is it common to try to reduce the number of mysql calls? Do you not care? should each page have its own code, and just copy paste all over, and tweek each place where common data is shown? There is a lot of how to questions (I have posted them) but I am hoping the community here can offer some more higher level theory. Thank you in advance!
***************** "We need to apply 21st-century information technology to the health care field. We need to have our medical records put on the I.T." —GW
I cannot answer your question directly, Some issues first of all if you a can create common function that will be used by multiple page, then make that one function. From my experience: making software using php sometime helps a lot. specially development time. But I have faced some issue when I cannot depend on php. I have faced a problem with one of my Software. I had to calculate stock by daily basis, Its not simple calculation which can be based on simple (insert into stock)-sales+(return from customer), Its more complicated than that. Because we have a different kind of stock management just because of type of product. The result is php page suffers big time. it cannot show the result. most case it ran out of memory. So, I solved it by C. I developed this page using C Another example is, report making. I take support from pdf making class MPDF(they are vanished, i cant find there site anymore). But It cannot handle big data, memory suffers. So, Now I am building them in C. Finally my point is PHP gives a great way of programming but not very good idea in every case...
-
I cannot answer your question directly, Some issues first of all if you a can create common function that will be used by multiple page, then make that one function. From my experience: making software using php sometime helps a lot. specially development time. But I have faced some issue when I cannot depend on php. I have faced a problem with one of my Software. I had to calculate stock by daily basis, Its not simple calculation which can be based on simple (insert into stock)-sales+(return from customer), Its more complicated than that. Because we have a different kind of stock management just because of type of product. The result is php page suffers big time. it cannot show the result. most case it ran out of memory. So, I solved it by C. I developed this page using C Another example is, report making. I take support from pdf making class MPDF(they are vanished, i cant find there site anymore). But It cannot handle big data, memory suffers. So, Now I am building them in C. Finally my point is PHP gives a great way of programming but not very good idea in every case...
Thanks. You say: if you a can create common function that will be used by multiple page, then make that one function. My main question is if doing so causes multiple MySQL calls that would not otherwise be required, is that bad practice? Thanks in advance.
***************** "We need to apply 21st-century information technology to the health care field. We need to have our medical records put on the I.T." —GW
-
Thanks. You say: if you a can create common function that will be used by multiple page, then make that one function. My main question is if doing so causes multiple MySQL calls that would not otherwise be required, is that bad practice? Thanks in advance.
***************** "We need to apply 21st-century information technology to the health care field. We need to have our medical records put on the I.T." —GW
some of the problem can be solved by single query, some of them are not possible in single queries if possible use multiple query