简体   繁体   English

按键自动完成查询到300万行表

[英]Key press autocomplete queries to a 3 million rows table

I have a table fs_city , with 3 millions cities all around the world, as well as a table fs_country . 我有一个表fs_city ,具有世界各地300万个城市,以及一个表fs_country

When the user visits the website it detects its country code, the user is required to select their country and city from a box (which looks for cities in fs_city on key press), if the user is from USA, and types "Ne", he will get a drop down list with "New York" for ie I do this to create an autocomplete input. 当用户访问它检测到其国家代码的网站时,如果用户来自美国,则要求用户从框中选择其国家和城市(在fs_city上查找fs_city城市),如果用户输入“ Ne”,他将通过“纽约”获得一个下拉列表,例如,我这样做是为了创建自动完成输入。

The problem is that, the query is sent upon every key press, in my example "Ne", there are two queries to the table fs_city . 问题是,查询是在每次按键时发送的,在我的示例“ Ne”中,对表fs_city有两个查询。

Also, even if there's only one query, it takes 6 seconds to return a response from that table... My table has primary keys. 另外,即使只有一个查询,它也需要6秒钟才能从该表返回响应...我的表具有主键。

This is my SQL query: 这是我的SQL查询:

SELECT 
    ci.city_id,
    ci.country_code,
    ci.city,
    ci.region,
    co.country_name
FROM 
    fs_city as ci,
    fs_country as co
WHERE
    ci.city LIKE "Ne%"
    AND co.country = ci.country_code
    AND ci.country_code = :country
ORDER BY 
    ci.city ASC 
    LIMIT 0,20

How can I create an autocomplete feature (with keypress), and how to speed up fs_city table queries? 如何创建自动完成功能(带按键),以及如何加快fs_city表查询?

Updated : 更新 :

fs_city primary key : city_id fs_city主键:city_id

fs_country primary key : country_id fs_country主键:country_id

Engine : InnoDB 引擎:InnoDB

jQuery has nice widget with exactly what you are looking for and I'm pretty sure you can limit it to only start query the database after a set amount of characters so you can limit your results. jQuery具有与您要查找的东西完全一样的漂亮小部件,并且我敢肯定您可以限制它,使其仅在设置一定数量的字符后才开始查询数据库,以便您可以限制结果。 That should solve your speed problem and shorten your javascript. 那应该解决您的速度问题并且缩短您的javascript。 http://jqueryui.com/autocomplete/ http://jqueryui.com/autocomplete/

EDIT MySQL also has a LIMIT term to cap the return, which might also help. EDIT MySQL还有一个LIMIT项来限制返回值,这也可能有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM