在 Oracle SQL 中,我可以查询 table 的分区而不是整个 table 以使其更快 运行 吗?
In Oracle SQL, can I query a partition of a table instead of an entire table to make it run faster?
我想为名为 'FooBar' 的客户查询具有一百万条记录的 table,这些记录的日期为 7-24-2016。 table 中有 10 天的数据。
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');
上面查询的问题是 运行 需要一段时间。我希望它 运行 更快。
table 分为 24 小时制。我可以将查询集中在 table 分区上吗?这会使查询 运行 更快吗?
select *
from partition-7-24-2016
where customer = 'FooBar';
正确的语法是select [columns] from [table] partition ([partition])
。所以,在这个用例中,你会有这样的东西:
SELECT *
FROM mytable PARTITION (partition_7_24_2016)
WHERE customer = 'FooBar';
你可以这样做:
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');
这将只查找分区中您的日期所在的行 - 在本例中为 date '2016-07-24'
。
您需要确保在括号中提供分区值。
此外,如果您创建了一个索引 - 确保它是本地的,否则您不会看到比从 table 本身进行选择有多大改进。
我想为名为 'FooBar' 的客户查询具有一百万条记录的 table,这些记录的日期为 7-24-2016。 table 中有 10 天的数据。
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');
上面查询的问题是 运行 需要一段时间。我希望它 运行 更快。
table 分为 24 小时制。我可以将查询集中在 table 分区上吗?这会使查询 运行 更快吗?
select *
from partition-7-24-2016
where customer = 'FooBar';
正确的语法是select [columns] from [table] partition ([partition])
。所以,在这个用例中,你会有这样的东西:
SELECT *
FROM mytable PARTITION (partition_7_24_2016)
WHERE customer = 'FooBar';
你可以这样做:
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');
这将只查找分区中您的日期所在的行 - 在本例中为 date '2016-07-24'
。
您需要确保在括号中提供分区值。 此外,如果您创建了一个索引 - 确保它是本地的,否则您不会看到比从 table 本身进行选择有多大改进。