yii global variable

i have stored countries and cities in database. the number of records can be over 2millions.

is there a way to store these in Yii, so that all users of all sessions can access the records, but the database is only being queried once only.

Where do you want to save the data? PHP is a language where the process is terminated after every request…

So you need to have an external data storage and this data storage will be queried again with every request…

Where would you like to store the data? In RAM? Every variable you make in PHP will be nulled after the request is terminated and only external file storages can hold the data (like databases, files on disc, etc…)

Why do you want to query the database only once? Maybe there is a solution but at the moment I can’t understand what you want to do…

I don’t know what you want to do but maybe you want to query the database only once because you want:

more speed: Use an other database like MongoDB or MySQL ENGINE MEMORY

external database: synchronise the database to a local database and use this

Tell us why you want to query the database only once…

Hi twb,

2 million is a large number.

It will be about 244 MB if we assume a single record consumes 128 bytes.

I don’t think it’s a good idea to load them all to RAM at once.

hi, my concern is as follow. i do not know if it is valid though…

because there are so many records of cities, to query the database to load all the cities for every user (say 10 concurrent users), it will also be quite heavy for the database server? afterwhich, to perform some operations on the result set every time, i thought it may be better if i can query once, perform operations once, and store it for future usage?

What operations do you want to perform on the data from the database? Most calculations and all possible filter can be made direct by the database… no need to make this operations in PHP…

If you design good queries you will have no need to load all data in RAM because SQL can be very very fast. And if you want to have everything in RAM: MySQL Engine Memory… ;)

the operation on the database is to load all the records of cities…

the other php operations are such as json_encode…