Welcome Guest, Not a member yet? Register   Sign In
Asking about performance of data retrieval
#1

[eluser]dimazarno[/eluser]
hello,

i write code like this, i'm using variable $type for select and return value from single field, whether this will affects the performance of data retrieval?

Code:
//get one field value
    function get_shop($type){
        $this->db->select($type);
        $this->db->from('shop');
        $query = $this->db->get();    
        foreach ($query->result() as $row)
        {
            return $row->$type;
        }        
    }

thanks.
#2

[eluser]nzmike[/eluser]
On the data retrieval? No it will not affect the speed of the database because by the time your query gets to the database the variable will have been evaluated to a string. On the other hand will it affect the overall speed of your program? Yes it will...but the performance difference is so slight it's probably not even measurable.

So in other words I can't see any performance issues with this piece of code. I am wondering why you're using a for loop though. Will it not drop out of the function after the first iteration?
#3

[eluser]jedd[/eluser]
Indeed.

This is slightly neater:
Code:
//get one field value
    function get_shop($type){
        $this->db->select($type);
        $this->db->from('shop');
        $query = $this->db->get();
        if ($query->num_rows() == 1)  {
                $row = $this->db->row();
                return $row->$type;
                }
        return FALSE;
    }

I check for =1 so it stands out if you're returning > 1 element in this query (presumably that's a bad thing and you want to know about it).
I think with PHP5 you can chain things in that if block, but AR calls aren't my forte.
#4

[eluser]BrianDHall[/eluser]
jedd is right on chaining, so I believe this would work:

Code:
$query = $this->db->select($type)->from('shop')->get();

Purely a matter of preference though.

As to the thread topic, this code is actually about as efficient as anyone could ask for.

Generally you have to screw up pretty bad to do something that is really going to effect performance in a big way - in fact, you rarely need to worry about performance until you actually experience a slow down and need to go back and optimize your program.

The only really 'dangerous' queries are unrestricted get() calls that pull in massive loads of records (if you store images in a database you have to be a little careful not to generate a select * from images sort of call, as you could be asking for gigabytes worth of information you didn't really need - or extremely complex joins on large tables with lots of records which are beyond my understanding.
#5

[eluser]garymardell[/eluser]
If you are only trying to return a single shop (which your function will do) i would advise using ->limit(1). I think, dont quote me this will speed up the query aswell as mysql can stop once it finds a match rather than trying to find all rows and then you selecting just the first one.
#6

[eluser]dimazarno[/eluser]
first i'm not sure whether the variable will affect the speed of retrieval data. but it seems affect to the program speed which i dont need worry because the difference is so slight.

thanks all, it's really nice community.




Theme © iAndrew 2016 - Forum software by © MyBB