views:

43

answers:

2

I'm using this SQL query to create an index:

    $query = "CREATE INDEX id_index2
            ON countries(geoname_id, name)";

How do I update the index when new entries are added?

Should I run a PHP script with the update query in CRON and run it every night?

Is this best practice for automated index updating?

+2  A: 

Indexes are updated by MySQL automatically as you insert/update rows. (This may be a performance hitter for huge websites, but you probably aren't running a huge one :).) You don't need to do anything after the initial creation.

Coronatus
but when my site is getting bigger, and it becomes "laggy", how do i do then? deactive automated updates and update manually? delete index and then create new one?
never_had_a_name
Wait until it is demonstrably (measurably) a problem. It probably won't be a problem. If you're worried, keep an eye on whether you're using the best performing storage engine, and try to minimize the DBMS-specific code in your application so it will be easy to move to an alternative DBMS should it prove necessary. Rebuilding indexes from scratch will be much slower than maintaining them in ordinary use - don't even think of deleting the index and creating a new one except in extremis.
Jonathan Leffler
Countries don't change all that often, on average. If you have lots of 'geoname_id' values and are determining the country each is located in, then you could have more traffic. Still, unless you know that MySQL indexing is worse than the average DBMS (which I doubt), you should not worry about it - let MySQL do its work.
Jonathan Leffler
+2  A: 

It will be automatically updated after adding rows, No need to run any update statements.

Wael Dalloul