简体   繁体   中英

In Oracle SQL, can I query a partition of a table instead of an entire table to make it run faster?

I would like to query a table with a million records for customers named 'FooBar' that have records dated on 7-24-2016. The table has 10 days of data in it.

select * 
from table
where customer = 'FooBar'
and insert_date between to_date('2016-07-24 00:00:00', 'YYYY-MM-DD HH24:MI:SS') and to_date('2016-07-24 23:59:59', 'YYYY-MM-DD HH24:MI:SS');

The problem with the query above is that it takes awhile to run. I would like it to run faster.

The table is partitioned into 24 hr days. Could I focus the query on the table partitions? Would that make the query run faster?

select *
from partition-7-24-2016
where customer = 'FooBar'; 

The correct syntax is select [columns] from [table] partition ([partition]) . So, in this usecase, you'd have something like this:

SELECT *
FROM   mytable PARTITION (partition_7_24_2016)
WHERE  customer = 'FooBar'; 

You can do it like this:

select * from table PARTITION FOR (date '2016-07-24') where customer = 'FooBar'
and insert_date between to_date('2016-07-24 00:00:00', 'YYYY-MM-DD HH24:MI:SS') and to_date('2016-07-24 23:59:59', 'YYYY-MM-DD HH24:MI:SS');

This will only look for rows in partition where your date falls into - date '2016-07-24' in this example.

You need to make sure you supply partition value in the brackets. Also, if you created an index - make sure it's local, otherwise you won't see much improvement over selecting from table itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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