简体   繁体   English

SQL Select Top with Left Join和Where子句

[英]SQL Select Top with Left Join and Where clause

I have two database tables: 我有两个数据库表:

Cities with columns: 有专栏的城市

Country_Code | City_Code | City_Name  

Countries with columns 有专栏的国家

Country_Code | Country_Name

Based on a few chars entered by User, it checks the City_Name column to return results to populate a City autocomplete box. 根据用户输入的几个字符,它检查City_Name列以返回结果以填充City自动完成框。 The result needs to have the city code, city name, country code, and country name, hence the need for a join. 结果需要具有城市代码,城市名称,国家/地区代码和国家/地区名称,因此需要加入。

The query I am using is 我正在使用的查询是

 SELECT TOP 10 
     ci.Country_Code, ci.City_Code, ci.City_Name, co.Country_Name  
 FROM 
     Cities ci 
 LEFT OUTER JOIN 
     Countries co ON ci.Country_Code = co.Country_Code  
 WHERE 
     ci.City_Name LIKE '@CityName'  
 ORDER BY 
     ci.City_Name  

The results I get are correct, but the query takes a long time to complete. 我得到的结果是正确的,但是查询需要很长时间才能完成。 From what I understand, first, the results contain join of both the tables, then the where clause kicks in to get the specific rows only, which are ordered by City Name and top 10 results returned. 据我了解,首先,结果包含两个表的联接,然后使用where子句以仅获取特定行,这些行按城市名称排序,并返回前10个结果。

My question is, is there a way to speed up the query. 我的问题是,有没有一种方法可以加快查询速度。 Have the where clause checked, and then only perform the join, better still perform it only on the top 10 results? 是否检查过where子句,然后仅执行联接,最好还是仅对前10个结果进行联接? I tried putting my WHERE clause in the ON clause, but that gave wrong results. 我尝试将WHERE子句放在ON子句中,但是结果不正确。


EDIT : @CityName contains 2-3 chars entered by the user and then a '%'. 编辑:@CityName包含用户输入的2-3个字符,然后是'%'。

I'd suggest start with adding clustered index on Countries.Country_Code (also making it the primary key of the Countries table if it is not already so). 我建议开始与添加聚簇索引 Countries.Country_Code (也使其成为国家表的主键,如果它是不是已经如此)。 An index would sort the table such that the search speed in join is increased. 索引将对表进行排序,从而提高联接中的搜索速度。

This appears to be your query: 这似乎是您的查询:

SELECT TOP 10 ci.Country_Code, ci.City_Code, ci.City_Name, co.Country_Name  
FROM Cities ci LEFT OUTER JOIN 
     Countries co
     ON ci.Country_Code = co.Country_Code  
WHERE ci.City_Name LIKE @CityName  
ORDER BY ci.City_Name  ;

Quotes should not be needed around @CityName . @CityName周围@CityName

I don't understand the LEFT JOIN . 我不了解LEFT JOIN It suggests that there are cities without a valid Country_Code -- and that seems unlikely. 这表明有些城市没有有效的Country_Code这似乎不太可能。

Assuming @CityName does not start with a wildcard (as suggested by your question), then this can make use of an index. 假设@CityName 并非以通配符开头(如您的问题所建议),则可以利用索引。 I would suggest the following indexes: 我建议以下索引:

  • cities(city_name, country_code)
  • countries(country_code, country_name)

The second is not needed if country_code is a primary key. 如果country_code是主键,则不需要第二个。

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

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