我有一张约有1亿行的表格。必须使用BETWEEN运算符。我看到查询运行速度很慢。
我在t.name上的交易者表1上添加了2个索引,第二个索引是t.amount和t.price。
查询执行速度很慢。在使用BETWEEN运算符时,对价格和金额进行索引是否有帮助。另外,我该如何优化呢?
select t.trader_id, t.name, t.city, t.state
from trader t where exists
(
SELECT null
FROM city_present p,city_state c
WHERE p.name = 'TEST_TEST'
AND c.city = p.city
AND c.state = p.state
AND t.city = c.city
AND t.state = c.state
AND t.price IS NOT NULL
AND t.price between (25.00000 , 58.000000)
AND t.amount IS NOT NULL
AND t.amount BETWEEN (-2500.0000 , 2800.000)
)
AND t.name = 'john test'
AND t.is_valid= 1
答案 0 :(得分:4)
选择性是优化绩效的关键 - 在city_present.name为'TEST_TEST'的城市/州中,您可能拥有名为'john test'的交易员,其中is_valid = 1,而且价格介于25和58之间数量介于-2500和2800之间。
因此,我建议在交易者表上设置索引,名称,城市,州和is_valid(按此顺序),然后尝试以下查询:
SELECT t.trader_id, t.name, t.city, t.state
FROM trader t
JOIN (select distinct p.city, p.state
from city_present p
JOIN city_state s
ON s.city = p.city AND s.state = p.state
WHERE p.name = 'TEST_TEST') c
ON t.city = c.city AND t.state = c.state
WHERE t.price between (25.00000 , 58.000000)
AND t.amount BETWEEN (-2500.0000 , 2800.000)
AND t.name = 'john test'
AND t.is_valid= 1