SQL Performance... design patterns etc

Associate
Joined
7 Dec 2007
Posts
302
Location
Derbyshire, Tibshelf
I was wondering about performance when doing SQL queries...

When I first started my project, I was sloshing code everywhere, entirely procedural with no functions or objects whatsoever...

Now I've started to turn most of my sloshed code, into functions so the code appears cleaner, nicer, easier to edit...

I'm kind of stuck (as im not that sure on how it affects performance) at how to design my functions...

As an example: I have a function which queries the database for two items: name and population, which seems to work fine for one use... But now I would prefer to seperate the function into two different functions, one that does a SELECT statement for name and another function that SELECTs the population and then running the two functions where I used to have one, and using one of the split offs within another function...

But how does this affect performance? Is it really much of a performance decrease/increase to do two SELECT statements rather than one? or perhaps im better off leaving the old function alone and creating one for the other use I need?

I heard that opening and closing an SQL connection is where the SQL performance slugs, so if I were to use pconnect on apache to keep it open, would it be ok to do 2 select statements rather than one?

Functions and queries are shown below to show what I mean... Thanks

function getNamePop($map_id){
$query = "SELECT name, population FROM village WHERE map_id = '".$map_id."'";

function getName($map_id){
$query = "SELECT name FROM village WHERE map_id = '".$map_id."'";

function getPop($map_id){
$query = "SELECT pop FROM village WHERE map_id = '".$map_id."'";
 
If it's a simple query with no joins and one where on an indexed field it's pretty harmless but your still wasting resources you don't need to.

You can global the connection object and keep it through your whole page, pconnect isn't needed for this.
 
Back
Top Bottom