If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > need help with MySQL vs. arrays dilemma

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-10-04, 06:08
newsman newsman is offline
Registered User
 
Join Date: Oct 2004
Posts: 2
need help with MySQL vs. arrays dilemma

Hello!

I got the following question. I use countries in my site - there are almost 250 of them.

Which variant will be faster for extracting country name by its code and creating a <select> with those countries? And which variant is less resource-consuming?

VARIANT 1

1. Create an array, say, $countries with keys as country codes (i.e. "eg" for "Egypt").
2. To extract country name by stored code use $country[$country_code]
3. To make a <SELECT use a function with WHILE ($element=each($country)) cycle, putting out all <OPTIONs as country code/country name pairs

VARIANT 2

1. Create a MySQL table with country codes as unique fields
2. To extract country name access a db and pull out a name with SELECT ... WHERE country_code='eg'
3. To make a <SELECT use a function with SELECT * FROM that_table_with_countries...



Any ideas? Any suggestion would be greately appreciated!

Thank you!
Dennis
Reply With Quote
  #2 (permalink)  
Old 10-11-04, 11:15
RBARAER RBARAER is offline
Registered User
 
Join Date: Aug 2004
Location: France
Posts: 754
Sorry, posted twice... please read the post below.

Last edited by RBARAER; 10-11-04 at 12:17.
Reply With Quote
  #3 (permalink)  
Old 10-11-04, 11:54
RBARAER RBARAER is offline
Registered User
 
Join Date: Aug 2004
Location: France
Posts: 754
Hello,

Well, undoubtedly, using an in-memory array will be much faster than retrieving data from your database (though it would not take long). However, that means that you would have the list of countries/country codes hard-coded instead of having them in the database and reload them dynamically.

In the case of countries, the list won't change tomorrow , so I think you can afford to hard-code it. Now, if you already have a MySQL database, creating a table and then execute queries on it won't be too heavy in this case. The second solution is cleaner and I may prefer this one. However, you will lose in terms of performance : in-memory work is always faster. Moreover, though in a non-web environment the difference may be unnoticeable with such a query, remember that if you use a table in a web environment, you will have to reload the page when you execute the query so as to reach the MySQL server, while you wouldn't have to if the list was hard-coded.

Now the choice is yours.

Regards,

RBARAER
Reply With Quote
  #4 (permalink)  
Old 10-12-04, 07:29
newsman newsman is offline
Registered User
 
Join Date: Oct 2004
Posts: 2
Quote:
Originally Posted by RBARAER
... Now the choice is yours.
Regards,
RBARAER
Thank you for the answer!

P.S.
Later i've made a speed-check of the scripts with ZEND Studio: a page "with an array" works more than 30% faster than that "with db".
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On