|
Posted by Jerry Stuckle on 11/26/07 13:19
Michael Martinek wrote:
>> So as far as I can tell: it is not a waste of memory, but it offers you
>> the choice what you want:
>> 1) faster code/less IO (use only indexes)
>> 2) better readable/more robust code. (use assoc arrays)
>>
>> just my 2 cent.
>>
>> Regards,
>> Erwin Moller
>
> And I fully agree with you, Erwin.
>
>
> As for the general topic, if you want to be able to expand on your
> code later, without having to worry about reviewing every SQL query..
> use associative arrays. For example, if you have a SQL query 'SELECT *
> FROM table WHERE something'.. then your numerical indexed array is
> going to be specific. But what happens if a DB admin inserts a new
> column somewhere, or drops one? Now you'll need to review all the
> code.. and good luck with that, because $row[3] is harder to find
> meaning in than $row['data_crc']. And seriously, we're in a day and
> age where using a little extra memory here or there isn't going to do
> any harm. In a business scenario, it'll likely cost you more to be as
> system efficient as you possibly can.
>
SELECT * is not a good idea. You should always specify the columns you
want, even if it's all current columns in the table. What happens if
someone adds a column with 2MB BLOBs, for instance? Or deletes a
column? SELECT * will take a lot of memory in the first case and NOT
get an error in the second - creating a possible difficult problem to find.
> I always use mysql_fetch_assoc, unless I'm doing a "SELECT
> COUNT(*) ..."
>
> If it's a script you know is going be extremely intensive to run, and
> using some extra storage here and there is going to bog down resources
> then use fetch_array.. but if that's the case, you might want to
> upgrade. ;) Also keep in mind, signed integers aren't free.. and if
> you're really worried about resources, do an unset() on variables when
> they are large arrays and contain many entries. When passing arrays to
> a function, pass them by reference so a new copy isn't made.
>
> My 1/50th of a dollar in summation:
> If you're writing code for a business, contract work, or something
> that will need to be maintained then use associative arrays. Otherwise
> if you're writing a quick one-time usage script that probably won't
> need to be maintained, expanded on, debugged.. then go ahead and used
> numerically indexed arrays. However, the speed increase over
> associative arrays in normal circumstances is not worth using as a
> reason to do so.
>
Good advice.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Navigation:
[Reply to this message]
|