[Solved] Mysql select speed

EverSQL Database Performance Knowledge Base

Mysql select speed

Database type:

I have a problem with a speed of mysql query. I have 2 select boxes with country and cities, on first select changes cities on the second, its provided by ajax:

$(document).ready(function()
{

$(".country").change(function()
{
var id=$(this).val(); 
var dataString = 'id='+ id;
function doAJAX(url) {

$.ajax
({
type: "POST",
url: url,

data: dataString,
cache: false,
success: function(html)
{
$(".city").html(html); 
}

});
}




doAJAX('search_ajax.php');
});
});

When is for example spain or russia where is so many cities, showing them took too much time, table cities:fips 2 varchar, city varchar 100, my select :

SELECT city from cities where fips='some value' //for example EN or any ..

I have on column fips index.

Can anobody help mi to speed this up? or is a better solution other than ajax to make live change of select's?? thanks

How to optimize this SQL query?

The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
Optimal indexes for this query:
ALTER TABLE `cities` ADD INDEX `cities_idx_fips` (`fips`);
The optimized query:
SELECT
        cities.city 
    FROM
        cities 
    WHERE
        cities.fips = 'some value'

Related Articles



* original question posted on StackOverflow here.