in my mysql database i’ve got the geonames database, containing all countries, states and cities.
i am using this to create a cascading menu so the user could select where he is from: country -> state -> county -> city.
but the main problem is that the query will search through all the 7 millions rows in that table each time i want to get the list of children rows, and that is taking a while 10-15 seconds.
i wonder how i could speed this up: caching? table views? reorganizing table structure somehow?
and most important, how do i do these things? are there good tutorials you could link to me?
i appreciate all help and feedback discussing smart ways of handling this issue!
UPDATE: here is my table structure:
CREATE TABLE `geonames_copy` (
`geoname_id` mediumint(9) NOT NULL,
`parent_id` mediumint(9) DEFAULT NULL,
`name` varchar(200) DEFAULT NULL,
`ascii_name` varchar(200) DEFAULT NULL,
`alternate_names` varchar(4000) DEFAULT NULL,
`latitude` decimal(10,7) DEFAULT NULL,
`longitude` decimal(10,7) DEFAULT NULL,
`feature_class` char(1) DEFAULT NULL,
`feature_code` varchar(10) DEFAULT NULL,
`country_code` varchar(2) DEFAULT NULL,
`cc2` varchar(60) DEFAULT NULL,
`admin1_code` varchar(20) DEFAULT NULL,
`admin2_code` varchar(80) DEFAULT NULL,
`admin3_code` varchar(20) DEFAULT NULL,
`admin4_code` varchar(20) DEFAULT NULL,
`population` bigint(20) DEFAULT NULL,
`elevation` int(11) DEFAULT NULL,
`gtopo30` smallint(6) DEFAULT NULL,
`time_zone` varchar(40) DEFAULT NULL,
`modification_date` date DEFAULT NULL,
PRIMARY KEY (`geoname_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
and here is the sql query:
$query = "SELECT geoname_id, name
FROM geonames
WHERE parent_id = '$geoname_id'
AND (feature_class = 'A')";
should i just create index for 2 columns: parent_id and feature_class?
one question: isn´t it better to create an index with solr instead of using mysql? one benefit is that im already using solr and another is that it supports full text search. so maybe it’s better so i dont use both solr and mysql (2 things to be good at)?
As mentioned, more info would be helpful (Sql, database structure).
The AJAX suggestion is a good one, though you could also do this without ajax.
Do NOT execute a select at any point that selects all of the data. This will be extremely slow.
First, populate the only list of countries. Allow the user to make a selection from this list. After the user selects a country, either via AJAX, or by refreshing the entire page, populate the list of states for that country only – something like (select state from geonames where country = @country). When the user selects a state, populate the list of counties for that country and state – something like (select country from geonames where country = @country and state = @state). Continue in this manner for the city.
I’m not very familiar with MySql, but in SqlServer I would create an index on (Country, State, County, City) to speed up this set of queries. I’m not sure if MySql would be able to accelerate the entire set of queries with this index or not.
Of course, I’m making some assumptions about how your data is structured here, so this info may or may not be relevant.