2017-07-06 70 views
0

我有一個大型數據庫,有超過310 k行。每一行都有以下字段:MYSQL多樣喜歡搜索位置

國家
地區

這裏是我的查詢 - 我使用PHP PDO來查詢我的財產分貝:

$sql = 'SELECT * FROM properties 
      WHERE (Country = "'.$searchString.'" OR Region = "'.$searchString.'" 
        OR City = "'.$searchString.'") LIMIT 100'; 
$stmt = $pdo->query($sql); 

現在所有的,雖然這工作正常但我知道這是凌亂和資源密集型任何人都可以向我解釋一個更清潔更快的方式做這也索引混淆我與MySQL,我應該爲三列設置一個索引,你會建議什麼樣的索引。

任何幫助或指針將不勝感激

+0

瞭解準備好的語句,以防止SQL注入 – Jens

+0

嗨jens,我實際上在我的實時代碼中使用了準備好的語句,對不起,我應該提到我只是在我的示例中將它們留下了 –

回答

0

索引是不會幫助,因爲你寫的查詢。你可以寫上properties(Country, Region, City)添加索引和使用這樣的查詢:

select p.* 
from properties p 
where country = @searchString 
union all 
select p.* 
from properties p 
where region = @searchString and country <> @searchString 
union all 
select p.* 
from properties p 
where city = @searchString and region <> @searchString and country <> @searchString; 

這可能會有所幫助,但往往一個or查詢返回所以許多行的索引不是特別有幫助。