Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2004
    Posts
    2

    Unanswered: 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

  2. #2
    Join Date
    Aug 2004
    Location
    France
    Posts
    754
    Sorry, posted twice... please read the post below.
    Last edited by RBARAER; 10-11-04 at 13:17.

  3. #3
    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

  4. #4
    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".

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •