Oracle query performance with BETWEEN operator

让人想犯罪 __ 提交于 2019-12-13 03:00:01

问题


I have a table with about 100million rows. Have to use BETWEEN operator. I see that query is running very slow.

I added 2 indexes on trader table one on t.name and second index is t.amount and t.price.

Query is performing very slow. Does indexing on price and amount help when using BETWEEN operator. Also, how can I optimize this?

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

回答1:


Selectivity is the key to performance in optimisation - you probably have vastly fewer traders called 'john test', with is_valid = 1, in cities/states where the city_present.name is 'TEST_TEST', than have price between 25 and 58 and amount between -2500 and 2800.

Therefore, I recommend setting up an index on the trader table, on name, city, state and is_valid (in that order), and then try the following query:

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


来源:https://stackoverflow.com/questions/16882344/oracle-query-performance-with-between-operator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!