Nikandlv Posted December 11, 2016 Share Posted December 11, 2016 Hi , Im wondering if is it efficient to do a sql (using pdo) quary for every visitor of website ? for example we have a page and we have a <h1> tag and its how we use it in here <?php echo '<h1>'; echo getthewebsitetitlefromdbfunctionlol() ; echo '</h1>; ?> and the getthewebsitetitlefromdbfunctionlol() will basically selects the title from db and return it . so is it efficient to do this for every visitor of the page ? Quote Link to comment https://forums.phpfreaks.com/topic/302717-is-it-efficient/ Share on other sites More sharing options...
NotionCommotion Posted December 11, 2016 Share Posted December 11, 2016 Well, I suppose it is more efficient to hardcode the title, but it is less flexible and realistically, it the performance doesn't matter. But instead, why not get all your site information with one query, and then just echo("<h1>$siteStuff[title]</h1>");? Quote Link to comment https://forums.phpfreaks.com/topic/302717-is-it-efficient/#findComment-1540211 Share on other sites More sharing options...
Nikandlv Posted December 11, 2016 Author Share Posted December 11, 2016 Well, I suppose it is more efficient to hardcode the title, but it is less flexible and realistically, it the performance doesn't matter. But instead, why not get all your site information with one query, and then just echo("<h1>$siteStuff[title]</h1>");? i guess so , that will be a good idea to get the info as an array for all of stuff i want in my page i t will do the request once thanks for the suggestion Quote Link to comment https://forums.phpfreaks.com/topic/302717-is-it-efficient/#findComment-1540212 Share on other sites More sharing options...
Jacques1 Posted December 11, 2016 Share Posted December 11, 2016 This discussion is silly. Every nontrivial web application makes dozens or hundreds of queries per request, and modern database systems can easily handle this. That's what they're made for. Even complex queries involving millions of rows (not just some title lookup) are no problem at all as long as you use proper indexes. Query results are also cached, so the query isn't actually executed “for every visitor”. Stop trying to solve problems that aren't there. Quote Link to comment https://forums.phpfreaks.com/topic/302717-is-it-efficient/#findComment-1540213 Share on other sites More sharing options...
NotionCommotion Posted December 11, 2016 Share Posted December 11, 2016 But it would also be silly to make 30 requests to the same table to retrieve 30 column values, no? Quote Link to comment https://forums.phpfreaks.com/topic/302717-is-it-efficient/#findComment-1540215 Share on other sites More sharing options...
Nikandlv Posted December 11, 2016 Author Share Posted December 11, 2016 This discussion is silly. Every nontrivial web application makes dozens or hundreds of queries per request, and modern database systems can easily handle this. That's what they're made for. Even complex queries involving millions of rows (not just some title lookup) are no problem at all as long as you use proper indexes. Query results are also cached, so the query isn't actually executed “for every visitor”. Stop trying to solve problems that aren't there. then sorry for asking ! i was wondering how it works . im not that level experienced to know that Quote Link to comment https://forums.phpfreaks.com/topic/302717-is-it-efficient/#findComment-1540216 Share on other sites More sharing options...
Solution Jacques1 Posted December 11, 2016 Solution Share Posted December 11, 2016 No need to apologize. I'm simply pointing out that efficiency isn't the issue here. Choose the most readable solution. If it makes sense to only fetch the title, that's perfectly valid. It doesn't matter if you need a few extra queries, because there won't be any performance difference at all. Quote Link to comment https://forums.phpfreaks.com/topic/302717-is-it-efficient/#findComment-1540219 Share on other sites More sharing options...
NotionCommotion Posted December 11, 2016 Share Posted December 11, 2016 Totally agree that efficiency isn't an issue, and making it the most readable (and maintainable) is much more important. Often it will also be the most efficient as well as you will not make silly mistakes. Quote Link to comment https://forums.phpfreaks.com/topic/302717-is-it-efficient/#findComment-1540220 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.